@SaCheckPermission 增加 orRole 字段,用于权限角色“双重or”匹配

This commit is contained in:
click33 2021-10-02 18:26:13 +08:00
parent b409df78b3
commit 977ab3ed40
10 changed files with 74 additions and 39 deletions

View File

@ -33,4 +33,19 @@ public @interface SaCheckPermission {
*/ */
String type() default ""; String type() default "";
/**
* 在权限认证不通过时的次要选择两者只要其一认证成功即可通过校验
*
* <p>
* 例1@SaCheckPermission(value="user-add", orRole="admin")
* 代表本次请求只要具有 user-add权限 admin角色 其一即可通过校验
* </p>
*
* <p>
* 例2 orRole={"admin", "manager", "staff"}具有三个角色其一即可 <br>
* 例3 orRole={"admin, manager, staff"}必须三个角色同时具备
* </p>
*/
String[] orRole() default {};
} }

View File

@ -74,7 +74,7 @@ public class SaTokenConfig implements Serializable {
/** /**
* jwt秘钥 (只有集成 sa-token-temp-jwt 模块时此参数才会生效) * jwt秘钥 (只有集成 sa-token-temp-jwt 模块时此参数才会生效)
*/ */
private String jwtSecretkey; private String jwtSecretKey;
/** /**
* Id-Token的有效期 (单位: ) * Id-Token的有效期 (单位: )
@ -391,16 +391,16 @@ public class SaTokenConfig implements Serializable {
/** /**
* @return jwt秘钥 (只有集成 sa-token-temp-jwt 模块时此参数才会生效) * @return jwt秘钥 (只有集成 sa-token-temp-jwt 模块时此参数才会生效)
*/ */
public String getJwtSecretkey() { public String getJwtSecretKey() {
return jwtSecretkey; return jwtSecretKey;
} }
/** /**
* @param jwtSecretkey jwt秘钥 (只有集成 sa-token-temp-jwt 模块时此参数才会生效) * @param jwtSecretKey jwt秘钥 (只有集成 sa-token-temp-jwt 模块时此参数才会生效)
* @return 对象自身 * @return 对象自身
*/ */
public SaTokenConfig setJwtSecretkey(String jwtSecretkey) { public SaTokenConfig setJwtSecretKey(String jwtSecretKey) {
this.jwtSecretkey = jwtSecretkey; this.jwtSecretKey = jwtSecretKey;
return this; return this;
} }
@ -476,7 +476,7 @@ public class SaTokenConfig implements Serializable {
+ ", tokenStyle=" + tokenStyle + ", tokenStyle=" + tokenStyle
+ ", dataRefreshPeriod=" + dataRefreshPeriod + ", tokenSessionCheckLogin=" + tokenSessionCheckLogin + ", dataRefreshPeriod=" + dataRefreshPeriod + ", tokenSessionCheckLogin=" + tokenSessionCheckLogin
+ ", autoRenew=" + autoRenew + ", cookieDomain=" + cookieDomain + ", tokenPrefix=" + tokenPrefix + ", autoRenew=" + autoRenew + ", cookieDomain=" + cookieDomain + ", tokenPrefix=" + tokenPrefix
+ ", isPrint=" + isPrint + ", isLog=" + isLog + ", jwtSecretkey=" + jwtSecretkey + ", idTokenTimeout=" + ", isPrint=" + isPrint + ", isLog=" + isLog + ", jwtSecretKey=" + jwtSecretKey + ", idTokenTimeout="
+ idTokenTimeout + ", basic=" + basic + ", currDomain=" + currDomain + ", sso=" + sso + "]"; + idTokenTimeout + ", basic=" + basic + ", currDomain=" + currDomain + ", sso=" + sso + "]";
} }
@ -497,25 +497,10 @@ public class SaTokenConfig implements Serializable {
* @param isV see note * @param isV see note
* @return see note * @return see note
*/ */
@Deprecated
public SaTokenConfig setIsV(Boolean isV) { public SaTokenConfig setIsV(Boolean isV) {
this.isPrint = isV; this.isPrint = isV;
return this; return this;
} }
/**
* @return <h1> 本函数设计已过时未来版本可能移除此函数请及时更换为 getJwtSecretkey() 使用方式保持不变 </h1>
*/
public String getJwtSecretKey() {
return jwtSecretkey;
}
/**
* @param <h1> 本函数设计已过时未来版本可能移除此函数请及时更换为 setJwtSecretkey() 使用方式保持不变 </h1>
* @return 对象自身
*/
public SaTokenConfig setJwtSecretKey(String jwtSecretKey) {
this.jwtSecretkey = jwtSecretKey;
return this;
}
} }

View File

