权限修改

This commit is contained in:
张松
2025-05-10 10:18:02 +08:00
parent 4421b1c33c
commit 4aadec6f1f
18 changed files with 287 additions and 28 deletions

View File

@@ -10,6 +10,7 @@ import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
@@ -32,6 +33,45 @@ public class LoadingRole implements CommandLineRunner {
Method method = value.getMethod();
try {
Class<?> controllerClass = method.getDeclaringClass();
// 获取类上的 @RequestMapping 路径
String classPath = "";
if (controllerClass.isAnnotationPresent(RequestMapping.class)) {
RequestMapping classMapping = controllerClass.getAnnotation(RequestMapping.class);
classPath = classMapping.value().length > 0 ? classMapping.value()[0] : "";
}
// 获取方法上的注解路径和请求方式
String methodPaths = "";
String httpMethod = "UNKNOWN";
if (method.isAnnotationPresent(GetMapping.class)) {
GetMapping mapping = method.getAnnotation(GetMapping.class);
methodPaths = mapping.value().length > 0 ? mapping.value()[0] : "";
httpMethod = "GET";
} else if (method.isAnnotationPresent(PostMapping.class)) {
PostMapping mapping = method.getAnnotation(PostMapping.class);
methodPaths = mapping.value().length > 0 ? mapping.value()[0] : "";
httpMethod = "POST";
} else if (method.isAnnotationPresent(PutMapping.class)) {
PutMapping mapping = method.getAnnotation(PutMapping.class);
methodPaths = mapping.value().length > 0 ? mapping.value()[0] : "";
httpMethod = "PUT";
} else if (method.isAnnotationPresent(DeleteMapping.class)) {
DeleteMapping mapping = method.getAnnotation(DeleteMapping.class);
methodPaths = mapping.value().length > 0 ? mapping.value()[0] : "";
httpMethod = "DELETE";
} else if (method.isAnnotationPresent(RequestMapping.class)) {
RequestMapping mapping = method.getAnnotation(RequestMapping.class);
methodPaths = mapping.value().length > 0 ? mapping.value()[0] : "";
RequestMethod[] methods = mapping.method();
httpMethod = methods.length > 0 ? methods[0].name() : "ALL";
}
// 拼接路径并输出
String fullPath = (classPath + "/" + methodPaths).replaceAll("//+", "/");
// 使用反射获取注解(不 import SaAdminCheckPermission
Class<?> annotationClass = Class.forName("com.czg.annotation.SaAdminCheckPermission");
Object annotation = AnnotationUtils.getAnnotation(method, (Class) annotationClass);
@@ -51,6 +91,8 @@ public class LoadingRole implements CommandLineRunner {
if (menu1 != null) {
Long menuId = menu1.getLong("menu_id");
String title = menu1.getString("title");
String url = menu1.getString("url");
String method1 = menu1.getString("method");
String permission = menu1.getString("permission");
String listSql = "select * from sys_roles_menus where menu_id=? and role_id=?";
List<Row> count1 = Db.selectListBySql(listSql, menuId, 1L);
@@ -61,17 +103,17 @@ public class LoadingRole implements CommandLineRunner {
log.info("接口菜单添加成功, 菜单名称: {}, 菜单权限: {}", title, permission);
}
if (StrUtil.isNotBlank(permissionName) && (title == null || !title.equals(permissionName))) {
sql = "update sys_menu set title=? where menu_id=?";
Db.updateBySql(sql, permissionName, menuId);
if (!title.equals(permissionName) || !fullPath.equals(url) || !httpMethod.equals(method1)) {
sql = "update sys_menu set title=?, url=?, method=? where menu_id=?";
Db.updateBySql(sql, permissionName, fullPath, httpMethod, menuId);
log.info("接口菜单修改成功, 旧名称: {}, 新菜单名称: {}", title, permissionName);
}
continue;
}
sql = "INSERT INTO `czg_cashier`.`sys_menu` ( `sub_count`, `type`, `title`, `name`, `component`, `menu_sort`, `icon`, `path`, `i_frame`, `cache`, `hidden`, `permission`, `create_by`, `update_by`, `create_time`, `update_time`, `active_menu`, `is_shop`) VALUES " +
"(0, 2, ?, NULL, '', 2, '', '', b'0', b'0', b'0', ?, NULL, NULL, ?, NULL, NULL, 0);";
Db.insertBySql(sql, StrUtil.isNotBlank(permissionName) ? permissionName : s, s, DateUtil.date());
sql = "INSERT INTO `czg_cashier`.`sys_menu` ( `sub_count`, `type`, `title`, `name`, `component`, `menu_sort`, `icon`, `path`, `i_frame`, `cache`, `hidden`, `permission`, `create_by`, `update_by`, `create_time`, `update_time`, `active_menu`, `is_shop`, 'url', 'method') VALUES " +
"(0, 2, ?, NULL, '', 2, '', '', b'0', b'0', b'0', ?, NULL, NULL, ?, NULL, NULL, 0, ?, ?);";
Db.insertBySql(sql, StrUtil.isNotBlank(permissionName) ? permissionName : s, s, DateUtil.date(), fullPath, httpMethod);
sql = "select * from sys_menu where permission=?";
Row info = Db.selectOneBySql(sql, s);
Long menuId = info.getLong("menu_id");