Acegi 扩展验证码认证[原创]

很多网站的用户登录,从安全上考虑.不仅需要输入用户名和密码,而且也需要输入"验证码",何为验证码这里不着解释,网上也有很多说明.下面给出在Acegi框架下的实现验证码功能的主要代码.

一.新建AuthenticationProcessingFilter 类

java 代码
  1. import net.sf.acegisecurity.Authentication;       
  2. import net.sf.acegisecurity.AuthenticationException;       
  3. import net.sf.acegisecurity.ui.webapp.AuthenticationProcessingFilter;       
  4. import javax.servlet.http.HttpServletRequest;       
  5. import ... ...       
  6.       
  7. public class ValidationCodeFilter extends AuthenticationProcessingFilter {       
  8.     public Authentication attemptAuthentication(HttpServletRequest httpServletRequest) throws AuthenticationException{       
  9.         String inputValidationCode = httpServletRequest.getParameter( "j_validation_code" );       
  10.         //从Session中取出验证码       
  11.         String ssnValidationCode = (String)httpServletRequest.getSession().getAttribute( VALIDATION_CODE );       
  12.       
  13.         if( ssnValidationCode != null && !ssnValidationCode.equals( inputValidationCode ) ){       
  14.             //用户输入的值与看到的不一致,抛出异常       
  15.             throw new ValidationCodeException( "验证码输入错误!");       
  16.         }       
  17.       
  18.         return super.attemptAuthentication( httpServletRequest ) ;       
  19.     }       
  20. }       

二.新建异常类ValidationCodeException

java 代码
  1. import net.sf.acegisecurity.AuthenticationException;   
  2.   
  3. /**  
  4.  * 验证码异常  
  5.  */  
  6. public class ValidationCodeException extends AuthenticationException {   
  7.     public ValidationCodeException(String s) {   
  8.         super(s);   
  9.     }   
  10. }   

三.修改Acegi配置文件

  1. <bean id="authenticationProcessingFilter" class="xxx.xxx.ValidationCodeFilter">  
  2.     <property name="authenticationManager"><ref local="authenticationManager"/>property>  
  3.     <property name="authenticationFailureUrl"><value>/loginFailure.htmlvalue>property>  
  4.     <property name="defaultTargetUrl"><value>/welcome.htmlvalue>property>  
  5.     <property name="filterProcessesUrl"><value>/j_security_checkvalue>property>  
  6. bean>  
xml 代码

    

   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值