获取所有@PreAuthorize列表 春季

问题描述 投票:0回答:1

我试图找出一种方法来从Spring容器中获取所有预授权注释的列表

假设我有类似下面的内容。

    @PreAuthorize("hasPermission(null, 'opetussuunnitelma', 'LUONTI1')")
@PreAuthorize("hasPermission(null, 'opetussuunnitelma', 'LUONTI2')")
@PreAuthorize("hasPermission(null, 'opetussuunnitelma', 'LUONTI3')")

> I need to get that string(hasPermission(null, 'opetussuunnitelma',
> 'LUONTI2')) or at least List<Map> (x=null,y='opetussuunnitelma',
> z='LUONTI1')

有什么方法可以完成此操作,因为我没有这样的批注,因此我需要解析所有这些批注字符串并对此进行处理。

spring spring-boot spring-mvc spring-security spring-aop
1个回答
1
投票

为了将所有应用程序类加载到JVM中,您可以尝试Reflection libraryget the loaded beans from Spring应用程序上下文或将它们手动添加到静态列表中。就是说,没有确定的正确方法来实现这一目标。

要获取注释,请使用Java反射。

更多信息:https://www.geeksforgeeks.org/method-class-getannotation-method-in-java/

public void handleAnnotations(Class c) {
    try { 
        Method[] methods = c.getMethods(); 

        for (Method method : methods) { 
            PreAuthorize[] annotations = c.getAnnotationsByType(PreAuthorize.class);
            // handle annotations
        } 

    } 
    catch (Exception e) { 
        // handle exception
    } 
}
© www.soinside.com 2019 - 2024. All rights reserved.