/
MsPermissionAnnotationMethodInterceptor.java
84 lines (75 loc) · 3.25 KB
/
MsPermissionAnnotationMethodInterceptor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package io.metersphere.security;
import io.metersphere.base.domain.Project;
import io.metersphere.commons.utils.SessionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.commons.lang3.reflect.MethodUtils;
import org.apache.shiro.aop.AnnotationResolver;
import org.apache.shiro.aop.MethodInvocation;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.aop.PermissionAnnotationMethodInterceptor;
import java.lang.reflect.Field;
import java.lang.reflect.Parameter;
public class MsPermissionAnnotationMethodInterceptor extends PermissionAnnotationMethodInterceptor {
public MsPermissionAnnotationMethodInterceptor(AnnotationResolver resolver) {
super(resolver);
}
@Override
public void assertAuthorized(MethodInvocation mi) throws AuthorizationException {
String projectId = null;
String workspaceId = null;
Object[] arguments = mi.getArguments();
if (ArrayUtils.isNotEmpty(arguments)) {
Parameter[] parameters = mi.getMethod().getParameters();
for (int i = 0; i < arguments.length; i++) {
Object argument = arguments[i];
if (argument instanceof String) {
if (StringUtils.equals(parameters[i].getName(), "projectId")) {
projectId = (String) argument;
}
if (StringUtils.equals(parameters[i].getName(), "workspaceId")) {
workspaceId = (String) argument;
}
} else {
try {
if (StringUtils.isEmpty(projectId) && isExistField(argument, "projectId")) {
projectId = (String) MethodUtils.invokeMethod(argument, "getProjectId");
}
if (StringUtils.equals(parameters[i].getName(), "project") && argument instanceof Project) {
projectId = ((Project) argument).getId();
}
if (StringUtils.isEmpty(workspaceId) && isExistField(argument, "workspaceId")) {
workspaceId = (String) MethodUtils.invokeMethod(argument, "getWorkspaceId");
}
} catch (Exception e) {
}
}
}
}
try {
SessionUtils.setCurrentWorkspaceId(workspaceId);
SessionUtils.setCurrentProjectId(projectId);
super.assertAuthorized(mi);
} finally {
SessionUtils.clearCurrentWorkspaceId();
SessionUtils.clearCurrentProjectId();
}
}
public static boolean isExistField(Object obj, String fieldName) {
if (obj == null || StringUtils.isEmpty(fieldName)) {
return false;
}
//获取这个类的所有属性
Field[] fields = FieldUtils.getAllFields(obj.getClass());
boolean flag = false;
//循环遍历所有的fields
for (Field field : fields) {
if (field.getName().equals(fieldName)) {
flag = true;
break;
}
}
return flag;
}
}