springMVC中快速配置shiro
来源:互联网 发布:网络购物诈骗定义 编辑:程序博客网 时间:2024/06/05 16:58
1.在web.xml文件中配置shiro的过滤器,以拦截项目内的访问
<!-- shiro过滤器定义 --><filter> <filter-name>shiroFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <init-param> <!-- 该值缺省为false,表示生命周期由SpringApplicationContext管理,设置为true则表示由ServletContainer管理 --> <param-name>targetFilterLifecycle</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
2.在spring 的配置文件中配置shiro的相关项
<!-- 保证实现了Shiro内部lifecycle函数的bean执行 --> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> <!-- 缓存管理 --> <bean id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager"></bean> <!-- 自定义Realm --><bean id="myRealm" class="com.bf.planner.realm.MyRealm"/> <!-- 自定义reaml获取令牌以及角色和权限的类 --><!-- 安全管理器 --><bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="myRealm"/> </bean> <!-- Shiro过滤器 --><bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <!-- Shiro的核心安全接口,这个属性是必须的 --> <property name="securityManager" ref="securityManager"/><!-- 身份认证失败,则跳转到登录页面的配置 --> <property name="loginUrl" value="/"/><!-- 权限认证失败,则跳转到指定页面 --> <property name="unauthorizedUrl" value="/unauthor.jsp"/> <!-- Shiro连接约束配置,即过滤链的定义 --> <property name="filterChainDefinitions"> <value>/** = authc<!-- 配置访问项目下的所有路径均需要验证令牌(即登录) --></value> </property></bean>
public class MyRealm extends AuthorizingRealm{@Autowiredprivate LoginService loginService;/** * 用户授权 */@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {String userName=(String)principals.getPrimaryPrincipal();//获取当前登录用户的用户名SimpleAuthorizationInfo authorizationInfo=new SimpleAuthorizationInfo();//新建权限的对象authorizationInfo.setRoles(loginService.getRoles(userName));//设置此用户对应的角色authorizationInfo.setStringPermissions(loginService.getPermissions(userName));//设置此用户对应的权限return authorizationInfo;}/** * 用户登录 */@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {String user_name=(String)token.getPrincipal();//获取登录者的用户名User user=loginService.getUser(user_name);//根据用户名查出数据库中所对应的用户信息if(user!=null){AuthenticationInfo authcInfo=new SimpleAuthenticationInfo(user.getUser_name(),user.getPassword(),"xx");//设置此用户的令牌,即设置正确的用户名与密码return authcInfo;}else{return null;}}}
@RequestMapping("/login")@ResponseBodypublic ResultInfo login(User user){ResultInfo resultInfo=new ResultInfo();Subject subject=SecurityUtils.getSubject();UsernamePasswordToken token=new UsernamePasswordToken(user.getUser_name(), user.getPassword());try{subject.login(token);resultInfo.setCode(true); //登录成功return resultInfo;}catch(Exception e){resultInfo.setCode(false);//登录失败,如果没有对应的用户,则会抛出异常return resultInfo;}}
5.使用shiro来控制方法的访问权限(例子):
@RequiresRoles(value={"admin","base"}, logical = Logical.OR)@RequiresPermissions(value = {"add","update"}, logical = Logical.AND)public void shiro_test(){}
上面的代码中对shiro_test方法添加了访问限制:
@RequiresRoles控制拥有admin或base角色的用户可以访问此方法
@RequiresPermissions控制拥有add和update权限的用户可以访问此方法
6.页面中使用shiro来控制页面内容的显示:
首先,需要在页面头部引入shiro支持:
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>然后,我们可以在页面中使用shiro的标签了:(<shiro:principal/>当前登录用户的用户名【即登录账号】)
<shiro:hasRole name="admin">欢迎有admin角色的用户!<shiro:principal/> <!-- 登录人 --></shiro:hasRole><shiro:hasPermission name="student:create">欢迎有student:create权限的用户!<shiro:principal/></shiro:hasPermission>
注意:从数据库中获取的角色集合和权限集合中不允许有空值
0 0
- springMVC中快速配置shiro
- spring+springmvc+shiro配置
- SpringMVC+Shiro配置
- shiro结合springMVC配置相关
- springmvc+shiro简单配置及作用(shiro部分)
- shiro+redis+springMvc整合配置及说明
- shiro+redis+springMvc整合配置及说明
- springmvc + shiro 配置(二、其他配置文件)
- springmvc配置shiro进行权限控制
- shiro+redis+springMvc整合配置及说明
- SpringMVC整合Shiro与filterChainDefinitions过滤器配置
- spring-shiro-springmvc-mybatis简单配置项目
- springmvc shiro
- springmvc+shiro
- springmvc+shiro
- Shiro+SpringMVC
- shiro springmvc
- 在Spring中配置Shiro
- function闭包、定时器剖析
- Xshell连接不上虚拟机的问题和解决办法
- leecode 解题总结:368. Largest Divisible Subset
- 设计模式无限重读之接口隔离原则(ISP)
- 索引和查询优化系列之(一)——索引总结
- springMVC中快速配置shiro
- redis 使用时发现的病毒 -- AnXqV
- [学习笔记]设计模式[6]-{适配器模式&外观模式}
- 关闭“您在 /var/spool/mail/root 中有新邮件”提示
- 92. Reverse Linked List II(unsolved)
- Hadoop 用Eclipse来Mapreduce WordCount实战(1)
- python3连接MySQL数据库
- PHP常用类函数(2)Cookie
- 2017AHOI备战日志