java - 我可以在自己的注释上使用 Spring Security @PreAuthorize 吗?

标签 java spring spring-mvc spring-security annotations

我可以在注释上使用 @PreAuthorize 吗?

在 Spring 中,我可以在注释中使用像 ComponentDependsOn 这样的注释,如下所示:

@Target(ElementType.TYPE)
@Component
@DependsOn(CoreInitializerConfig.ROLE_INITIALIZER_ID)
public @interface WebComponent
{

}

而且效果很好。但是当我尝试以同样的方式使用 PreAuthorize 时:

@Target(
{
    ElementType.TYPE, ElementType.METHOD
})
@Component
@PreAuthorize("hasAuthority('PERM_READ_SETTINGS')")
public @interface SettingsAuthorized
{

}

不工作,我在 MVC Controller pojo 和 Bean 的方法中尝试过但没有工作,我必须明确指出:

@Controller
@PreAuthorize("hasAuthority('PERM_READ_SETTINGS')") 
public class SettingsController
{
    ...

}

最佳答案

我通过添加 @Retention(RetentionPolicy.RUNTIME) 解决了这个问题 还建议在最后的注解中加上@Documented@Inherited,所以结果是:

@Target(
{
    ElementType.TYPE, ElementType.METHOD
})
@Component
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@PreAuthorize("hasAuthority('PERM_READ_SETTINGS')") 
public @interface SettingsAuthorized
{

}

关于java - 我可以在自己的注释上使用 Spring Security @PreAuthorize 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31388677/

相关文章:

java - 当在数据库中的表之间添加新映射时,出现错误: Injection of autowired dependencies failed

java - centos上liquibase安装,JNI报错

java - ExceptionHandler,得到 java.lang.IllegalStateException : Could not resolve method parameter at index 0?

spring-mvc - Swagger for SpringMVC-Springfox的替代品?

java - Spring Boot Spark 应用程序

java - 如何将 Spring Application Context 事件桥接到其他上下文

java - 在 java 中填充值后,Excel 下载无法正常工作

java - 在 tomcat 上运行时 ClassLoader.getSystemResourceAsStream 不工作

java - JBoss 7 : EJB over HTTP

java - mybatis-guice mysql 加载jdbc驱动失败