基于字符串的权限检查
虽然基于对象的权限检查很有用(编译期类型安全,对行为担保,定制隐含逻辑等),但在许多程序里有时候感觉有点笨重,另一种选择是用普通的字符串来代表权限。
例如,对于上面打印权限的例子,我们可以使用字符串权限检查达到同样的结果:
Subject currentUser = SecurityUtils.getSubject();
if (currentUser.isPermitted("printer:print:laserjet4400n")) {
//show the Print button
} else {
//don't show the button? Grey it out?
}
这个例子同样实现了实例级别的权限检查,但是所有主要权限部件--printer(资源类型)、print(动作)、laserjet4400n(实例ID)都表现为一个字符串。
上面的例子展示了一种以冒号分割的特殊形式的字符串,定义于Shiro的 org.apache.shiro.authz.permission.WildcardPermission中,它适合大多数用户的需求。
上面的代码块基本上是下面这段代码的缩写:
Permission p = new WildcardPermission("printer:print:laserjet4400n");
if (currentUser.isPermitted(p) {
//show the Print button
} else {
//don't show the button? Grey it out?
}
WildcardPermission令牌形式和构成选项将在Shiro的Permission文档中深入讨论。
上面的字符串使用默认的WildcardPermission格式,实际上你可以创造并使用你自己的字符串格式,我们将在下面Realm授权章节讨论如何这样做。
基于字符串的权限有利的一面在于你不需要实现一个接口而且简单的字符串也非常易读,而不利的一面在于不保证类型安全,而且当你需要定义超出字符串表现能力之外的更复杂的行为时,你仍旧需要利用权限接口实现你自己的权限对象。实际上,大部分Shiro的终端用户因为其简单而选择基于字符串的方式,但最终你的程序需求决定了哪一种方法会更好。
和基于对象的权限检查方法一样,下面是字符串权限检查的函数:
isPermitted(String perm)
如果Subject被允许执行字符串表达的动作或资源访问权限,返回真,否则返回假;
isPermitted(String... perms)
按照参数顺序返回isPermitted的结果数组,当许多字符串权限需要检查时非常有用(如定制一个复杂的视图时);
isPermittedAll(String... perms)
当Subject具备所有字符串定义的权限时返回真,否则返回假。
权限判断
另一种检查Subject是否被允许做某件事的方法是,在逻辑执行之前简单判断他们是否具备所需的权限,如果不允许,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("account:open");
openBankAccount();
与isPermitted* 方法相比较,这种方法的优势是代码更为清晰,如果当前Subject不符合条件,你不必创建你自己的AuthorizationExceptions异常(如果你不想那么做)。
下面是你可以根据需要调用的函数:
checkPermission(Permission p)
如果Subject被允许执行特定权限实例指定的动作或资源访问,安静地返回,否则抛出AuthorizationException异常。
checkPermission(String perm)
如果Subject被允许执行权限字符串指定的动作或资源访问,安静地返回,否则抛出AuthorizationException异常。
checkPermissions(Collection<Permission> perms)
如果Subject被允许执行所有权限实例指定的动作或资源访问,安静地返回,否则抛出AuthorizationException异常。
checkPermissions(String... perms) 和上面的checkPermissions效果一样,只是使用字符串权限类型。
基于注解的授权
如果你更喜欢基于注解的授权控制,除了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
...
}
