Commit 56287fc9 authored by 赵啸非's avatar 赵啸非

添加新的菜单权限模块

parent 59d6541f
Pipeline #2946 failed with stages
package com.mortals.xhx.common.code;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* 方法name path枚举类
*
* @author zxfei
*/
public enum MethodNameEnum {
list("list", "列表"),
delete("delete", "删除"),
logicDelete("logicDelete", "逻辑删除"),
saveOrUpdate("save", "保存更新"),
batchSave("batchSave", "批量保存更新"),
info("info", "详细"),
exportExcel("exportExcel", "导出excel"),
importData("importData", "导入excel"),
downloadTemplate("downloadTemplate", "下载模板"),
view("view", "详情");
private String value;
private String desc;
MethodNameEnum(String value, String desc) {
this.value = value;
this.desc = desc;
}
public String getValue() {
return this.value;
}
public String getDesc() {
return this.desc;
}
public static MethodNameEnum getByValue(String value) {
for (MethodNameEnum wySigninEnum : MethodNameEnum.values()) {
if (wySigninEnum.getValue() == value) {
return wySigninEnum;
}
}
return null;
}
/**
* 获取Map集合
*
* @param eItem 不包含项
* @return
*/
public static Map<String, String> getEnumMap(String... eItem) {
Map<String, String> resultMap = new LinkedHashMap<>();
for (MethodNameEnum item : MethodNameEnum.values()) {
try {
boolean hasE = false;
for (String e : eItem) {
if (item.getValue() == e) {
hasE = true;
break;
}
}
if (!hasE) {
resultMap.put(item.getValue() + "", item.getDesc());
}
} catch (Exception ex) {
}
}
return resultMap;
}
}
\ No newline at end of file
...@@ -4,6 +4,9 @@ import cn.hutool.core.util.StrUtil; ...@@ -4,6 +4,9 @@ import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.annotation.UnAuth; import com.mortals.framework.annotation.UnAuth;
import com.mortals.xhx.base.system.resource.model.ResourceEntity; import com.mortals.xhx.base.system.resource.model.ResourceEntity;
import com.mortals.xhx.common.code.MethodNameEnum;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
...@@ -17,66 +20,46 @@ import java.lang.reflect.Method; ...@@ -17,66 +20,46 @@ import java.lang.reflect.Method;
import java.net.JarURLConnection; import java.net.JarURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.util.*; import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import java.util.stream.Collectors;
@Slf4j
public class ControllerScanUtil { public class ControllerScanUtil {
/**
* 查看类路径
*/
public static final String[] ULR_VIEW = {"view", "info", "list", "get", "find", "export", "download",
"index", "bill"};
public static final String[] WHITE_URL = {"login", "test","file","securitycode"}; public static final String[] WHITE_URL = {"login", "test", "file", "securitycode"};
public static void main(String[] args) { public static void main(String[] args) {
List<Class<?>> classList = getAllClassByPackageName("com.mortals.xhx"); List<Class<?>> classList = getAllClassByPackageName("com.mortals.xhx.base.system.menu");
//System.out.println(classList); //获取到了所有的类 //System.out.println(classList); //获取到了所有的类
getAnnotationInfo(classList); List<ResourceEntity> list = getAnnotationInfo(classList);
log.info(JSONObject.toJSONString(list));
} }
public static List<ResourceEntity> getAnnotationInfo(List<Class<?>> clsList) { public static List<ResourceEntity> getAnnotationInfo(List<Class<?>> clsList) {
List<ResourceEntity> resourceArrayList = new ArrayList<>(); List<ResourceEntity> resourceArrayList = new ArrayList<>();
if (clsList != null && clsList.size() > 0) { if (clsList != null && clsList.size() > 0) {
for (Class<?> cls : clsList) { for (Class<?> cls : clsList) {
boolean exits = cls.isAnnotationPresent(RestController.class); boolean exits = cls.isAnnotationPresent(RestController.class);
if (!exits) { if (!exits) {
continue; continue;
} }
//白名单地址的 也不需要 //获取controller类,
ResourceEntity resourceViewEntity = new ResourceEntity();
resourceViewEntity.initAttrValue();
ResourceEntity resourceEditEntity = new ResourceEntity();
resourceEditEntity.initAttrValue();
resourceEditEntity.setSourceType(1);
Method substringMethod = null; Method substringMethod = null;
String result = ""; String result = "";
try { try {
String packName = cls.getPackage().getName();
if (StrUtil.contains(packName, "system")) {
//系统管理-xx管理-
substringMethod = cls.getMethod("getModuleDesc");
result = (String) substringMethod.invoke(cls.newInstance());
resourceViewEntity.setName("系统管理-" + result + "管理-查看");
resourceEditEntity.setName("系统管理-" + result + "管理-维护");
} else {
substringMethod = cls.getMethod("getModuleDesc"); substringMethod = cls.getMethod("getModuleDesc");
result = (String) substringMethod.invoke(cls.newInstance()); if(!ObjectUtils.isEmpty(substringMethod)){
resourceViewEntity.setName(result + "-查看"); result = (String) substringMethod.invoke(cls.getDeclaredConstructor().newInstance());
resourceEditEntity.setName(result + "-维护");
} }
} catch (Exception e) { } catch (Exception e) {
log.error("反射获取controller类异常", e.getMessage());
} }
//获取基础路径
RequestMapping requestMappingCls = cls.getAnnotation(RequestMapping.class); RequestMapping requestMappingCls = cls.getAnnotation(RequestMapping.class);
String prefix = ""; String prefix = "";
if (requestMappingCls != null) { if (requestMappingCls != null) {
...@@ -97,105 +80,107 @@ public class ControllerScanUtil { ...@@ -97,105 +80,107 @@ public class ControllerScanUtil {
if (requestMappingCls != null) { if (requestMappingCls != null) {
result = StrUtil.removeSuffix(StrUtil.removePrefix(requestMappingCls.value()[0].toUpperCase(), "/"), "/") + "接口模块"; result = StrUtil.removeSuffix(StrUtil.removePrefix(requestMappingCls.value()[0].toUpperCase(), "/"), "/") + "接口模块";
} }
resourceViewEntity.setName(result + "-查看");
resourceEditEntity.setName(result + "-维护");
} }
Set<String> urlSet = new HashSet<>();
//获取类中的所有的方法 //获取类中的所有的方法 并生成resourceEntity
Method[] methods = cls.getDeclaredMethods(); Method[] methods = cls.getDeclaredMethods();
if (methods != null && methods.length > 0) { if (!ObjectUtils.isEmpty(methods)) {
for (Method method : methods) { for (Method method : methods) {
boolean unAuth = method.isAnnotationPresent(UnAuth.class); boolean unAuth = method.isAnnotationPresent(UnAuth.class);
if (unAuth) { if (unAuth) {
continue; continue;
} }
String name = "其它";
String url = "";
boolean mExits = method.isAnnotationPresent(RequestMapping.class); boolean mExits = method.isAnnotationPresent(RequestMapping.class);
if (mExits) { if (mExits) {
RequestMapping requestMapping = method.getAnnotation(RequestMapping.class); RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
String url = prefix + "/" + requestMapping.value()[0]; url = prefix + "/" + requestMapping.value()[0];
url = url.replaceAll("/+", "/");
urlSet.add(url);
} }
boolean mExits1 = method.isAnnotationPresent(GetMapping.class); boolean mExits1 = method.isAnnotationPresent(GetMapping.class);
if (mExits1) { if (mExits1) {
GetMapping getMapping = method.getAnnotation(GetMapping.class); GetMapping getMapping = method.getAnnotation(GetMapping.class);
String url = prefix + "/" + getMapping.value()[0]; url = prefix + "/" + getMapping.value()[0];
url = url.replaceAll("/+", "/");
urlSet.add(url);
} }
boolean mExits2 = method.isAnnotationPresent(PostMapping.class); boolean mExits2 = method.isAnnotationPresent(PostMapping.class);
if (mExits2) { if (mExits2) {
PostMapping postMapping = method.getAnnotation(PostMapping.class); PostMapping postMapping = method.getAnnotation(PostMapping.class);
String url = prefix + "/" + postMapping.value()[0]; url = prefix + "/" + postMapping.value()[0];
url = url.replaceAll("/+", "/");
urlSet.add(url);
} }
if (!ObjectUtils.isEmpty(url)){
ResourceEntity resourceEntity = buildResourceEntity( result, method, url, name);
resourceArrayList.add(resourceEntity);
}
} }
} }
//获取超类中的方法 //获取超类中的方法
Method[] superMethods = cls.getSuperclass().getDeclaredMethods(); Method[] superMethods = cls.getSuperclass().getDeclaredMethods();
if (superMethods != null && superMethods.length > 0) { if (!ObjectUtils.isEmpty(superMethods)) {
for (Method method : superMethods) { for (Method method : superMethods) {
boolean unAuth = method.isAnnotationPresent(UnAuth.class); boolean unAuth = method.isAnnotationPresent(UnAuth.class);
if (unAuth) { if (unAuth) {
continue; continue;
} }
String name = "其它";
String url = "";
boolean mExits = method.isAnnotationPresent(RequestMapping.class); boolean mExits = method.isAnnotationPresent(RequestMapping.class);
if (mExits) { if (mExits) {
RequestMapping requestMapping = method.getAnnotation(RequestMapping.class); RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
String url = prefix + "/" + requestMapping.value()[0]; url = prefix + "/" + requestMapping.value()[0];
url = url.replaceAll("/+", "/"); //判断路径是否存在枚举类中
urlSet.add(url); MethodNameEnum[] values = MethodNameEnum.values();
for (MethodNameEnum value : values) {
boolean contains = StrUtil.contains(requestMapping.value()[0], value.getValue());
if (contains) {
name = value.getDesc();
break;
}
}
} }
boolean mExits1 = method.isAnnotationPresent(GetMapping.class); boolean mExits1 = method.isAnnotationPresent(GetMapping.class);
if (mExits1) { if (mExits1) {
GetMapping getMapping = method.getAnnotation(GetMapping.class); GetMapping getMapping = method.getAnnotation(GetMapping.class);
String url = prefix + "/" + getMapping.value()[0]; url = prefix + "/" + getMapping.value()[0];
url = url.replaceAll("/+", "/"); //判断路径是否存在枚举类中
urlSet.add(url); MethodNameEnum[] values = MethodNameEnum.values();
for (MethodNameEnum value : values) {
boolean contains = StrUtil.contains(getMapping.value()[0], value.getValue());
if (contains) {
name = value.getDesc();
break;
}
}
} }
boolean mExits2 = method.isAnnotationPresent(PostMapping.class); boolean mExits2 = method.isAnnotationPresent(PostMapping.class);
if (mExits2) { if (mExits2) {
PostMapping postMapping = method.getAnnotation(PostMapping.class); PostMapping postMapping = method.getAnnotation(PostMapping.class);
String url = prefix + "/" + postMapping.value()[0]; url = prefix + "/" + postMapping.value()[0];
url = url.replaceAll("/+", "/"); MethodNameEnum[] values = MethodNameEnum.values();
urlSet.add(url); for (MethodNameEnum value : values) {
} boolean contains = StrUtil.contains(postMapping.value()[0], value.getValue());
}
}
if (!ObjectUtils.isEmpty(urlSet)) {
Set<String> UrlViewSet = new HashSet<>();
Set<String> UrlEditSet = new HashSet<>();
for (String url : urlSet) {
String str = StrUtil.subAfter(url, "/", true);
boolean bool = false;
for (String checkStr : ULR_VIEW) {
boolean contains = StrUtil.contains(str.toLowerCase(), checkStr);
if (contains) { if (contains) {
UrlViewSet.add(url); name = value.getDesc();
bool = true;
break; break;
} }
} }
if (!bool) {
UrlEditSet.add(url);
} }
}
resourceViewEntity.setUrl(UrlViewSet.stream().collect(Collectors.joining(",")));
System.out.println(JSONObject.toJSONString(resourceViewEntity));
resourceArrayList.add(resourceViewEntity);
resourceEditEntity.setUrl(UrlEditSet.stream().collect(Collectors.joining(",")));
System.out.println(JSONObject.toJSONString(resourceEditEntity));
resourceArrayList.add(resourceEditEntity); if (!ObjectUtils.isEmpty(url)){
ResourceEntity resourceEntity = buildResourceEntity( result, method, url, name);
resourceArrayList.add(resourceEntity);
}
}
} }
} }
} }
...@@ -203,6 +188,25 @@ public class ControllerScanUtil { ...@@ -203,6 +188,25 @@ public class ControllerScanUtil {
return resourceArrayList; return resourceArrayList;
} }
private static ResourceEntity buildResourceEntity( String result, Method method, String url, String name) {
url = url.replaceAll("/+", "/");
//判断方法上是否有ApiOperation注解 有读取名称
ApiOperation annotation = method.getAnnotation(ApiOperation.class);
if (annotation != null) {
name = annotation.value();
}
ResourceEntity resourceEntity = new ResourceEntity();
resourceEntity.initAttrValue();
resourceEntity.setName(name);
resourceEntity.setGroupName(result);
resourceEntity.setUrl(url);
resourceEntity.setCreateTime(new Date());
resourceEntity.setCreateUserId(1L);
resourceEntity.setCreateUserName("admin");
return resourceEntity;
}
public static List<Class<?>> getAllClassByPackageName(String packageName) { public static List<Class<?>> getAllClassByPackageName(String packageName) {
// 获取当前包下以及子包下所以的类 // 获取当前包下以及子包下所以的类
List<Class<?>> returnClassList = getClasses(packageName); List<Class<?>> returnClassList = getClasses(packageName);
......
package com.mortals.xhx.daemon.applicationservice; package com.mortals.xhx.daemon.applicationservice;
import com.mortals.framework.springcloud.service.IApplicationStartedService; import com.mortals.framework.springcloud.service.IApplicationStartedService;
import com.mortals.xhx.base.system.resource.service.ResourceService;
import com.mortals.xhx.base.system.user.service.UserService; import com.mortals.xhx.base.system.user.service.UserService;
import com.mortals.xhx.module.device.service.DeviceService; import com.mortals.xhx.module.device.service.DeviceService;
import com.mortals.xhx.module.skin.service.SkinService; import com.mortals.xhx.module.skin.service.SkinService;
...@@ -19,6 +20,9 @@ public class DemoStartedService implements IApplicationStartedService { ...@@ -19,6 +20,9 @@ public class DemoStartedService implements IApplicationStartedService {
@Autowired @Autowired
private SkinService skinService; private SkinService skinService;
@Autowired
private ResourceService resourceService;
@Override @Override
public void start() { public void start() {
log.info("开始服务..[配置已加载完成,并且所有框架都已经初始化]"); log.info("开始服务..[配置已加载完成,并且所有框架都已经初始化]");
...@@ -36,6 +40,13 @@ public class DemoStartedService implements IApplicationStartedService { ...@@ -36,6 +40,13 @@ public class DemoStartedService implements IApplicationStartedService {
log.info("开始同步用户!"); log.info("开始同步用户!");
userService.refreshUser(); userService.refreshUser();
log.info("结束同步用户!"); log.info("结束同步用户!");
try {
resourceService.refreshResourceUrl("com.mortals.xhx", null);
} catch (Exception e) {
log.error("刷新资源失败", e);
}
} }
@Override @Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment