自动载入权限实现

This commit is contained in:
张松
2025-02-19 17:04:10 +08:00
parent c024d83309
commit 77803e0b36
2 changed files with 83 additions and 60 deletions

View File

@@ -0,0 +1,82 @@
package com.czg;
import cn.hutool.core.date.DateUtil;
import com.mybatisflex.core.row.Db;
import com.mybatisflex.core.row.Row;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
@Component
@Slf4j
@SuppressWarnings("all")
@ConditionalOnClass(name = "com.czg.annotation.SaAdminCheckPermission")
public class LoadingRole implements CommandLineRunner {
@Autowired
private RequestMappingHandlerMapping requestMappingHandlerMapping;
public void run(String... args) {
Map<RequestMappingInfo, HandlerMethod> handlerMethods = this.requestMappingHandlerMapping.getHandlerMethods();
handlerMethods.forEach((key, value) -> {
Method method = value.getMethod();
try {
// 使用反射获取注解(不 import SaAdminCheckPermission
Class<?> annotationClass = Class.forName("com.czg.annotation.SaAdminCheckPermission");
Object annotation = AnnotationUtils.getAnnotation(method, (Class) annotationClass);
if (annotation == null) return;
// 通过反射获取注解的 value() 方法
Method valueMethod = annotationClass.getMethod("value");
String[] permissions = (String[]) valueMethod.invoke(annotation);
for (String s : permissions) {
String sql = "select * from sys_menu where permission=?";
Row menu1 = Db.selectOneBySql(sql, s);
if (menu1 != null) {
Long menuId = menu1.getLong("menu_id");
String name = menu1.getString("name");
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);
if (count1.isEmpty()) {
sql = "INSERT INTO `sys_roles_menus` (`menu_id`, `role_id`) VALUES (?, ?);";
Db.insertBySql(sql, menu1, 1L);
log.info("接口菜单添加成功, 菜单名称: {}, 菜单权限: {}", name, permission);
}
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, s, s, DateUtil.date());
sql = "select * from sys_menu where permission=?";
Row info = Db.selectOneBySql(sql, s);
Long menuId = info.getLong("menu_id");
sql = "INSERT INTO `sys_roles_menus` (`menu_id`, `role_id`) VALUES (?, ?);";
Db.insertBySql(sql, menuId, 1L);
log.info("接口菜单添加成功, 菜单名称: {}, 菜单权限: {}", s, s);
}
} catch (ClassNotFoundException e) {
log.warn("SaAdminCheckPermission 注解未找到,跳过权限检查");
} catch (Exception e) {
log.error("获取 SaAdminCheckPermission 注解失败", e);
}
});
}
}