过滤器
| 过滤器简称 | 对应的java类 |
|---|---|
| anon | org.apache.shiro.web.filter.authc.AnonymousFilter |
| authc | org.apache.shiro.web.filter.authc.FormAuthenticationFilter |
| authcBasic | org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter |
| perms | org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter |
| port | org.apache.shiro.web.filter.authz.PortFilter |
| rest | org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter |
| roles | org.apache.shiro.web.filter.authz.RolesAuthorizationFilter |
| ssl | org.apache.shiro.web.filter.authz.SslFilter |
| user | org.apache.shiro.web.filter.authc.UserFilter |
| logout | org.apache.shiro.web.filter.authc.LogoutFilter |
在spring-shiro.xml配置过滤器
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<!-- loginUrl认证地址,如果没有认证将会此地址进行认证-->
<property name="loginUrl" value="/toLogin"/>
<!-- 认证成功统一跳转到index,建议不配置,shiro认证成功自动跳请求路径-->
<!--<property name="successUrl" value="index"/>-->
<!-- 通过unauthorizedUrl指定没有权限操作时跳转页面-->
<property name="unauthorizedUrl" value="403.html" />
<property name="filterChainDefinitions">
<value>
<!-- 这里顺序执行 anon不要认证 authc需要认证,一般情况下 /*放在最下面-->
<!--加载静态资源 /js/** = anon-->
/login.html = anon
/toLogin = anon
/testRole2=roles["admin"]
/testRole3=rolesOr["admin","admin1"]
/testPerms =perms[user:select]
/testPerms1 =perms["user:select","user:update"]
<!-- 请求logout地址(这个地址可以不存在),shiro去清除session-->
<!--/logout = logout-->
/** = authc
</value>
</property>
<property name="filters">
<util:map>
<entry key="rolesOr" value-ref="rolesOrFilter"/>
</util:map>
</property>
</bean>
在过滤器中配路径,路径多会造成过滤器配置太繁琐了,可以使用注解
注解
对controller开启AOP
在springmvc.xml中配置shiro注解支持,可在controller方法中使用shiro注解配置权限
<aop:config proxy-target-class="true"/>
<bean class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>
权限注解控制
- @RequiresPermissions("")//权限
- @RequiresRoles()//角色
@RequestMapping(value = "/massages/{id}")
@RequiresPermissions("massage:select")//该方法必须有massage:select权限才能访问
public String findMassageById(@PathVariable Integer id,ModelMap modelMap){
jsp标签控制
Jsp页面添加:
<%@ tagliburi=“http://shiro.apache.org/tags” prefix=“shiro” %>
| 标签名称 | 标签条件(均是显示标签内容) |
|---|---|
| <shiro:authenticated > | 登录之后 |
| <shiro:notAuthenticated > | 用户在没有RememberMe时 |
| <shiro:user > | 用户在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”/> | 显示用户身份中的属性值 |
jsp页面添加标签
例:没有认证——出现注册、登录按钮
<shiro:notAuthenticated>
<button >注册</button>
<button >登录</button>
</shiro:notAuthenticated>

本文详细介绍了Apache Shiro框架中的权限管理配置,包括过滤器设置、注解使用及JSP标签控制,帮助开发者掌握Shiro的权限控制机制。
960

被折叠的 条评论
为什么被折叠?



