我可以在我自己的注释上使用 Spring Security @PreAuthorize 吗?
Can I use the Spring Security @PreAuthorize on my own annotations?
我可以在注释上使用 @PreAuthorize
吗?
在Spring中,我可以在注释中使用像Component
和DependsOn
这样的注释,像这样:
@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
{
}
我可以在注释上使用 @PreAuthorize
吗?
在Spring中,我可以在注释中使用像Component
和DependsOn
这样的注释,像这样:
@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
{
}