Apache Shiro 使用手册(三) Shiro授权

2019-01-17 03:58:09王冬梅
Subject拥有制定权限时,返回treu isPermitted(List<Permission> perms) 返回对应权限的boolean数组 isPermittedAll(Collection<Permission> perms) Subject拥有所有制定权限时,返回true
2、基于字符串的实现

相比笨重的基于对象的实现方式,基于字符串的实现便显得更加简洁。


Subject currentUser = SecurityUtils.getSubject();
if (currentUser.isPermitted("printer:print:laserjet4400n")) {
    //show the Print button
} else {
    //don't show the button?  Grey it out?
}

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

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

基于权限对象的断言实现


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("account:open");
openBankAccount();