簡體   English   中英

我可以在自己的注釋中使用Spring Security @PreAuthorize嗎?

[英]Can I use the Spring Security @PreAuthorize on my own 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控制器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
{

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM