Skip to content

Commit

Permalink
Merge pull request #106 from hs-web/3.0.x
Browse files Browse the repository at this point in the history
3.0.4
  • Loading branch information
zhou-hao authored Dec 19, 2018
2 parents e8dc76c + 63615a7 commit a82c50a
Show file tree
Hide file tree
Showing 222 changed files with 2,779 additions and 273 deletions.
2 changes: 1 addition & 1 deletion hsweb-authorization/hsweb-authorization-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>hsweb-authorization</artifactId>
<groupId>org.hswebframework.web</groupId>
<version>3.0.3</version>
<version>3.0.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ default boolean hasRole(String roleId) {
* @param <T> 属性值类型
* @return Optional属性值
*/
@Deprecated
<T extends Serializable> Optional<T> getAttribute(String name);

/**
Expand All @@ -139,7 +138,6 @@ default boolean hasRole(String roleId) {
* @param object 属性值
* @see AuthenticationManager#sync(Authentication)
*/
@Deprecated
void setAttribute(String name, Serializable object);

/**
Expand All @@ -148,7 +146,6 @@ default boolean hasRole(String roleId) {
* @param attributes 属性值map
* @see AuthenticationManager#sync(Authentication)
*/
@Deprecated
void setAttributes(Map<String, Serializable> attributes);

/**
Expand All @@ -159,15 +156,13 @@ default boolean hasRole(String roleId) {
* @return 被删除的值
* @see AuthenticationManager#sync(Authentication)
*/
@Deprecated
<T extends Serializable> T removeAttributes(String name);

/**
* 获取全部属性,此属性为通过{@link this#setAttribute(String, Serializable)}或{@link this#setAttributes(Map)}设置的属性。
*
* @return 全部属性集合
*/
@Deprecated
Map<String, Serializable> getAttributes();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.hswebframework.web.authorization.annotation;

import org.hswebframework.web.authorization.twofactor.TwoFactorValidator;

import java.lang.annotation.*;

/**
* 开启2FA双重验证
*
* @see org.hswebframework.web.authorization.twofactor.TwoFactorValidatorManager
* @see org.hswebframework.web.authorization.twofactor.TwoFactorValidatorProvider
* @see org.hswebframework.web.authorization.twofactor.TwoFactorValidator
* @since 3.0.4
*/
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface TwoFactor {

/**
* @return 接口的标识, 用于实现不同的操作, 可能会配置不同的验证规则
*/
String value();

/**
* @return 验证有效期, 超过有效期后需要重新进行验证
*/
long timeout() default 10 * 60 * 1000L;

/**
* 验证器供应商,如: totp,sms,email,由{@link org.hswebframework.web.authorization.twofactor.TwoFactorValidatorProvider}进行自定义.
* <p>
* 可通过配置项: hsweb.authorize.two-factor.default-provider 来修改默认配置
*
* @return provider
* @see TwoFactorValidator#getProvider()
*/
String provider() default "default";

/**
* 验证码的http参数名,在进行验证的时候需要传入此参数
*
* @return 验证码的参数名
*/
String parameter() default "verifyCode";

/**
* @return 关闭验证
*/
boolean ignore() default false;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.hswebframework.web.authorization.exception;

import lombok.Getter;

/**
* 权限验证异常
*
Expand All @@ -10,6 +12,9 @@ public class AccessDenyException extends RuntimeException {

private static final long serialVersionUID = -5135300127303801430L;

@Getter
private String code;

public AccessDenyException() {
this("权限不足,拒绝访问!");
}
Expand All @@ -21,4 +26,9 @@ public AccessDenyException(String message) {
public AccessDenyException(String message, Throwable cause) {
super(message, cause);
}

public AccessDenyException(String message, String code, Throwable cause) {
super(message, cause);
this.code = code;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.hswebframework.web.authorization.exception;

import lombok.Getter;

/**
* @author zhouhao
* @since 3.0.4
*/
@Getter
public class NeedTwoFactorException extends RuntimeException {
private static final long serialVersionUID = 3655980280834947633L;
private String provider;

public NeedTwoFactorException(String message, String provider) {
super(message);
this.provider = provider;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.hswebframework.web.authorization.setting;

import java.util.List;
import java.util.Optional;

/**
* @author zhouhao
* @since 1.0.0
*/
public class SettingNullValueHolder implements SettingValueHolder {

public static final SettingNullValueHolder INSTANCE = new SettingNullValueHolder();

private SettingNullValueHolder() {
}

@Override
public <T> Optional<List<T>> asList(Class<T> t) {
return Optional.empty();
}

@Override
public <T> Optional<T> as(Class<T> t) {
return Optional.empty();
}

@Override
public Optional<String> asString() {
return Optional.empty();
}

@Override
public Optional<Long> asLong() {
return Optional.empty();
}

@Override
public Optional<Integer> asInt() {
return Optional.empty();
}

@Override
public Optional<Double> asDouble() {
return Optional.empty();
}

@Override
public Optional<Object> getValue() {
return Optional.empty();
}

@Override
public UserSettingPermission getPermission() {
return UserSettingPermission.NONE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.hswebframework.web.authorization.setting;

import java.util.List;
import java.util.Optional;

public interface SettingValueHolder {

SettingValueHolder NULL = SettingNullValueHolder.INSTANCE;

<T> Optional<List<T>> asList(Class<T> t);

<T> Optional<T> as(Class<T> t);

Optional<String> asString();

Optional<Long> asLong();

Optional<Integer> asInt();

Optional<Double> asDouble();

Optional<Object> getValue();

UserSettingPermission getPermission();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.hswebframework.web.authorization.setting;


import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.hswebframework.utils.StringUtils;
import org.hswebframework.web.dict.EnumDict;

import java.util.List;
import java.util.Optional;

/**
* @author zhouhao
* @since 3.0.4
*/
@AllArgsConstructor
@Getter
public class StringSourceSettingHolder implements SettingValueHolder {

private String value;

private UserSettingPermission permission;

public static SettingValueHolder of(String value, UserSettingPermission permission) {
if (value == null) {
return SettingValueHolder.NULL;
}
return new StringSourceSettingHolder(value, permission);
}

@Override
public <T> Optional<List<T>> asList(Class<T> t) {
return getNativeValue()
.map(v -> JSON.parseArray(v, t));
}

protected <T> T convert(String value, Class<T> t) {
if (t.isEnum()) {
if (EnumDict.class.isAssignableFrom(t)) {
T val = (T) EnumDict.find((Class) t, value).orElse(null);
if (null != val) {
return val;
}
}
for (T enumConstant : t.getEnumConstants()) {
if (((Enum) enumConstant).name().equalsIgnoreCase(value)) {
return enumConstant;
}
}
}
return JSON.parseObject(value, t);
}

@Override
@SuppressWarnings("all")
public <T> Optional<T> as(Class<T> t) {
if (t == String.class) {
return (Optional) asString();
} else if (Long.class == t || long.class == t) {
return (Optional) asLong();
} else if (Integer.class == t || int.class == t) {
return (Optional) asInt();
} else if (Double.class == t || double.class == t) {
return (Optional) asDouble();
}
return getNativeValue().map(v -> convert(v, t));
}

@Override
public Optional<String> asString() {
return getNativeValue();
}

@Override
public Optional<Long> asLong() {
return getNativeValue().map(StringUtils::toLong);
}

@Override
public Optional<Integer> asInt() {
return getNativeValue().map(StringUtils::toInt);
}

@Override
public Optional<Double> asDouble() {
return getNativeValue().map(StringUtils::toDouble);
}

private Optional<String> getNativeValue() {
return Optional.ofNullable(value);
}

@Override
public Optional<Object> getValue() {
return Optional.ofNullable(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.hswebframework.web.authorization.setting;

/**
* @author zhouhao
* @since 3.0.4
*/
public interface UserSettingManager {

SettingValueHolder getSetting(String userId, String key);

void saveSetting(String userId, String key, String value, UserSettingPermission permission);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.hswebframework.web.authorization.setting;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.hswebframework.web.dict.Dict;
import org.hswebframework.web.dict.EnumDict;

/**
* @author zhouhao
* @since 3.0.4
*/
@AllArgsConstructor
@Getter
@Dict(id = "user-setting-permission")
public enum UserSettingPermission implements EnumDict<String> {
NONE("无"),
R("读"),
W("写"),
RW("读写");
private String text;

@Override
public String getValue() {
return name();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.hswebframework.web.authorization.token.DefaultUserTokenManager;
import org.hswebframework.web.authorization.token.UserTokenAuthenticationSupplier;
import org.hswebframework.web.authorization.token.UserTokenManager;
import org.hswebframework.web.authorization.twofactor.TwoFactorValidatorManager;
import org.hswebframework.web.authorization.twofactor.defaults.DefaultTwoFactorValidatorManager;
import org.hswebframework.web.convert.CustomMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
Expand Down Expand Up @@ -56,6 +58,13 @@ public SimpleDataAccessConfigBuilderFactory dataAccessConfigBuilderFactory() {
return factory;
}

@Bean
@ConditionalOnMissingBean(TwoFactorValidatorManager.class)
@ConfigurationProperties("hsweb.authorize.two-factor")
public DefaultTwoFactorValidatorManager defaultTwoFactorValidatorManager() {
return new DefaultTwoFactorValidatorManager();
}

@Bean
@ConditionalOnMissingBean(AuthenticationBuilderFactory.class)
public AuthenticationBuilderFactory authenticationBuilderFactory(DataAccessConfigBuilderFactory dataAccessConfigBuilderFactory) {
Expand Down
Loading

0 comments on commit a82c50a

Please sign in to comment.