springsecurity验证码实现
来源:互联网 发布:eclipse java界面开发 编辑:程序博客网 时间:2024/05/22 08:11
http://www.itwendao.com/article/detail/165400.html 基于java config 配置方式。
http://www.itdadao.com/articles/c15a754492p0.html 基于xml 配置方式,讲的比较散。
http://blog.csdn.net/jaune161/article/details/18502265 springSecurity教程有个系列,验证码实现写的简单粗暴。
http://www.cnblogs.com/softidea/p/6914279.html 对上的方法进行了汇总,摘的比较散,写的也散。没一张图。
我就再汇总一次吧!都写的散,简单,我写个特别简单的。算是给福利,如果可以,妈的别忘了点赞。
springsecurity至少现在是不支持验证码功能,以后支持不支持,不知道。以后要是支持,省去了一部分苦逼的活。
实现方法 大都这么说,有三种方法。
可以有三种方法可以实现验证码的功能
第一种
自定义一个filter,放在SpringSecurity过滤器之前,在用户登录的时候会先经过这个filter,然后在这个filter中实现对验证码进行验证的功能,这种方法不推荐,因为它已经脱离了SpringSecurity
第二种
自定义一个filter让它继承自UsernamePasswordAuthenticationFilter,然后重写attemptAuthentication方法在这个方法中实现验证码的功能,如果验证码错误就抛出一个继承自AuthenticationException的验证吗错误的异常比如(CaptchaException),然后这个异常就会被SpringSecurity捕获到并将异常信息返回到前台,这种实现起来比较简单。
重写attemptAuthentication方法的重点部分代码
@Override public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException { String requestCaptcha = request.getParameter(this.getCaptchaFieldName()); String genCaptcha = (String)request.getSession().getAttribute("code"); logger.info("开始校验验证码,生成的验证码为:"+genCaptcha+" ,输入的验证码为:"+requestCaptcha); if( !genCaptcha.equals(requestCaptcha)){ throw new CaptchaException( this.messageSource.getMessage("AbstractUserDetailsAuthenticationProvider.badCaptcha",null,"Default",null)); } return super.attemptAuthentication(request, response); }接着在配置文件中配置:
<bean id="loginFilter" class="com.zrhis.system.security.DefaultUsernamePasswordAuthenticationFilter"> <property name="authenticationManager" ref="authenticationManager"></property> <property name="authenticationSuccessHandler"> <bean class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler"> <property name="defaultTargetUrl" value="/index.jsp"></property> </bean> </property> <property name="authenticationFailureHandler"> <bean class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler"> <property name="defaultFailureUrl" value="/login.jsp"></property> </bean> </property> </bean> <!--http中加入custom-filter配置,将这个filter放在SpringSecurity的FORM_LOGIN_FILTER之前.--><custom-filter ref="loginFilter" before="FORM_LOGIN_FILTER"/>
第三种
直接替换掉SpringSecurity的UsernamePasswordAuthenticationFilter,这种比较复杂,但是更为合理,也是我现在正在用的。
- springsecurity验证码实现
- SpringSecurity实现登录认证及权限验证
- springSecurity的登录验证
- springSecurity的登录验证
- springSecurity的登录验证
- springSecurity 安全验证
- SpringBoot-Actuator-加SpringSecurity验证
- SpringSecurity-认证实现
- SpringBoot+SpringSecurity+thymeleaf 代码实现
- SpringSecurity
- SpringSecurity
- SpringSecurity
- SpringSecurity (Spring权限验证) 之 配置1
- springSecurity登录验证分析——AbstractUserDetailsAuthenticationProvider
- springSecurity登录验证分析——AbstractUserDetailsAuthenticationProvider
- springBoot+springSecurity验证密码MD5加密
- springBoot+springSecurity验证密码MD5加密
- SpringSecurity 学习记录(一)- SpringSecurity 主要的实现类
- 位运算
- 最小生成树
- 形参个数可变的知识点
- UML用例图
- C++概述
- springsecurity验证码实现
- 【ActiveMQ】ActiveMQ与Spring实现发邮件
- 读取文件内容的方法
- More is better(并查集)
- Linux与Windows有何区别
- 8.3 A
- Python 创建一个 UDP 服务
- "Accordian" Patience UVA
- ThinkPHP5 表单请求和验证