Shiro入门-整合验证码
来源:互联网 发布:晚上醒了就睡不着 知乎 编辑:程序博客网 时间:2024/06/01 09:57
思路
shiro使用FormAuthenticationFilter进行表单认证,验证校验的功能应该加在FormAuthenticationFilter中,在认证之前进行验证码校验。
需要写FormAuthenticationFilter的子类,继承FormAuthenticationFilter,改写它的认证方法,在认证之前进行验证码校验。
自定义FormAuthenticationFilter
public class CustomFormAuthenticationFilter extends FormAuthenticationFilter { //原FormAuthenticationFilter的认证方法 @Override protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception { //在这里进行验证码的校验 //从session获取正确验证码 HttpServletRequest httpServletRequest = (HttpServletRequest) request; HttpSession session =httpServletRequest.getSession(); //取出session的验证码(正确的验证码) String validateCode = (String) session.getAttribute("validateCode"); //取出页面的验证码 //输入的验证和session中的验证进行对比 String randomcode = httpServletRequest.getParameter("randomcode"); if(randomcode!=null && validateCode!=null && !randomcode.equals(validateCode)){ //如果校验失败,将验证码错误失败信息,通过shiroLoginFailure设置到request中 httpServletRequest.setAttribute("shiroLoginFailure", "randomCodeError"); //拒绝访问,不再校验账号和密码 return true; } return super.onAccessDenied(request, response); }}
配置自定义FormAuthenticationFilter spring-shiro.xml
<!-- Shiro 的Web过滤器 --><bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager" /> <!-- loginUrl认证提交地址,如果没有认证将会请求此地址进行认证,请求此地址将由formAuthenticationFilter进行表单认证 --> <property name="loginUrl" value="/login.action" /> <!-- 认证成功统一跳转到first.action,建议不配置,shiro认证成功自动到上一个请求路径 --> <property name="successUrl" value="/first.action"/> <!-- 通过unauthorizedUrl指定没有权限操作时跳转页面--> <property name="unauthorizedUrl" value="/refuse.jsp" /> <!-- 自定义filter配置 --> <property name="filters"> <map> <!-- 将自定义 的FormAuthenticationFilter注入shiroFilter中--> <entry key="authc" value-ref="formAuthenticationFilter" /> </map> </property><!-- 自定义form认证过虑器 --><!-- 基于Form表单的身份验证过滤器,不配置将也会注册此过虑器,表单中的用户账号、密码及loginurl将采用默认值,建议配置 --><bean id="formAuthenticationFilter" class="cn.itcast.ssm.shiro.CustomFormAuthenticationFilter "> <!-- 表单中账号的input名称 --> <property name="usernameParam" value="username" /> <!-- 表单中密码的input名称 --> <property name="passwordParam" value="password" /> <!-- 记住我input的名称 --> <property name="rememberMeParam" value="rememberMe"/></bean>
在login.action对验证错误 进行解析
//登陆提交地址,和applicationContext-shiro.xml中配置的loginurl一致 @RequestMapping("login") public String login(HttpServletRequest request)throws Exception{ //如果登陆失败从request中获取认证异常信息,shiroLoginFailure就是shiro异常类的全限定名 String exceptionClassName = (String) request.getAttribute("shiroLoginFailure"); //根据shiro返回的异常类路径判断,抛出指定异常信息 if(exceptionClassName!=null){ if (UnknownAccountException.class.getName().equals(exceptionClassName)) { //最终会抛给异常处理器 throw new CustomException("账号不存在"); } else if (IncorrectCredentialsException.class.getName().equals( exceptionClassName)) { throw new CustomException("用户名/密码错误"); } else if("randomCodeError".equals(exceptionClassName)){ throw new CustomException("验证码错误 "); }else { throw new Exception();//最终在异常处理器生成未知错误 } } //此方法不处理登陆成功(认证成功),shiro认证成功会自动跳转到上一个请求路径 //登陆失败还到login页面 return "login"; }
在filter配置匿名访问验证码jsp
0 0
- Shiro入门-整合验证码
- Shiro整合Spring实现登录验证和授权之入门
- Shiro入门-shiro与spring整合
- Spring Boot Shiro权限管理--自定义 FormAuthenticationFilter验证码整合
- spring mvc整合shiro登录 权限验证
- spring mvc整合shiro登录 权限验证
- spring mvc整合shiro登录 权限验证
- spring mvc整合shiro登录 权限验证
- spring 整合 shiro 之用户验证
- Spring boot 整合shiro 实现登陆验证
- Shiro验证码检测
- Shiro 入门系列 四 (shiro缓冲整合EhCache)
- Shiro入门(2)Shiro提供的验证模块
- Shiro入门(2)Shiro提供的验证模块
- springMVC与shiro整合与自定义验证码用户类型类
- SpringBoot学习:整合shiro(验证码功能和登录次数限制功能)
- Java权限验证框架Shiro的入门
- Shiro第四篇【Shiro与Spring整合、快速入门、Shiro过滤器、登陆认证】
- python 中偏函数 partial 的使用
- Deep Learning-TensorFlow (8) CNN卷积神经网络_《TensorFlow实战》及经典网络模型(上)
- 2017跟着小虎玩着去软考--趣味好玩解析2015年下半年信息系统项目管理师上午试题56-60题
- 基于DragonBoard 410c的家庭智能环保卫士——(10)数据库python代码实现
- uikit——UIView——view hierarchy
- Shiro入门-整合验证码
- python基础知识之条件语句
- leetcode 18. 4Sum
- leetcodeOJ 326. Power of Three
- 拨钟问题
- mongodb视频课程以及spring data mongodb视频
- STL源码学习----lower_bound和upper_bound算法
- 良好的代码风格
- 深入Java基础(四)--哈希表(1)HashMap应用及源码详解