Skip to content

Commit 76dd333

Browse files
click33gitee-org
authored andcommitted
!41 注解定制loginKey
Merge pull request !41 from AppleOfGray/dev
2 parents e6e53d4 + 9c1ff87 commit 76dd333

File tree

8 files changed

+174
-70
lines changed

8 files changed

+174
-70
lines changed

sa-token-core/src/main/java/cn/dev33/satoken/annotation/SaCheckLogin.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package cn.dev33.satoken.annotation;
22

3+
import cn.dev33.satoken.stp.StpUtil;
4+
35
import java.lang.annotation.ElementType;
46
import java.lang.annotation.Retention;
57
import java.lang.annotation.RetentionPolicy;
@@ -15,4 +17,12 @@
1517
@Target({ ElementType.METHOD, ElementType.TYPE })
1618
public @interface SaCheckLogin {
1719

20+
/**
21+
* 多账号体系下使用哪个体系检测登录
22+
* 每个StpUtil都有一个stpLogic属性
23+
* 初始化StpLogic时, 指定的LoginKey字符串复制到这里
24+
* @return LoginKey字符串
25+
*/
26+
String key() default "login";
27+
1828
}

sa-token-core/src/main/java/cn/dev33/satoken/annotation/SaCheckPermission.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,13 @@
2626
* @return 验证模式
2727
*/
2828
SaMode mode() default SaMode.AND;
29-
29+
30+
/**
31+
* 多账号体系下使用哪个体系检测权限
32+
* 每个StpUtil都有一个stpLogic属性
33+
* 初始化StpLogic时, 指定的LoginKey字符串复制到这里
34+
* @return LoginKey字符串
35+
*/
36+
String key() default "login";
37+
3038
}

sa-token-core/src/main/java/cn/dev33/satoken/annotation/SaCheckRole.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,13 @@
2626
* @return 验证模式
2727
*/
2828
SaMode mode() default SaMode.AND;
29+
30+
/**
31+
* 多账号体系下使用哪个体系检测角色
32+
* 每个StpUtil都有一个stpLogic属性
33+
* 初始化StpLogic时, 指定的LoginKey字符串复制到这里
34+
* @return LoginKey字符串
35+
*/
36+
String key() default "login";
2937

3038
}

sa-token-core/src/main/java/cn/dev33/satoken/exception/NotPermissionException.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package cn.dev33.satoken.exception;
22

3-
import cn.dev33.satoken.stp.StpUtil;
4-
53
/**
64
* 没有指定权限码,抛出的异常
75
*
@@ -13,7 +11,7 @@ public class NotPermissionException extends SaTokenException {
1311
/**
1412
* 序列化版本号
1513
*/
16-
private static final long serialVersionUID = 6806129545290130142L;
14+
private static final long serialVersionUID = 6806129545290130141L;
1715

1816
/** 权限码 */
1917
private String code;
@@ -39,10 +37,6 @@ public String getLoginKey() {
3937
return loginKey;
4038
}
4139

