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
...
}
JSP标签库授权
Shiro提供了一个标签库来控制JSP/GSP页面输出,这将在Web文档中的JSP/GSP标签库中讨论。
授权序列
现在我们已经看到如何对当前Subject执行授权,让我们了解一下当一个授权命令调用时Shiro内部发生了什么事情。
我们仍使用前面Architecture章节里的架构图,在左侧仅仅与授权相关的组件是高亮的,每一个数字代表授权操作中的一个步骤:

第1步:程序或框架代码调用一个Subject的hasRole*、checkRole*、 isPermitted*或者 checkPermission*方法,传递所需的权限或角色。
第2步:Subject实例,通常是一个DelegatingSubject(或子类),通过调用securityManager与各hasRole*、checkRole*、 isPermitted*或checkPermission*基本一致的方法将权限或角色传递给程序的SecurityManager。
第3步:SecurityManager,基本的安全组件,通过调用authorizer的各hasRole*、checkRole*、 isPermitted*或checkPermission*方法将权限转交/委托给其内部的org.apache.shiro.authz.Authorizer实例,它负责在授权操作中协调一个或多个Realm实例。
第4步,检查每一个被配置的Realm是否实现相同的Authorizer接口,如果是,Realm自己的各hasRole*、checkRole*、 isPermitted*或checkPermission*方法被调用。
ModularRealmAuthorizer
前面提到过,Shiro SecurityManager默认使用ModularRealmAuthorizer实例,ModularRealmAuthorizer实例同等支持用一个Realm的程序和用多个Realm的程序。
对于任何授权操作,ModularRealmAuthorizer将在其内部的Realm集中迭代(iterator),按迭代(iteration)顺序同每一个Realm交互,与每一个Realm交互的方法如下:
1.如果Realm实现了Authorizer接口,调用它各自的授权方法(hasRole*、 checkRole*、isPermitted*或 checkPermission*)。
1.如果Realm函数的结果是一个exception,该exception衍生自一个Subject调用者的AuthorizationException,就切断授权过程,剩余的授权Realm将不在执行。
2.如果Realm的方法是一个hasRole*或isPermitted*,并且返回真,则真值立即被返回而且剩余的Realm被短路,这种做法作为一种性能增强,在一个Realm判断允许后,隐含认为这个Subject被允许。它支持最安全的安全策略:默认情况下所有都被禁止,明确指定允许的事情。
2.如果Realm没有实现Authorizer接口,将被忽略。
Realm授权顺序
需要指出非常重要的一点,就如同验证(authentication)一样,ModularRealmAuthorizer按迭代(iteration)顺序与Realm交互。
ModularRealmAuthorizer拥有SecurityManager配置的Realm实例的入口,当执行一个授权操作时,它将在整个集合中进行迭代(iteration),对于每一个实现Authorizer接口的Realm,调用Realm各自的Authorizer方法(如 hasRole*、 checkRole*、 isPermitted*或 checkPermission*)。
