• springsecurity验证码实现


    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,这种比较复杂,但是更为合理,也是我现在正在用的。 



  • 相关阅读:
    codeforces round 512 F. Putting Boxes Together 树状数组维护区间加权平均数
    sgu 110 射线关于球的反射光线
    快速读模板
    HDU-3506 二维四边形不等式
    BZOJ 1563 四边形不等式
    HIT Summer Day17 计算几何初步 题解
    IME Starters Try-outs 2018 C. China Adventures
    IME Starters Try-outs 2018 J. JHADCBEIGF
    ACM International Collegiate Programming Contest, Amman Collegiate Programming Contest (2018) GYM 100810 K. League of Demacia
    Codeforces Round #493 (Div. 1) C. Sky Full of Stars
  • 原文地址:https://www.cnblogs.com/ncepu/p/13695055.html
Copyright © 2020-2023  润新知