`
QiaoDuanni
  • 浏览: 135279 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

spring security添加验证码处理

阅读更多
public class CustomAuthenticationProcessingFilter extends AuthenticationProcessingFilter {

public Authentication attemptAuthentication(HttpServletRequest httpServletRequest) throws AuthenticationException {
     
        if (校验验证码不通过) {
            String username = obtainUsername(httpServletRequest);
            httpServletRequest.getSession().setAttribute(SPRING_SECURITY_LAST_USERNAME_KEY, username);
            // 用户输入的值与看到的不一致,抛出异常
            throw new AuthCodeValidationException("验证码输入不正确");
        }
     
        return super.attemptAuthentication(httpServletRequest);
    }


}

配置文件中,重新定义和替换authenticationProcessingFilter
 
  <bean id="authenticationProcessingFilter" class="com.hm.core.security.CustomAuthenticationProcessingFilter">
        <s:custom-filter position="AUTHENTICATION_PROCESSING_FILTER" />
        <property name="authenticationManager" ref="authenticationManager" />
        <property name="authenticationFailureUrl" value="/login.jsp?login_error=1" />
        <property name="defaultTargetUrl" value="/" />
        <property name="rememberMeServices" ref="rememberMeServices" />
    </bean>

其中s命名空间是指
    xmlns:s="http://www.springframework.org/schema/security"
分享到:
评论
3 楼 QiaoDuanni 2010-03-29  
看文章分类.只是资料存档.具体怎么做扒源代码去吧
2 楼 hanjiangit 2010-03-26  
lengyue 写道
楼主,如果要在首页上提示怎么办呀.我做完提示用户名和密码输入错时,也提示验证码错误的.
我在验证码错误时返回login_error为0
密码和用户错误时返回login_error为1
便是无论密码和用户错误,还是验证码错误都提示验证码错误.能发一个例子吗?

在页面上怎么获取 'login_error' 的值?
1 楼 lengyue 2009-07-13  
楼主,如果要在首页上提示怎么办呀.我做完提示用户名和密码输入错时,也提示验证码错误的.
我在验证码错误时返回login_error为0
密码和用户错误时返回login_error为1
便是无论密码和用户错误,还是验证码错误都提示验证码错误.能发一个例子吗?

相关推荐

Global site tag (gtag.js) - Google Analytics