@ -409,7 +409,7 @@ public class SaSsoTemplate {
* @author kong * @author kong
*/ */
@FunctionalInterface @FunctionalInterface
static interface CallSloUrlFunction{ public static interface CallSloUrlFunction{
/** /**
* 调用function * 调用function
* @param url 注销回调URL * @param url 注销回调URL

View File

@ -1313,10 +1313,24 @@ public class StpLogic {
*/ */
public void checkByAnnotation(SaCheckPermission at) { public void checkByAnnotation(SaCheckPermission at) {
String[] permissionArray = at.value(); String[] permissionArray = at.value();
if(at.mode() == SaMode.AND) { try {
this.checkPermissionAnd(permissionArray); if(at.mode() == SaMode.AND) {
} else { this.checkPermissionAnd(permissionArray);
this.checkPermissionOr(permissionArray); } else {
this.checkPermissionOr(permissionArray);
}
} catch (NotPermissionException e) {
// 权限认证未通过再开始角色认证
if(at.orRole().length > 0) {
for (String role : at.orRole()) {
String[] rArr = SaFoxUtil.convertStringToArray(role);
// 某一项role认证通过则可以提前退出了代表通过
if(hasRoleAnd(rArr)) {
return;
}
}
}
throw e;
} }
} }

View File

@ -812,6 +812,7 @@ public class StpUtil {
* <p> 当对方再次访问系统时会抛出NotLoginException异常场景值=-2 * <p> 当对方再次访问系统时会抛出NotLoginException异常场景值=-2
* @param loginId 账号id * @param loginId 账号id
*/ */
@Deprecated
public static void logoutByLoginId(Object loginId) { public static void logoutByLoginId(Object loginId) {
stpLogic.kickout(loginId); stpLogic.kickout(loginId);
} }
@ -824,6 +825,7 @@ public class StpUtil {
* @param loginId 账号id * @param loginId 账号id
* @param device 设备标识 (填null代表所有注销设备) * @param device 设备标识 (填null代表所有注销设备)
*/ */
@Deprecated
public static void logoutByLoginId(Object loginId, String device) { public static void logoutByLoginId(Object loginId, String device) {
stpLogic.kickout(loginId, device); stpLogic.kickout(loginId, device);
} }

View File

@ -83,7 +83,7 @@ public interface SaTempInterface {
/** /**
* @return jwt秘钥 (只有集成 sa-token-temp-jwt 模块时此参数才会生效) * @return jwt秘钥 (只有集成 sa-token-temp-jwt 模块时此参数才会生效)
*/ */
public default String getJwtSecretkey() { public default String getJwtSecretKey() {
return null; return null;
} }

View File

@ -404,6 +404,26 @@ public class SaFoxUtil {
return str; return str;
} }
/**
* String Array按照逗号切割
* @param str 字符串
* @return 数组
*/
public static String[] convertStringToArray(String str) {
List<String> list = convertStringToList(str);
return list.toArray(new String[list.size()]);
}
/**
* Array String按照逗号切割
* @param arr 数组
* @return 字符串
*/
public static String convertArrayToString(String[] arr) {
if(arr == null || arr.length == 0) {
return "";
}
return String.join(",", arr);
}
} }

View File

@ -239,9 +239,8 @@ public class TestController {
// 测试 浏览器访问 http://localhost:8081/test/test // 测试 浏览器访问 http://localhost:8081/test/test
@RequestMapping("test") @RequestMapping("test")
public AjaxJson test() { public AjaxJson test() {
System.out.println("进来了"); System.out.println("进来了");
StpUtil.checkLogin(); return AjaxJson.getSuccess();
return AjaxJson.getSuccess();
} }
// 测试 浏览器访问 http://localhost:8081/test/test2 // 测试 浏览器访问 http://localhost:8081/test/test2

View File

@ -71,5 +71,5 @@ SaTempUtil.deleteToken(token);
``` java ``` java
sa-token: sa-token:
# sa-token-temp-jwt 模块的秘钥 (随便乱摁几个字母就行了) # sa-token-temp-jwt 模块的秘钥 (随便乱摁几个字母就行了)
jwt-secretkey: JfdDSgfCmPsDfmsAaQwnXk jwt-secret-key: JfdDSgfCmPsDfmsAaQwnXk
``` ```

View File

@ -17,7 +17,7 @@ public class SaTempForJwt implements SaTempInterface {
*/ */
@Override @Override
public String createToken(Object value, long timeout) { public String createToken(Object value, long timeout) {
String token = SaJwtUtil.createToken(value, timeout, getJwtSecretkey()); String token = SaJwtUtil.createToken(value, timeout, getJwtSecretKey());
return token; return token;
} }
@ -26,7 +26,7 @@ public class SaTempForJwt implements SaTempInterface {
*/ */
@Override @Override
public Object parseToken(String token) { public Object parseToken(String token) {
Object value = SaJwtUtil.getValue(token, getJwtSecretkey()); Object value = SaJwtUtil.getValue(token, getJwtSecretKey());
return value; return value;
} }
@ -35,7 +35,7 @@ public class SaTempForJwt implements SaTempInterface {
*/ */
@Override @Override
public long getTimeout(String token) { public long getTimeout(String token) {
long timeout = SaJwtUtil.getTimeout(token, getJwtSecretkey()); long timeout = SaJwtUtil.getTimeout(token, getJwtSecretKey());
return timeout; return timeout;
} }
@ -52,7 +52,7 @@ public class SaTempForJwt implements SaTempInterface {
* @return jwt秘钥 * @return jwt秘钥
*/ */
@Override @Override
public String getJwtSecretkey() { public String getJwtSecretKey() {
String jwtSecretKey = SaManager.getConfig().getJwtSecretKey(); String jwtSecretKey = SaManager.getConfig().getJwtSecretKey();
if(SaFoxUtil.isEmpty(jwtSecretKey)) { if(SaFoxUtil.isEmpty(jwtSecretKey)) {
throw new SaTokenException("请配置jwtSecretKey"); throw new SaTokenException("请配置jwtSecretKey");