42-
public NotPermissionException(String code) {
43-
this(code, StpUtil.stpLogic.loginKey);
44-
}
45-
4640
public NotPermissionException(String code, String loginKey) {
4741
super("无此权限:" + code);
4842
this.code = code;

sa-token-core/src/main/java/cn/dev33/satoken/exception/NotRoleException.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package cn.dev33.satoken.exception;
22

3-
import cn.dev33.satoken.stp.StpUtil;
4-
53
/**
64
* 没有指定角色标识,抛出的异常
75
*
@@ -39,10 +37,6 @@ public String getLoginKey() {
3937
return loginKey;
4038
}
4139

42-
public NotRoleException(String role) {
43-
this(role, StpUtil.stpLogic.loginKey);
44-
}
45-
4640
public NotRoleException(String role, String loginKey) {
4741
// 这里到底要不要拼接上loginKey呢?纠结
4842
super("无此角色:" + role);

sa-token-core/src/main/java/cn/dev33/satoken/stp/StpLogic.java

Lines changed: 21 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,65 +1217,32 @@ public SaTokenConfig getConfig() {
12171217

12181218

12191219
// =================== 其它方法 ===================
1220-
1220+
12211221
/**
1222-
* 对一个Method对象进行注解检查(注解鉴权内部实现)
1223-
* @param method Method对象
1222+
* 检查当前登录体系是否拥有给定角色
1223+
* @param roleArray 角色字符串数组
1224+
* @param saMode SaMode.AND, SaMode.OR
12241225
*/
1225-
public void checkMethodAnnotation(Method method) {
1226-
1227-
// ----------- 验证登录
1228-
if(method.isAnnotationPresent(SaCheckLogin.class) || method.getDeclaringClass().isAnnotationPresent(SaCheckLogin.class)) {
1229-
this.checkLogin();
1230-
}
1231-
1232-
// ----------- 验证角色
1233-
// 验证方法上的
1234-
SaCheckRole scr = method.getAnnotation(SaCheckRole.class);
1235-
if(scr != null) {
1236-
String[] roleArray = scr.value();
1237-
if(scr.mode() == SaMode.AND) {
1238-
this.checkRoleAnd(roleArray);
1239-
} else {
1240-
this.checkRoleOr(roleArray);
1241-
}
1242-
}
1243-
// 验证类上的
1244-
scr = method.getDeclaringClass().getAnnotation(SaCheckRole.class);
1245-
if(scr != null) {
1246-
String[] roleArray = scr.value();
1247-
if(scr.mode() == SaMode.AND) {
1248-
this.checkRoleAnd(roleArray);
1249-
} else {
1250-
this.checkRoleOr(roleArray);
1251-
}
1252-
}
1253-
1254-
// ----------- 验证权限
1255-
// 验证方法上的
1256-
SaCheckPermission scp = method.getAnnotation(SaCheckPermission.class);
1257-
if(scp != null) {
1258-
String[] permissionArray = scp.value();
1259-
if(scp.mode() == SaMode.AND) {
1260-
this.checkPermissionAnd(permissionArray);
1261-
} else {
1262-
this.checkPermissionOr(permissionArray);
1263-
}
1264-
}
1265-
// 验证类上的
1266-
scp = method.getDeclaringClass().getAnnotation(SaCheckPermission.class);
1267-
if(scp != null) {
1268-
String[] permissionArray = scp.value();
1269-
if(scp.mode() == SaMode.AND) {
1270-
this.checkPermissionAnd(permissionArray);
1271-
} else {
1272-
this.checkPermissionOr(permissionArray);
1273-
}
1226+
public void checkHasRoles(String[] roleArray, SaMode saMode) {
1227+
if(saMode == SaMode.AND) {
1228+
this.checkRoleAnd(roleArray);
1229+
} else {
1230+
this.checkRoleOr(roleArray);
12741231
}
1275-
1276-
// 验证通过
12771232
}
12781233

1234+
/**
1235+
* 检查当前登录体系是否拥有给定权限
1236+
* @param permissionArray 权限字符串数组
1237+
* @param saMode SaMode.AND, SaMode.OR
1238+
*/
1239+
public void checkHasPermissions(String[] permissionArray, SaMode saMode) {
1240+
if(saMode == SaMode.AND) {
1241+
this.checkPermissionAnd(permissionArray);
1242+
} else {
1243+
this.checkPermissionOr(permissionArray);
1244+
}
1245+
}
12791246

12801247
// =================== 身份切换 ===================
12811248

sa-token-spring-aop/src/main/java/cn/dev33/satoken/aop/SaCheckAspect.java

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package cn.dev33.satoken.aop;
22

3+
import cn.dev33.satoken.SaManager;
4+
import cn.dev33.satoken.annotation.SaCheckLogin;
5+
import cn.dev33.satoken.annotation.SaCheckPermission;
6+
import cn.dev33.satoken.annotation.SaCheckRole;
7+
import cn.dev33.satoken.exception.NotLoginException;
38
import org.aspectj.lang.ProceedingJoinPoint;
49
import org.aspectj.lang.annotation.Around;
510
import org.aspectj.lang.annotation.Aspect;
@@ -12,6 +17,9 @@
1217
import cn.dev33.satoken.stp.StpUtil;
1318
import cn.dev33.satoken.util.SaTokenConsts;
1419

20+
import java.lang.reflect.Method;
21+
import java.util.Map;
22+
1523
/**
1624
* sa-token 基于 Spring Aop 的注解鉴权
1725
*
@@ -58,9 +66,66 @@ public void pointcut() {
5866
*/
5967
@Around("pointcut()")
6068
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
69+
6170
// 注解鉴权
6271
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
63-
getStpLogic().checkMethodAnnotation(signature.getMethod());
72+
Method method = signature.getMethod();
73+
Class<?> cutClass = method.getDeclaringClass();
74+
Map<String, StpLogic> stpLogicMap = SaManager.stpLogicMap;
75+
76+
// ----------- 验证登录
77+
SaCheckLogin checkLogin = null;
78+
if(method.isAnnotationPresent(SaCheckLogin.class)) { // 方法注解的优先级高于类注解
79+
checkLogin = method.getAnnotation(SaCheckLogin.class);
80+
} else if(cutClass.isAnnotationPresent(SaCheckLogin.class)) {
81+
checkLogin = cutClass.getAnnotation(SaCheckLogin.class);
82+
}
83+
if (checkLogin != null) {
84+
String loginKey = checkLogin.key();
85+
if (stpLogicMap.containsKey(loginKey)) {
86+
StpLogic stpLogic = stpLogicMap.get(loginKey);
87+
stpLogic.checkLogin();
88+
} else {
89+
// StpUserUtil里面的StpLogic对象只有调用至少一次才会初始化,如果没有初始化SaManager.stpLogicMap里面是没有loginKey的
90+
// 还有一种可能是使用者写错了loginKey,这两种方式都会导致SaManager.stpLogicMap查不到loginKey
91+
throw NotLoginException.newInstance(loginKey, NotLoginException.DEFAULT_MESSAGE);
92+
}
93+
}
94+
95+
// ----------- 验证角色
96+
SaCheckRole saCheckRole = null;
97+
if (method.isAnnotationPresent(SaCheckRole.class)) { // 方法注解的优先级高于类注解
98+
saCheckRole = method.getAnnotation(SaCheckRole.class);
99+
} else if (cutClass.isAnnotationPresent(SaCheckRole.class)) {
100+
saCheckRole = cutClass.getAnnotation(SaCheckRole.class);
101+
}
102+
if (saCheckRole != null) {
103+
String loginKey = saCheckRole.key();
104+
if (stpLogicMap.containsKey(loginKey)) {
105+
StpLogic stpLogic = stpLogicMap.get(loginKey);
106+
stpLogic.checkHasRoles(saCheckRole.value(), saCheckRole.mode());
107+
} else {
108+
throw NotLoginException.newInstance(loginKey, NotLoginException.DEFAULT_MESSAGE);
109+
}
110+
}
111+
112+
// ----------- 验证权限
113+
SaCheckPermission saCheckPermission = null;
114+
if (method.isAnnotationPresent(SaCheckPermission.class)) { // 方法注解的优先级高于类注解
115+
saCheckPermission = method.getAnnotation(SaCheckPermission.class);
116+
} else if (cutClass.isAnnotationPresent(SaCheckPermission.class)){
117+
saCheckPermission = cutClass.getAnnotation(SaCheckPermission.class);
118+
}
119+
if (saCheckPermission != null) {
120+
String loginKey = saCheckPermission.key();
121+
if (stpLogicMap.containsKey(loginKey)) {
122+
StpLogic stpLogic = stpLogicMap.get(loginKey);
123+
stpLogic.checkHasPermissions(saCheckPermission.value(), saCheckPermission.mode());
124+
} else {
125+
throw NotLoginException.newInstance(loginKey, NotLoginException.DEFAULT_MESSAGE);
126+
}
127+
}
128+
64129
try {
65130
// 执行原有逻辑
66131
Object obj = joinPoint.proceed();

sa-token-spring-boot-starter/src/main/java/cn/dev33/satoken/interceptor/SaAnnotationInterceptor.java

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package cn.dev33.satoken.interceptor;
22

33
import java.lang.reflect.Method;
4+
import java.util.Map;
45

56
import javax.servlet.http.HttpServletRequest;
67
import javax.servlet.http.HttpServletResponse;
78

9+
import cn.dev33.satoken.SaManager;
10+
import cn.dev33.satoken.annotation.SaCheckLogin;
11+
import cn.dev33.satoken.annotation.SaCheckPermission;
12+
import cn.dev33.satoken.annotation.SaCheckRole;
13+
import cn.dev33.satoken.exception.NotLoginException;
814
import org.springframework.web.method.HandlerMethod;
915
import org.springframework.web.servlet.HandlerInterceptor;
1016

@@ -62,7 +68,59 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
6268
Method method = ((HandlerMethod) handler).getMethod();
6369

6470
// 进行验证
65-
getStpLogic().checkMethodAnnotation(method);
71+
Class<?> cutClass = method.getDeclaringClass();
72+
Map<String, StpLogic> stpLogicMap = SaManager.stpLogicMap;
73+
74+
// ----------- 验证登录
75+
SaCheckLogin checkLogin = null;
76+
if(method.isAnnotationPresent(SaCheckLogin.class)) { // 方法注解的优先级高于类注解
77+
checkLogin = method.getAnnotation(SaCheckLogin.class);
78+
} else if(cutClass.isAnnotationPresent(SaCheckLogin.class)) {
79+
checkLogin = cutClass.getAnnotation(SaCheckLogin.class);
80+
}
81+
if (checkLogin != null) {
82+
String loginKey = checkLogin.key();
83+
if (stpLogicMap.containsKey(loginKey)) {
84+
StpLogic stpLogic = stpLogicMap.get(loginKey);
85+
stpLogic.checkLogin();
86+
} else {
87+
throw NotLoginException.newInstance(loginKey, NotLoginException.DEFAULT_MESSAGE);
88+
}
89+
}
90+
91+
// ----------- 验证角色
92+
SaCheckRole saCheckRole = null;
93+
if (method.isAnnotationPresent(SaCheckRole.class)) { // 方法注解的优先级高于类注解
94+
saCheckRole = method.getAnnotation(SaCheckRole.class);
95+
} else if (cutClass.isAnnotationPresent(SaCheckRole.class)) {
96+
saCheckRole = cutClass.getAnnotation(SaCheckRole.class);
97+
}
98+
if (saCheckRole != null) {
99+
String loginKey = saCheckRole.key();
100+
if (stpLogicMap.containsKey(loginKey)) {
101+
StpLogic stpLogic = stpLogicMap.get(loginKey);
102+
stpLogic.checkHasRoles(saCheckRole.value(), saCheckRole.mode());
103+
} else {
104+
throw NotLoginException.newInstance(loginKey, NotLoginException.DEFAULT_MESSAGE);
105+
}
106+
}
107+
108+
// ----------- 验证权限
109+
SaCheckPermission saCheckPermission = null;
110+
if (method.isAnnotationPresent(SaCheckPermission.class)) { // 方法注解的优先级高于类注解
111+
saCheckPermission = method.getAnnotation(SaCheckPermission.class);
112+
} else if (cutClass.isAnnotationPresent(SaCheckPermission.class)){
113+
saCheckPermission = cutClass.getAnnotation(SaCheckPermission.class);
114+
}
115+
if (saCheckPermission != null) {
116+
String loginKey = saCheckPermission.key();
117+
if (stpLogicMap.containsKey(loginKey)) {
118+
StpLogic stpLogic = stpLogicMap.get(loginKey);
119+
stpLogic.checkHasPermissions(saCheckPermission.value(), saCheckPermission.mode());
120+
} else {
121+
throw NotLoginException.newInstance(loginKey, NotLoginException.DEFAULT_MESSAGE);
122+
}
123+
}
66124

67125
// 通过验证
68126
return true;

0 commit comments

Comments
 (0)