shiro基于注解的授權

jopen 10年前發布 | 43K 次閱讀 Shiro 安全相關

基于注解的授權

如果你更喜歡基于注解的授權控制,除了Subject的API之外,Shiro提供了一個Java5的注解集。

配置

在你使用JAVA的注解之前,你需要在程序中啟動AOP支持,因為有許多AOP框架,所以很不幸,在這里并沒有標準的在程序中啟用AOP的方法。

關于AspectJ,你可以查看我們的AspectJ sample application(http://svn.apache.org/repos/asf/shiro/trunk/samples/aspectj/);

關于Spring,你可以查看Spring Integration文檔;

關于Guice,你可以查看我們的 Guice Integration文檔;

 

RequiresAuthentication注解

RequiresAuthentication注解要求在訪問或調用被注解的類/實例/方法時,Subject在當前的session中已經被驗證。

例如:

@RequiresAuthentication

public void updateAccount(Account userAccount) {

//this method will only be invoked by a

//Subject that is guaranteed authenticated

...

}

這基本上與下面的基于對象的邏輯效果相同:

public void updateAccount(Account userAccount) {

if (!SecurityUtils.getSubject().isAuthenticated()) {

throw new AuthorizationException(...);

}

//Subject is guaranteed authenticated here

...

}

 

RequiresGuest注解

RequiresGuest注解要求當前Subject是一個“訪客”,也就是,在訪問或調用被注解的類/實例/方法時,他們沒有被認證或者在被前一個Session記住。

例如:

@RequiresGuest

public void signUp(User newUser) {

//this method will only be invoked by a

//Subject that is unknown/anonymous

...

}

這基本上與下面的基于對象的邏輯效果相同:

public void signUp(User newUser) {

Subject currentUser = SecurityUtils.getSubject();

PrincipalCollection principals = currentUser.getPrincipals();

if (principals != null && !principals.isEmpty()) {

//known identity - not a guest:

throw new AuthorizationException(...);

}

//Subject is guaranteed to be a 'guest' here

...

}

 

RequiresPermissions 注解

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

...

}

這基本上與下面的基于對象的邏輯效果相同:

public void createAcc

/this method will only be invoked by an administrator

...

}

 

RequiresRoles 注解

RequiresPermissions 注解要求當前Subject在執行被注解的方法時具備所有的角色,否則將拋出AuthorizationException異常。

例如:

@RequiresRoles("administrator")

public void deleteUser(User user) {

//this method will only be invoked by an administrator

...

}

這基本上與下面的基于對象的邏輯效果相同:

public void deleteUser(User user) {

Subject currentUser = SecurityUtils.getSubject();

if (!subject.hasRole("administrator")) {

throw new AuthorizationException(...);

}

//Subject is guaranteed to be an 'administrator' here

...

}

 

RequiresUser 注解

RequiresUser*注解要求在訪問或調用被注解的類/實例/方法時,當前Subject是一個程序用戶,“程序用戶”是一個已知身份的Subject,或者在當前Session中被驗證過或者在以前的Session中被記住過。

例如:

@RequiresUser

public void updateAccount(Account account) {

//this method will only be invoked by a 'user'

//i.e. a Subject with a known identity

...

}

這基本上與下面的基于對象的邏輯效果相同:

public void updateAccount(Account account) {

Subject currentUser = SecurityUtils.getSubject();

PrincipalCollection principals = currentUser.getPrincipals();

if (principals == null || principals.isEmpty()) {

//no identity - they're anonymous, not allowed:

throw new AuthorizationException(...);

}

//Subject is guaranteed to have a known identity here

...

}

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!