|
|
@@ -8,6 +8,9 @@ import org.springframework.context.ApplicationContext;
|
|
|
import org.springframework.core.annotation.AnnotationUtils;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
import org.springframework.util.ClassUtils;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
import java.util.*;
|
|
|
@@ -15,36 +18,13 @@ import java.util.stream.Collectors;
|
|
|
|
|
|
public class PermissionUtil {
|
|
|
|
|
|
- public static Map<RequiresPermissions, RequiresPermissionsDesc> findPermissions(ApplicationContext context, String basicPackage) {
|
|
|
- Map<String, Object> map = context.getBeansWithAnnotation(Controller.class);
|
|
|
- Map<RequiresPermissions, RequiresPermissionsDesc> permissions = new HashMap<>();
|
|
|
- for(Map.Entry<String, Object> entry : map.entrySet()){
|
|
|
- Object bean = entry.getValue();
|
|
|
- if(!StringUtils.contains(ClassUtils.getPackageName(bean.getClass()), basicPackage)){
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- Class<?> clz = bean.getClass();
|
|
|
- Class controllerClz = clz.getSuperclass();
|
|
|
- List<Method> methods = MethodUtils.getMethodsListWithAnnotation(controllerClz, RequiresPermissions.class);
|
|
|
- for(Method method : methods){
|
|
|
- RequiresPermissions requiresPermissions = AnnotationUtils.getAnnotation(method, RequiresPermissions.class);
|
|
|
- RequiresPermissionsDesc requiresPermissionsDesc = AnnotationUtils.getAnnotation(method, RequiresPermissionsDesc.class);
|
|
|
- if(requiresPermissions == null || requiresPermissionsDesc == null){
|
|
|
- continue;
|
|
|
- }
|
|
|
- permissions.put(requiresPermissions, requiresPermissionsDesc);
|
|
|
- }
|
|
|
- }
|
|
|
- return permissions;
|
|
|
- }
|
|
|
-
|
|
|
public static List<PermVo> listPermissions(ApplicationContext context, String basicPackage) {
|
|
|
List<PermVo> root = new ArrayList<>();
|
|
|
- Map<RequiresPermissions, RequiresPermissionsDesc> map = findPermissions(context, basicPackage);
|
|
|
- for(Map.Entry<RequiresPermissions, RequiresPermissionsDesc> entry : map.entrySet()) {
|
|
|
- RequiresPermissions requiresPermissions = entry.getKey();
|
|
|
- RequiresPermissionsDesc requiresPermissionsDesc = entry.getValue();
|
|
|
+ List<Permission> permissions = findPermissions(context, basicPackage);
|
|
|
+ for(Permission permission : permissions) {
|
|
|
+ RequiresPermissions requiresPermissions = permission.getRequiresPermissions();
|
|
|
+ RequiresPermissionsDesc requiresPermissionsDesc = permission.getRequiresPermissionsDesc();
|
|
|
+ String api = permission.getApi();
|
|
|
|
|
|
String[] menus = requiresPermissionsDesc.menu();
|
|
|
if(menus.length != 2){
|
|
|
@@ -84,9 +64,66 @@ public class PermissionUtil {
|
|
|
PermVo leftPerm = new PermVo();
|
|
|
leftPerm.setId(requiresPermissions.value()[0]);
|
|
|
leftPerm.setLabel(requiresPermissionsDesc.button());
|
|
|
+ leftPerm.setApi(api);
|
|
|
|
|
|
perm2.getChildren().add(leftPerm);
|
|
|
}
|
|
|
return root;
|
|
|
}
|
|
|
+
|
|
|
+ public static List<Permission> findPermissions(ApplicationContext context, String basicPackage) {
|
|
|
+ Map<String, Object> map = context.getBeansWithAnnotation(Controller.class);
|
|
|
+ List<Permission> permissions = new ArrayList<>();
|
|
|
+ for(Map.Entry<String, Object> entry : map.entrySet()){
|
|
|
+ Object bean = entry.getValue();
|
|
|
+ if(!StringUtils.contains(ClassUtils.getPackageName(bean.getClass()), basicPackage)){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ Class<?> clz = bean.getClass();
|
|
|
+ Class controllerClz = clz.getSuperclass();
|
|
|
+ RequestMapping clazzRequestMapping = AnnotationUtils.findAnnotation(controllerClz, RequestMapping.class);
|
|
|
+ List<Method> methods = MethodUtils.getMethodsListWithAnnotation(controllerClz, RequiresPermissions.class);
|
|
|
+ for(Method method : methods){
|
|
|
+ RequiresPermissions requiresPermissions = AnnotationUtils.getAnnotation(method, RequiresPermissions.class);
|
|
|
+ RequiresPermissionsDesc requiresPermissionsDesc = AnnotationUtils.getAnnotation(method, RequiresPermissionsDesc.class);
|
|
|
+
|
|
|
+ if(requiresPermissions == null || requiresPermissionsDesc == null){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ String api = "";
|
|
|
+ if(clazzRequestMapping != null){
|
|
|
+ api = clazzRequestMapping.value()[0];
|
|
|
+ }
|
|
|
+
|
|
|
+ PostMapping postMapping = AnnotationUtils.getAnnotation(method, PostMapping.class);
|
|
|
+ if(postMapping != null){
|
|
|
+ api = "POST " + api + postMapping.value()[0];
|
|
|
+
|
|
|
+ Permission permission = new Permission();
|
|
|
+ permission.setRequiresPermissions(requiresPermissions);
|
|
|
+ permission.setRequiresPermissionsDesc(requiresPermissionsDesc);
|
|
|
+ permission.setApi(api);
|
|
|
+ permissions.add(permission);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ GetMapping getMapping = AnnotationUtils.getAnnotation(method, GetMapping.class);
|
|
|
+ if(getMapping != null){
|
|
|
+ api = "GET " + api + getMapping.value()[0];
|
|
|
+ Permission permission = new Permission();
|
|
|
+ permission.setRequiresPermissions(requiresPermissions);
|
|
|
+ permission.setRequiresPermissionsDesc(requiresPermissionsDesc);
|
|
|
+ permission.setApi(api);
|
|
|
+ permissions.add(permission);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // TODO
|
|
|
+ // 这里只支持GetMapping注解或者PostMapping注解,应该进一步提供灵活性
|
|
|
+ throw new RuntimeException("目前权限管理应该在method的前面使用GetMapping注解或者PostMapping注解");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return permissions;
|
|
|
+ }
|
|
|
+
|
|
|
}
|