shiro授权实现

授权实现

Shiro支持三种方式实现授权过程:

  • 编码实现
  • 注解实现
  • JSP Taglig实现
1、基于编码的授权实现

1.1基于传统角色授权实现

当需要验证用户是否拥有某个角色时,可以调用Subject 实例的hasRole*方法验证。
[java]  view plain  copy
  1. Subject currentUser = SecurityUtils.getSubject();    
  2. if (currentUser.hasRole("administrator")) {    
  3.     //show the admin button    
  4. else {    
  5.     //don't show the button?  Grey it out?    
  6. }   


相关验证方法如下:
Subject方法描述
hasRole(String roleName)当用户拥有指定角色时,返回true
hasRoles(List<String> roleNames)按照列表顺序返回相应的一个boolean值数组
hasAllRoles(Collection<String> roleNames)如果用户拥有所有指定角色时,返回true

断言支持
Shiro还支持以断言的方式进行授权验证。断言成功,不返回任何值,程序继续执行;断言失败时,将抛出异常信息。使用断言,可以使我们的代码更加简洁。
[java]  view plain  copy
  1. Subject currentUser = SecurityUtils.getSubject();    
  2. //guarantee that the current user is a bank teller and    
  3. //therefore allowed to open the account:    
  4. currentUser.checkRole("bankTeller");    
  5. openBankAccount();    


断言的相关方法:
Subject方法描述
checkRole(String roleName)断言用户是否拥有指定角色
checkRoles(Collection<String> roleNames)断言用户是否拥有所有指定角色
checkRoles(String... roleNames)对上一方法的方法重载

1.2 基于权限角色授权实现
相比传统角色模式,基于权限的角色模式耦合性要更低些,它不会因角色的改变而对源代码进行修改,因此,基于权限的角色模式是更好的访问控制方式。
它的代码实现有以下几种实现方式:
1、基于权限对象的实现
创建org.apache.shiro.authz.Permission的实例,将该实例对象作为参数传递给Subject.isPermitted()进行验证。
[java]  view plain  copy
  1. Permission printPermission = new PrinterPermission("laserjet4400n""print");    
  2. Subject currentUser = SecurityUtils.getSubject();    
  3. if (currentUser.isPermitted(printPermission)) {    
  4.     //show the Print button    
  5. else {    
  6.     //don't show the button?  Grey it out?    
  7. }    
  8. Permission printPermission = new PrinterPermission("laserjet4400n""print");    
  9. Subject currentUser = SecurityUtils.getSubject();    
  10. if (currentUser.isPermitted(printPermission)) {    
  11.     //show the Print button    
  12. else {    
  13.     //don't show the button?  Grey it out?    
  14. }    


相关方法如下:
Subject方法描述
isPermitted(Permission p)Subject拥有制定权限时,返回treu
isPermitted(List<Permission> perms)返回对应权限的boolean数组
isPermittedAll(Collection<Permission> perms)Subject拥有所有制定权限时,返回true

2、 基于字符串的实现
相比笨重的基于对象的实现方式,基于字符串的实现便显得更加简洁。
[java]  view plain  copy
  1. Subject currentUser = SecurityUtils.getSubject();    
  2. if (currentUser.isPermitted("printer:print:laserjet4400n")) {    
  3.     //show the Print button    
  4. else {    
  5.     //don't show the button?  Grey it out?    
  6. }   


使用冒号分隔的权限表达式是org.apache.shiro.authz.permission.WildcardPermission 默认支持的实现方式。
这里分别代表了 资源类型:操作:资源ID

类似基于对象的实现相关方法,基于字符串的实现相关方法:
isPermitted(String perm)、isPermitted(String... perms)、isPermittedAll(String... perms)

基于权限对象的断言实现
[java]  view plain  copy
  1. Subject currentUser = SecurityUtils.getSubject();    
  2. //guarantee that the current user is permitted    
  3. //to open a bank account:    
  4. Permission p = new AccountPermission("open");    
  5. currentUser.checkPermission(p);    
  6. openBankAccount();    


基于字符串的断言实现
[java]  view plain  copy
  1. Subject currentUser = SecurityUtils.getSubject();    
  2. //guarantee that the current user is permitted    
  3. //to open a bank account:    
  4. currentUser.checkPermission("account:open");    
  5. openBankAccount();    


断言实现的相关方法
Subject方法说明
checkPermission(Permission p)断言用户是否拥有制定权限
checkPermission(String perm)断言用户是否拥有制定权限
checkPermissions(Collection<Permission> perms)断言用户是否拥有所有指定权限
checkPermissions(String... perms)断言用户是否拥有所有指定权限

2、基于注解的授权实现
Shiro注解支持AspectJ、Spring、Google-Guice等,可根据应用进行不同的配置。

相关的注解:
@ RequiresAuthentication
可以用户类/属性/方法,用于表明当前用户需是经过认证的用户。
[java]  view plain  copy
  1. @RequiresAuthentication    
  2. public void updateAccount(Account userAccount) {    
  3.     //this method will only be invoked by a     
  4.     //Subject that is guaranteed authenticated    
  5.     ...    
  6. }    


@ RequiresGuest
表明该用户需为”guest”用户

@ RequiresPermissions
当前用户需拥有制定权限
[java]  view plain  copy
  1. @RequiresPermissions("account:create")    
  2. public void createAccount(Account account) {    
  3.     //this method will only be invoked by a Subject    
  4.     //that is permitted to create an account    
  5.     ...    
  6. }    


@RequiresRoles
当前用户需拥有制定角色

@ RequiresUser
当前用户需为已认证用户或已记住用户

3、基于JSP  TAG的授权实现
Shiro提供了一套JSP标签库来实现页面级的授权控制。
在使用Shiro标签库前,首先需要在JSP引入shiro标签:
[java]  view plain  copy
  1. <%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>    


下面一一介绍Shiro的标签:
guest标签
验证当前用户是否为“访客”,即未认证(包含未记住)的用户
[html]  view plain  copy
  1. <shiro:guest>    
  2.     Hi there!  Please <a href="login.jsp">Login</a> or <a href="signup.jsp">Signup</a> today!    
  3. </shiro:guest>    


user标签
认证通过或已记住的用户
[html]  view plain  copy
  1. <shiro:user>    
  2.     Welcome back John!  Not John? Click <a href="login.jsp">here<a> to login.    
  3. </shiro:user>    


authenticated标签
已认证通过的用户。不包含已记住的用户,这是与user标签的区别所在。
[html]  view plain  copy
  1. <shiro:authenticated>    
  2.     <a href="updateAccount.jsp">Update your contact information</a>.    
  3. </shiro:authenticated>    


notAuthenticated标签
未认证通过用户,与authenticated标签相对应。与guest标签的区别是,该标签包含已记住用户。
[html]  view plain  copy
  1. <shiro:notAuthenticated>    
  2.     Please <a href="login.jsp">login</a> in order to update your credit card information.    
  3. </shiro:notAuthenticated>    


principal 标签
输出当前用户信息,通常为登录帐号信息
[html]  view plain  copy
  1. Xml代码    
  2. Hello, <shiro:principal/>, how are you today?    


hasRole标签
验证当前用户是否属于该角色
[html]  view plain  copy
  1. <shiro:hasRole name="administrator">    
  2.     <a href="admin.jsp">Administer the system</a>    
  3. </shiro:hasRole>  


lacksRole标签
与hasRole标签逻辑相反,当用户不属于该角色时验证通过
[html]  view plain  copy
  1. <shiro:lacksRole name="administrator">    
  2.     Sorry, you are not allowed to administer the system.    
  3. </shiro:lacksRole>    


hasAnyRole标签
验证当前用户是否属于以下任意一个角色。
[html]  view plain  copy
  1. <shiro:hasAnyRoles name="developer, project manager, administrator">    
  2.     You are either a developer, project manager, or administrator.    
  3. </shiro:lacksRole>    


hasPermission标签
验证当前用户是否拥有制定权限
[html]  view plain  copy
  1. <shiro:hasPermission name="user:create">    
  2.     <a href="createUser.jsp">Create a new User</a>    
  3. </shiro:hasPermission>    


lacksPermission标签
与hasPermission标签逻辑相反,当前用户没有制定权限时,验证通过
[html]  view plain  copy
  1. <shiro:hasPermission name="user:create">    
  2.     <a href="createUser.jsp">Create a new User</a>    
  3. </shiro:hasPermission>    

转自:http://blog.csdn.net/lufeng20/article/details/7594711

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值