gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:42:41 28 4
gpt4 key购买 nike

我可以在注释上使用 @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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com