除了通过API方式外,Shiro提供Java5+注解的集合,以注解为基础的授权控制。在你可以使用Java注释之前,你需要在你的应用程序中启用AOP支持。
Shiro注解支持AspectJ、Spring、Google-Guice等,可根据应用进行不同的配置。
相关的注解如下:
TheRequiresAuthenticationannotation(RequiresAuthentication注解)
要求当前Subject已经在当前的session中被验证通过才能被访问或调用。例如:
@RequiresAuthentication publicvoidupdateAccount(AccountuserAccount){ //thismethodwillonlybeinvokedbya //Subjectthatisguaranteedauthenticated … } |
TheRequiresGuestannotation(RequiresGuest注解)
要求当前的Subject是一个"guest",也就是说,他们必须是在之前的session中没有被验证或被记住才能被访问或调用。例如:
@RequiresGuest publicvoidsignUp(UsernewUser){ //thismethodwillonlybeinvokedbya //Subjectthatisunknown/anonymous … } |
TheRequiresPermissionsannotation(RequiresPermissions注解)
要求当前的Subject被允许一个或多个权限,以便执行注解的方法。例如:
@RequiresPermissions("account:create") publicvoidcreateAccount(Accountaccount){ //thismethodwillonlybeinvokedbyaSubject //thatispermittedtocreateanaccount … } |
TheRequiresRolesannotation(RequiresRoles注解)
要求当前的Subject拥有所有指定的角色。如果他们没有,则该方法将不会被执行,而且AuthorizationException异常将会被抛出。例如:
@RequiresRoles("administrator") publicvoiddeleteUser(Useruser){ //thismethodwillonlybeinvokedbyanadministrator … } |
TheRequiresUserannotation(RequiresUser注解)
RequiresUser注解需要当前的Subject是一个应用程序用户才能被注解的类/实例/方法访问或调用。一个“应用程序用户”被定义为一个拥有已知身份,或在当前session中由于通过验证被确认,或者在之前session中的'RememberMe'服务被记住。例如:
@RequiresUser publicvoidupdateAccount(Accountaccount){ //thismethodwillonlybeinvokedbya'user' //i.e.aSubjectwithaknownidentity … } |