Shiro权限标签
在使用Shiro标签库前,首先需要在JSP引入shiro标签:
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<shiro:authenticated/> 登录之后
<shiro:notAuthenticated/> 不在登录状态时
<shiro:user/> 用户在登录之后或RememberMe时
<shiro:guest/> 用户在没有登录或RememberMe时
<shiro:hasAnyRoles name="abc,123"/ > 拥有abc或者123角色时
<shiro:hasRole name="abc"/> 拥有角色abc
<shiro:lacksRole name="abc"/> 没有角色abc
<shiro:hasPermission name="abc"/> 拥有权限资源abc
<shiro:lacksPermission name="abc"/> 没有abc权限资源
<shiro:principal/> 显示用户身份名称
<shiro:principal property="username"/> 显示用户身份中的属性
Shiro权限注解
Shiro 提供了相应的注解用于权限控制,如果使用这些注解就需要使用AOP
的功能来进行判断,如Spring AOP;Shiro 提供了Spring AOP 集成用于权限注解的解析和验证。
1 注解配置
在springmvc-config.xml中配置shiro注解支持,可在controller方法中使用shiro注解配置权限
<!-- 开启aop对类代理 -->
<aop:config proxy-target-class="true"/>
<!-- 开启shiro注解支持 配置shiro的注解适配器-->
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>
2 @RequiresAuthentication
表示当前Subject已经通过login 进行了身份验证;
即Subject. isAuthenticated()返回true。
3 @RequiresUser
表示当前Subject已经身份验证或者通过记住我登录的。
4 @RequiresGuest
表示当前Subject没有身份验证或通过记住我登录过,即是游客身份。
5 @RequiresRoles
表示当前Subject需要角色admin和user。
@RequiresRoles(value={“admin”, “user”}, logical= Logical.AND)
@RequiresRoles(value={“admin”})
@RequiresRoles({“admin”})
6 @RequiresPermissions
@RequiresPermissions (value={“user:add”, “user:update”}, logical= Logical.OR)
表示当前Subject需要权限user:add或user:update。