-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from hs-web/3.0.x
3.0.4
- Loading branch information
Showing
222 changed files
with
2,779 additions
and
273 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...rization-api/src/main/java/org/hswebframework/web/authorization/annotation/TwoFactor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
.../src/main/java/org/hswebframework/web/authorization/exception/NeedTwoFactorException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
...pi/src/main/java/org/hswebframework/web/authorization/setting/SettingNullValueHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...on-api/src/main/java/org/hswebframework/web/authorization/setting/SettingValueHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} |
98 changes: 98 additions & 0 deletions
98
...src/main/java/org/hswebframework/web/authorization/setting/StringSourceSettingHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...on-api/src/main/java/org/hswebframework/web/authorization/setting/UserSettingManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
...api/src/main/java/org/hswebframework/web/authorization/setting/UserSettingPermission.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.