Apache Shiro 註解方式授權
除了通過API方式外,Shiro 提供Java 5+註解的集合,以註解為基礎的授權控制。在你可以使用Java 註釋之前,你需要在你的應用程式中啟用AOP 支援。
Shiro註解支援AspectJ、Spring、Google-Guice等,可根據應用進行不同的配置。
相關的註解如下:
The RequiresAuthentication annotation(RequiresAuthentication 註解)
要求當前Subject 已經在當前的session 中被驗證通過才能被訪問或呼叫。例如:
@RequiresAuthentication public void updateAccount(Account userAccount) { //this method will only be invoked by a //Subject that is guaranteed authenticated … } |
The RequiresGuest annotation(RequiresGuest 註解)
要求當前的Subject 是一個"guest",也就是說,他們必須是在之前的session 中沒有被驗證或被記住才能被訪問或呼叫。例如:
@RequiresGuest public void signUp(User newUser) { //this method will only be invoked by a //Subject that is unknown/anonymous … } |
The RequiresPermissions annotation(RequiresPermissions 註解)
要求當前的Subject 被允許一個或多個許可權,以便執行註解的方法。例如:
@RequiresPermissions("account:create") public void createAccount(Account account) { //this method will only be invoked by a Subject //that is permitted to create an account … } |
The RequiresRoles annotation(RequiresRoles 註解
要求當前的Subject 擁有所有指定的角色。如果他們沒有,則該方法將不會被執行,而且AuthorizationException 異常將會被丟擲。例如:
@RequiresRoles("administrator") public void deleteUser(User user) { //this method will only be invoked by an administrator … } |
The RequiresUser annotation(RequiresUser 註解)
RequiresUser 註解需要當前的Subject 是一個應用程式使用者才能被註解的類/例項/方法訪問或呼叫。一個“應用程式使用者”被定義為一個擁有已知身份,或在當前session 中由於通過驗證被確認,或者在之前session 中的'RememberMe'服務被記住。例如:
@RequiresUser public void updateAccount(Account account) { //this method will only be invoked by a 'user' //i.e. a Subject with a known identity … } |