From f6e3577573140d048b4a0a56f914808f0715f6b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E6=B2=90?= Date: Mon, 13 Jan 2025 10:47:04 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96PreAuthorize=E6=B3=A8?= =?UTF-8?q?=E8=A7=A3=E7=9A=84=E6=9D=83=E9=99=90=E6=A0=87=E8=AF=86=E5=88=97?= =?UTF-8?q?=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/security/utils/PreAuthorizeUtil.java | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 maku-framework/src/main/java/net/maku/framework/security/utils/PreAuthorizeUtil.java diff --git a/maku-framework/src/main/java/net/maku/framework/security/utils/PreAuthorizeUtil.java b/maku-framework/src/main/java/net/maku/framework/security/utils/PreAuthorizeUtil.java new file mode 100644 index 0000000..dc642ab --- /dev/null +++ b/maku-framework/src/main/java/net/maku/framework/security/utils/PreAuthorizeUtil.java @@ -0,0 +1,53 @@ +package net.maku.framework.security.utils; + +import cn.hutool.extra.spring.SpringUtil; +import org.springframework.context.ApplicationContext; +import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.stereotype.Controller; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * SpringSecurity @PreAuthorize 注解 权限标识 + * + * @author 阿沐 babamu@126.com + * MAKU + */ +public class PreAuthorizeUtil { + + /** + * 获取 @PreAuthorize 注解的权限标识列表 + * + * @return 权限标识列表 + */ + public static List getPreAuthorizeList() { + List authorities = new ArrayList<>(); + ApplicationContext context = SpringUtil.getApplicationContext(); + String[] beanNames = context.getBeanNamesForAnnotation(Controller.class); + for (String beanName : beanNames) { + Object bean = context.getBean(beanName); + Method[] methods = bean.getClass().getDeclaredMethods(); + for (Method method : methods) { + PreAuthorize preAuthorize = AnnotationUtils.findAnnotation(method, PreAuthorize.class); + if (preAuthorize != null) { + String value = preAuthorize.value(); + // 使用正则表达式提取权限字符串 + Pattern pattern = Pattern.compile("hasAuthority\\('([^']*)'\\)"); + Matcher matcher = pattern.matcher(value); + while (matcher.find()) { + String authority = matcher.group(1); + authorities.add(authority); + } + } + } + } + + return authorities; + } + +} \ No newline at end of file