最简单和最常见的方式来执行授权是直接以编程方式与当前Subject 实例交互。
基于角色授权
Role checks(角色检查)
如果你只是简单的想检查当前的Subject 是否拥有一个角色,你可以在Subject 实例上调用hasRole方法。例如:
Subject currentUser = SecurityUtils.getSubject(); if(currentUser.hasRole("administrator")) { //show the admin button } else { //don't show the button? Grey it out? } |
相关的角色检查方法如下:
Subject方法 |
描述 |
hasRole(String roleName) |
返回true 如果Subject 被分配了指定的角色,否则返回false。 |
hasRoles(List<String> roleNames) |
返回true 如果Subject 被分配了所有指定的角色,否则返回false。 |
hasAllRoles(Collection<String> roleNames) |
返回一个与方法参数中目录一致的hasRole 结果的数组。 |
Role Assertions(角色断言)
Shiro还支持以断言的方式进行授权验证。断言成功,不返回任何值,程序继续执行;断言失败时,将抛出异常信息AuthorizationException。例如:
Subject currentUser = SecurityUtils.getSubject(); //guarantee that the current user is a bank teller and //therefore allowed to open the account: currentUser.checkRole("bankTeller"); openBankAccount(); |
相关的断言检查方法如下:
Subject方法 |
描述 |
checkRole(String roleName) |
安静地返回,如果Subject 被分配了指定的角色,不然的话就抛出。 |
checkRoles(Collection<String> roleNames) |
安静地返回,如果Subject 被分配了所有的指定的角色,不然的话就抛出。 |
checkRoles(String... roleNames) |
与上面的checkRoles 方法的效果相同,但允许Java5 的var-args 类型的参数。 |
基于权限授权
相比传统角色模式,基于权限的授权模式耦合性要更低些,它不会因角色的改变而对源代码进行修改,因此,基于权限的授权模式是更好的访问控制方式。
如果你想进行检查,看一个Subject 是否被允许做某事,你可以调用各种isPermitted*方法的变种。检查权限主要有两个方式——基于对象的权限实例或代表权限的字符串。
Object-based Permission Checks(基于对象的权限检查)
执行权限检查的一个可行方法是实例化org.apache.shiro.authz.Permission 接口的一个实例,并把它传递给接收权限实例的*isPermitted 方法。比如,在办公室有一台打印机,具有唯一标识符laserjet4400n。我们的软件需要检查当前用户是否被允许在该打印机上打印文档。上述情况的权限检查可以明确地像这样表达:
Permission printPermission = new PrinterPermission("laserjet4400n","print"); Subject currentUser = SecurityUtils.getSubject(); If (currentUser.isPermitted(printPermission)) { //show the Print button } else { //don't show the button? Grey it out? } |
相关的验证方法如下:
Subject方法 |
描述 |
isPermitted(Permission p) |
返回true 如果该Subject 被允许执行某动作或访问被权限实例指定的资源集合,否则返回false。 |
isPermitted(List<Permission> perms) |
返回一个与方法参数中目录一致的isPermitted 结果的数组。 |
isPermittedAll(Collection<Permission> perms) |
返回true 如果该Subject 被允许所有指定的权限,否则返回false。 |
String-based permission checks(基于字符串的权限检查)
基于对象的权限可以是很有用的(编译时类型安全,保证行为,定制蕴含逻辑等),但它们有时对应用程序来说会感到有点“笨手笨脚”的。另一种方法是使用正常的字符串来表示权限实例。例如上述的例子使用基于字符串的权限检查如下:
Subject currentUser = SecurityUtils.getSubject(); if (currentUser.isPermitted("printer:print:laserjet4400n")) { //show the Print button } else { //don't show the button? Grey it out? } |
这个例子显示了一个特殊冒号分隔的格式,它由Shiro 默认的org.apache.shiro.authz.permission.WildcardPermission 实现来定义的。这里分别代表了:资源类型:操作:资源ID 。WildcardPermission token 规定和构造操作的格式在Shiro 的Permission 文档中被深入的涉及到。除了上面的字符串默认的WildcardPermission 格式,你可以创建和使用自己的字符串格式如果你喜欢的话。
相关的验证方法如下:
Subject方法 |
描述 |
isPermitted(String perm) |
返回true 如果该Subject 被允许执行某动作或访问被字符串权限指定的资源,否则返回false。 |
isPermitted(String…perms) |
返回一个与方法参数中目录一致的isPermitted 结果的数组。 |
isPermittedAll(String…perms) |
返回true 如果该Subject 被允许所有指定的字符串权限,否则返回false。 |
Permission Assertions(权限断言)
以断言的方式进行授权验证。断言成功,不返回任何值,程序继续执行;断言失败时,将抛出异常信息AuthorizationException。例如:
Subject currentUser = SecurityUtils.getSubject(); //guarantee that the current user is permitted //to open a bank account: Permission p = new AccountPermission("open"); currentUser.checkPermission(p); openBankAccount(); |
或者使用字符串来检查权限:
Subject currentUser = SecurityUtils.getSubject(); //guarantee that the current user is permitted //to open a bank account: currentUser.checkPermission("open"); openBankAccount(); |
相关的断言方法如下:
Subject方法 |
描述 |
checkPermission(Permission p) |
安静地返回,如果Subject 被允许执行某动作或访问被特定的权限实例指定的资源,不然的话就抛出AuthorizationException 异常。 |
checkPermission(String perm) |
安静地返回,如果Subject 被允许执行某动作或访问被特定的字符串权限指定的资源,不然的话就抛出AuthorizationException 异常。 |
checkPermissions(Collection<Permission> perms) |
安静地返回,如果Subject 被允许所有的权限,不然的话就抛出AuthorizationException 异常。 |
checkPermissions(String… perms) |
和上面的checkPermissions 方法效果相同,但是使用的是基于字符串的权限。 |