结合shiro 的图形验证码生成

来源:互联网 发布:ubuntu lts 编辑:程序博客网 时间:2024/04/30 19:39

见:http://jinnianshilongnian.iteye.com/blog/2046041

见:http://blog.csdn.net/topwqp/article/details/8217794




在做用户登录功能时,很多时候都需要验证码支持,验证码的目的是为了防止机器人模拟真实用户登录而恶意访问,如暴力破解用户密码/恶意评论等。目前也有一些验证码比较简单,通过一些OCR工具就可以解析出来;另外还有一些验证码比较复杂(一般通过如扭曲、加线条/噪点等干扰)防止OCR工具识别;但是在中国就是人多,机器干不了的可以交给人来完成,所以在中国就有很多打码平台,人工识别验证码;因此即使比较复杂的如填字、算数等类型的验证码还是能识别的。所以验证码也不是绝对可靠的,目前比较可靠还是手机验证码,但是对于用户来说相对于验证码还是比较麻烦的。

 

对于验证码图片的生成,可以自己通过如Java提供的图像API自己去生成,也可以借助如JCaptcha这种开源Java类库生成验证码图片;JCaptcha提供了常见的如扭曲、加噪点等干扰支持。

 

一、添加JCaptcha依赖 

Java代码  收藏代码
  1. <dependency>  
  2.     <groupId>com.octo.captcha</groupId>  
  3.     <artifactId>jcaptcha</artifactId>  
  4.     <version>2.0-alpha-1</version>  
  5. </dependency>  
  6. <dependency>  
  7.     <groupId>com.octo.captcha</groupId>  
  8.     <artifactId>jcaptcha-integration-simple-servlet</artifactId>  
  9.     <version>2.0-alpha-1</version>  
  10.     <exclusions>  
  11.         <exclusion>  
  12.             <artifactId>servlet-api</artifactId>  
  13.             <groupId>javax.servlet</groupId>  
  14.         </exclusion>  
  15.     </exclusions>  
  16. </dependency>   

com.octo.captcha . jcaptcha 提供了jcaptcha 核心;而jcaptcha-integration-simple-servlet提供了与Servlet集成。

 

二、GMailEngine

来自https://code.google.com/p/musicvalley/source/browse/trunk/musicvalley/doc/springSecurity/springSecurityIII/src/main/java/com/spring/security/jcaptcha/GMailEngine.java?spec=svn447&r=447(目前无法访问了),仿照JCaptcha2.0编写类似GMail验证码的样式;具体请参考com.github.zhangkaitao.shiro.chapter22.jcaptcha.GMailEngine。

 

三、MyManageableImageCaptchaService

提供了判断仓库中是否有相应的验证码存在。 

Java代码  收藏代码
  1. public class MyManageableImageCaptchaService extends   
  2.   DefaultManageableImageCaptchaService {   
  3.     public MyManageableImageCaptchaService(  
  4.       com.octo.captcha.service.captchastore.CaptchaStore captchaStore,        
  5.       com.octo.captcha.engine.CaptchaEngine captchaEngine,  
  6.       int minGuarantedStorageDelayInSeconds,   
  7.       int maxCaptchaStoreSize,   
  8.       int captchaStoreLoadBeforeGarbageCollection) {  
  9.         super(captchaStore, captchaEngine, minGuarantedStorageDelayInSeconds,   
  10.             maxCaptchaStoreSize, captchaStoreLoadBeforeGarbageCollection);  
  11.     }  
  12.     public boolean hasCapcha(String id, String userCaptchaResponse) {  
  13.         return store.getCaptcha(id).validateResponse(userCaptchaResponse);  
  14.     }  
  15. }  

  

 

四、JCaptcha工具类

提供相应的API来验证当前请求输入的验证码是否正确。  

Java代码  收藏代码
  1. public class JCaptcha {  
  2.     public static final MyManageableImageCaptchaService captchaService  
  3.             = new MyManageableImageCaptchaService(new FastHashMapCaptchaStore(),   
  4.                             new GMailEngine(), 18010000075000);  
  5.     public static boolean validateResponse(  
  6.         HttpServletRequest request, String userCaptchaResponse) {  
  7.         if (request.getSession(false) == nullreturn false;  
  8.         boolean validated = false;  
  9.         try {  
  10.             String id = request.getSession().getId();  
  11.             validated =   
  12.                 captchaService.validateResponseForID(id, userCaptchaResponse)  
  13.                             .booleanValue();  
  14.         } catch (CaptchaServiceException e) {  
  15.             e.printStackTrace();  
  16.         }  
  17.         return validated;  
  18.     }   
  19.     public static boolean hasCaptcha(  
  20.         HttpServletRequest request, String userCaptchaResponse) {  
  21.         if (request.getSession(false) == nullreturn false;  
  22.         boolean validated = false;  
  23.         try {  
  24.             String id = request.getSession().getId();  
  25.             validated = captchaService.hasCapcha(id, userCaptchaResponse);  
  26.         } catch (CaptchaServiceException e) {  
  27.             e.printStackTrace();  
  28.         }  
  29.         return validated;  
  30.     }  
  31. }   

validateResponse():验证当前请求输入的验证码否正确;并从CaptchaService中删除已经生成的验证码;

hasCaptcha():验证当前请求输入的验证码是否正确;但不从CaptchaService中删除已经生成的验证码(比如Ajax验证时可以使用,防止多次生成验证码);

 

五、JCaptchaFilter

用于生成验证码图片的过滤器。  

Java代码  收藏代码
  1. public class JCaptchaFilter extends OncePerRequestFilter {  
  2.     protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {  
  3.   
  4.         response.setDateHeader("Expires", 0L);  
  5.         response.setHeader("Cache-Control""no-store, no-cache, must-revalidate");  
  6.         response.addHeader("Cache-Control""post-check=0, pre-check=0");  
  7.         response.setHeader("Pragma""no-cache");  
  8.         response.setContentType("image/jpeg");  
  9.         String id = request.getRequestedSessionId();  
  10.         BufferedImage bi = JCaptcha.captchaService.getImageChallengeForID(id);  
  11.         ServletOutputStream out = response.getOutputStream();  
  12.         ImageIO.write(bi, "jpg", out);  
  13.         try {  
  14.             out.flush();  
  15.         } finally {  
  16.             out.close();  
  17.         }  
  18.     }  
  19. }   

CaptchaService使用当前会话ID当作key获取相应的验证码图片;另外需要设置响应内容不进行浏览器端缓存。 

 

Java代码  收藏代码
  1. <!-- 验证码过滤器需要放到Shiro之后 因为Shiro将包装HttpSession 如果不,可能造成两次的sesison id 不一样 -->  
  2. <filter>  
  3.   <filter-name>JCaptchaFilter</filter-name>  
  4.   <filter-class>   
  5.     com.github.zhangkaitao.shiro.chapter22.jcaptcha.JCaptchaFilter  
  6.   </filter-class>  
  7.   </filter>  
  8.   <filter-mapping>  
  9.     <filter-name>JCaptchaFilter</filter-name>  
  10.     <url-pattern>/jcaptcha.jpg</url-pattern>  
  11. </filter-mapping>   

这样就可以在页面使用/jcaptcha.jpg地址显示验证码图片。

 

六、JCaptchaValidateFilter

用于验证码验证的Shiro过滤器。  

Java代码  收藏代码
  1. public class JCaptchaValidateFilter extends AccessControlFilter {  
  2.     private boolean jcaptchaEbabled = true;//是否开启验证码支持  
  3.     private String jcaptchaParam = "jcaptchaCode";//前台提交的验证码参数名  
  4.     private String failureKeyAttribute = "shiroLoginFailure"//验证失败后存储到的属性名  
  5.     public void setJcaptchaEbabled(boolean jcaptchaEbabled) {  
  6.         this.jcaptchaEbabled = jcaptchaEbabled;  
  7.     }  
  8.     public void setJcaptchaParam(String jcaptchaParam) {  
  9.         this.jcaptchaParam = jcaptchaParam;  
  10.     }  
  11.     public void setFailureKeyAttribute(String failureKeyAttribute) {  
  12.         this.failureKeyAttribute = failureKeyAttribute;  
  13.     }  
  14.     protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {  
  15.         //1、设置验证码是否开启属性,页面可以根据该属性来决定是否显示验证码  
  16.         request.setAttribute("jcaptchaEbabled", jcaptchaEbabled);  
  17.   
  18.         HttpServletRequest httpServletRequest = WebUtils.toHttp(request);  
  19.         //2、判断验证码是否禁用 或不是表单提交(允许访问)  
  20.         if (jcaptchaEbabled == false || !"post".equalsIgnoreCase(httpServletRequest.getMethod())) {  
  21.             return true;  
  22.         }  
  23.         //3、此时是表单提交,验证验证码是否正确  
  24.         return JCaptcha.validateResponse(httpServletRequest, httpServletRequest.getParameter(jcaptchaParam));  
  25.     }  
  26.     protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {  
  27.         //如果验证码失败了,存储失败key属性  
  28.         request.setAttribute(failureKeyAttribute, "jCaptcha.error");  
  29.         return true;  
  30.     }  
  31. }  

 

七、MyFormAuthenticationFilter

用于验证码验证的Shiro拦截器在用于身份认证的拦截器之前运行;但是如果验证码验证拦截器失败了,就不需要进行身份认证拦截器流程了;所以需要修改下如FormAuthenticationFilter身份认证拦截器,当验证码验证失败时不再走身份认证拦截器。 

Java代码  收藏代码
  1. public class MyFormAuthenticationFilter extends FormAuthenticationFilter {  
  2.     protected boolean onAccessDenied(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {  
  3.         if(request.getAttribute(getFailureKeyAttribute()) != null) {  
  4.             return true;  
  5.         }  
  6.         return super.onAccessDenied(request, response, mappedValue);  
  7.     }  
  8. }   

即如果之前已经错了,那直接跳过即可。

 

八、spring-config-shiro.xml       

Java代码  收藏代码
  1. <!-- 基于Form表单的身份验证过滤器 -->  
  2. <bean id="authcFilter"   
  3.   class="com.github.zhangkaitao.shiro.chapter22.jcaptcha.MyFormAuthenticationFilter">  
  4.     <property name="usernameParam" value="username"/>  
  5.     <property name="passwordParam" value="password"/>  
  6.     <property name="rememberMeParam" value="rememberMe"/>  
  7.     <property name="failureKeyAttribute" value="shiroLoginFailure"/>  
  8. </bean>  
  9. <bean id="jCaptchaValidateFilter"   
  10.   class="com.github.zhangkaitao.shiro.chapter22.jcaptcha.JCaptchaValidateFilter">  
  11.     <property name="jcaptchaEbabled" value="true"/>  
  12.     <property name="jcaptchaParam" value="jcaptchaCode"/>  
  13.     <property name="failureKeyAttribute" value="shiroLoginFailure"/>  
  14. </bean>  
  15. <!-- Shiro的Web过滤器 -->  
  16. <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">  
  17.     <property name="securityManager" ref="securityManager"/>  
  18.     <property name="loginUrl" value="/login"/>  
  19.     <property name="filters">  
  20.         <util:map>  
  21.             <entry key="authc" value-ref="authcFilter"/>  
  22.             <entry key="sysUser" value-ref="sysUserFilter"/>  
  23.             <entry key="jCaptchaValidate" value-ref="jCaptchaValidateFilter"/>  
  24.         </util:map>  
  25.     </property>  
  26.     <property name="filterChainDefinitions">  
  27.         <value>  
  28.             /static/** = anon  
  29.             /jcaptcha* = anon  
  30.             /login = jCaptchaValidate,authc  
  31.             /logout = logout  
  32.             /authenticated = authc  
  33.             /** = user,sysUser  
  34.         </value>  
  35.     </property>  
  36. </bean>  

 

九、login.jsp登录页面

Java代码  收藏代码
  1. <c:if test="${jcaptchaEbabled}">  
  2.     验证码:  
  3.     <input type="text" name="jcaptchaCode">  
  4. <img class="jcaptcha-btn jcaptcha-img"   
  5. src="${pageContext.request.contextPath}/jcaptcha.jpg" title="点击更换验证码">  
  6.     <a class="jcaptcha-btn" href="javascript:;">换一张</a>  
  7.     <br/>  
  8. </c:if>   

根据jcaptchaEbabled来显示验证码图片。

 

十、测试

输入http://localhost:8080/chapter22将重定向到登录页面;输入正确的用户名/密码/验证码即可成功登录,如果输入错误的验证码,将显示验证码错误页面: 


  

 

示例源代码:https://github.com/zhangkaitao/shiro-example;




十一、另附讲解:

先说明错误原因:
用<a href="http://lib.csdn.net/base/javaee" class="replace_word" title="Java EE知识库" target="_blank" >spring</a>安全拦截器进行验证码的验证的时候抛出异常。
throw new RuntimeException("captcha validation failed due to exception", cse);前台提交数据后跳转到如下方法:


  1. package com.davidstudio.gbp.core.security.jcaptcha;  
  2.   
  3. import org.acegisecurity.captcha.CaptchaServiceProxy;  
  4.   
  5. import org.apache.log4j.Logger;  
  6.   
  7. import com.octo.captcha.service.CaptchaService;  
  8. import com.octo.captcha.service.CaptchaServiceException;  
  9.   
  10. /** 
  11.  * 调用CaptchaService类,完jcaptcha的验证过程 
  12.  *  
  13.  * 
  14.  *  
  15.  * 
  16.  */  
  17. public class JCaptchaServiceProxyImpl implements CaptchaServiceProxy {  
  18.   
  19.     /** 
  20.      * Logger for this class 
  21.      */  
  22.     private static final Logger logger = Logger.getLogger(JCaptchaServiceProxyImpl.class);  
  23.   
  24.     private CaptchaService jcaptchaService;  
  25.   
  26.     public boolean validateReponseForId(String id, Object response) {  
  27.         if (logger.isDebugEnabled()) {  
  28.             logger.debug("validating captcha response");  
  29.         }  
  30.    
  31.         try {  
  32.             boolean isHuman = false;  
  33.               
  34.             isHuman = jcaptchaService.validateResponseForID(id, response).booleanValue();  
  35.               
  36.             if (isHuman) {  
  37.                 if (logger.isDebugEnabled()) {  
  38.                     logger.debug("captcha passed");  
  39.                 }  
  40.             } else {  
  41.                 if (logger.isDebugEnabled()) {  
  42.                     logger.debug("captcha failed");  
  43.                 }  
  44.             }  
  45.             return isHuman;  
  46.               
  47.         } catch (CaptchaServiceException cse) {  
  48.             // fixes known bug in JCaptcha  
  49.             logger.warn("captcha validation failed due to exception", cse);  
  50.             throw new RuntimeException("captcha validation failed due to exception", cse);  
  51.         }  
  52.     }  
  53.   
  54.     public void setJcaptchaService(CaptchaService jcaptchaService) {  
  55.         this.jcaptchaService = jcaptchaService;  
  56.     }  
  57. }  



设置断点debug改语句不能顺利执行 

  1. jcaptchaService.validateResponseForID(id, response).booleanValue();  


查了网上的资料,这个方法的作用是: 根据HttpSession的 sessionId进行验证码的验证,原理是这样的,页面生成的验证码是通过Spring中的配置生成的,查了一下配置:

  1. <bean id="security.filter.manager" class="org.acegisecurity.util.FilterChainProxy">  
  2.         <property name="filterInvocationDefinitionSource">  
  3.             <value>  
  4.                 PATTERN_TYPE_APACHE_ANT  
  5.                 /**=security.filter.channel,security.filter.sessionIntegration,security.filter.logout,security.filter.thsso,security.filter.jcaptcha,security.filter.jcaptchachannel,security.filter.formAuth,security.filter.requestWrap,security.filter.exceptionTranslation,security.filter.filterInvocation  
  6.             </value>  
  7.         </property>  
  8.     </bean> 


这是一个过滤器链,其中登录的时候会进行如下过滤操作,

security.filter.channel,security.filter.sessionIntegration,security.filter.logout,security.filter.thsso,security.filter.jcaptcha,security.filter.jcaptchachannel,security.filter.formAuth,security.filter.requestWrap,security.filter.exceptionTranslation,security.filter.filterInvocation

一般配置的顺序不能变,因为这是这些配置定义了用户登录的一套认证机制。

看了一下命名还算规范,其中涉及到验证码的过滤:security.filter.jcaptcha

查了一下这个验证码的引用配置:

  1. <!-- jcaptacha过滤器 -->  
  2.     <bean id="security.filter.jcaptcha"  
  3.         class="org.acegisecurity.captcha.CaptchaValidationProcessingFilter">  
  4.         <property name="captchaService" ref="security.captcha.serviceproxy" />  
  5.         <property name="captchaValidationParameter" value="j_captcha_response" />  
  6.     </bean>  
  7.     <bean id="security.captcha.serviceproxy"  
  8.         class="com.davidstudio.gbp.core.security.jcaptcha.JCaptchaServiceProxyImpl">  
  9.         <property name="jcaptchaService" ref="security.captcha.service" />  
  10.     </bean>  
  11.     <bean id="security.captcha.service"  
  12.         class="com.octo.captcha.service.image.DefaultManageableImageCaptchaService">  
  13.         <constructor-arg type="com.octo.captcha.service.captchastore.CaptchaStore" index="0">  
  14.             <bean class="com.octo.captcha.service.captchastore.FastHashMapCaptchaStore" />  
  15.         </constructor-arg>  
  16.         <constructor-arg type="com.octo.captcha.engine.CaptchaEngine" index="1">  
  17.             <bean class="com.davidstudio.gbp.core.security.jcaptcha.CaptchaEngine" />  
  18.         </constructor-arg>  
  19.         <constructor-arg index="2">  
  20.             <value>180</value>  
  21.         </constructor-arg>  
  22.         <constructor-arg index="3">  
  23.             <value>100000</value>  
  24.         </constructor-arg>  
  25.         <constructor-arg index="4">  
  26.             <value>75000</value>  
  27.         </constructor-arg>  
  28.     </bean>  



通过bean配置反复引用。

刚开始以为SecurityContext没有创建,查了一下配置也创建了:


  1. <!--  session整合过滤器。自动将用户身份信息存放在session里。 -->  
  2. <bean id="security.filter.sessionIntegration"  
  3.     class="org.acegisecurity.context.HttpSessionContextIntegrationFilter">  
  4.     <property name="context" value="org.acegisecurity.captcha.CaptchaSecurityContextImpl" />  
  5. </bean>  

 copy

  仔细看了一下这个方法的作用:

  1. jcaptchaService.validateResponseForID(id, response).booleanValue();  

id就是httpSession的Id,response是从页面获得的输入的验证码,当调用这个方法的时候,根据httpSession的id找到相应的验证码,如果有sessionId并且sessionId对应的验证码和输入的验证码(这里就是response)一致的时候返回true,也就是用户通过了验证。

有一个疑问,验证码是怎么生成的?又怎么和httpSession进行绑定的?其实这套理论是可行的,当用户第一次访问页面的时候会生成一个sessionId,页面生成有验证码,关于验证码的生成,下面会进行介绍。就是画一个图片以留的方式显示到页面而已。用户访问的时候有一个对应的验证码和sessionId相对应。

如果验证码不清楚,点击换一张,因为浏览器没有关闭,sessionId依然是那个sessionId,只需要更新生成的验证码的值即可,这样就做到了一个sessionId和一个验证码进行绑定了,这个过程在生成验证码的过程中就发生了。

如果用户再次提交登录信息,其中的sessionId没有变,验证码是最新生成的验证码并且和sessionId进行了绑定,这样就可以调用:


  1. jcaptchaService.validateResponseForID(id, response).booleanValue(); 这个条件进行验证码的验证了,当然了验证码验证前面还可以有很多过滤器认证,比如说对用户名和密码的验证等等。形成一套的链式认证!  

      然而还有一个疑惑,这个sessionId是怎么和验证码进行绑定的呢?又是怎样进行存储的呢?

 我们看一下内存:


调用这段代码的时候内存中有sessionId和response验证码的值:

下面是验证码生成的线程中内存的状态:


由内存的状态可以看出和配置文件是一致的,首先调用了com.davidstudio.gbp.core.security.jcaptcha.JCaptchaServiceProxyImpl

这个代理实现,这个代理实现类 又去调用com.octo.captcha.service.image.DefaultManageableImageCaptchaService

这个类才是生成验证码的类:查下spring这个类的源码如下:



传入的参数都有相应的说明,其中这个类继承了AbstractManageableImageCaptchaService  


继续深入到这个类中看个究竟:

这个类中果然有我们想要的方法:




  相应的通过store.getCaptcha(ID)通过这个ID获得和这个sessionId匹配的验证码,再调用vilidateResponse方法进行验证,如果和输入的验证码相同就验证通过了。

验证通过后就把这个sessionId删除了,如果你再次登录,输入验证码的时候是同一个逻辑,之所以删除了这个ID我想是有好处的:

           原因如下,如果不进行删除,随着的登录访问用户的过多,hashMap中的值会越来越多,这样以后再进行验证的时候速度和效率都会受到印象,如果删除了这个sessionId,这样这个store中的hashMap只是存储了当前正在准备登录的sessionId和相应的验证码!这样效率就大大提高了,如果有10万个人同时登录,都不是问题!

       通过这个方法的调用我们就知道了sessionId是怎么和验证码绑定存储在hashMap中的!让我们进入源码验证一下:




上面就是CaptchaStore接口的实现类MapCaptchaStore,其中定义了一个hashMap,通过storeCaptcha(String id,Captcha captcha)方法来存储sessionId和captcha的键值对,这是进入登录页面生成的时候调用的方法,当进行验证的时候就需要hasCaptcha(String ID)方法和



但是我们是调用了

[java] view plain copy
  1. MapCaptchaStore 的子类FastHashMapCaptchaStore来存储信息的:同样看FastHashMapCaptchaStore这个类:

  2.    17   public class FastHashMapCaptchaStore extends MapCaptchaStore {  
  3.    18       public FastHashMapCaptchaStore() {  
  4.    19           this.store = new FastHashMap();  
  5.    20       }  
  6.    21   }

  7. 这就是这个类的全部了,再看一下FastHashMap类:  

  8. public class FastHashMap extends HashMap {  
  9.    67     
  10.    68       /** 
  11.    69        * The underlying map we are managing. 
  12.    70        */  
  13.    71       protected HashMap map = null;  
  14.    72     
  15.    73       /** 
  16.    74        * Are we currently operating in "fast" mode? 
  17.    75        */  
  18.    76       protected boolean fast = false;  
  19.    77     
  20.    78       // Constructors  
  21.    79       // ----------------------------------------------------------------------  
  22.    80     
  23.    81       /** 
  24.    82        * Construct an empty map. 
  25.    83        */  
  26.    84       public FastHashMap() {  
  27.    85           super();  
  28.    86           this.map = new HashMap();  
  29.    87       }  
  30.    
  31. 这个类是HashMap的一个扩展,里面有两种方式操作,一种是快速的不同步,一种是同步的操作!  

  32. 显然FastHashMapCaptchaStore就是一个HashMap。验证码的实现在这个类中:

  33.    18    * Base implementation of the ImageCaptchaService.  
  34.    19    *  
  35.    20    * @author <a href="mailto:mag@jcaptcha.net">Marc-Antoine Garrigue</a>  
  36.    21    * @version 1.0  
  37.    22    */  
  38.    23   public abstract class AbstractManageableImageCaptchaService extends AbstractManageableCaptchaService  
  39.    24           implements ImageCaptchaService {  
  40.    25     
  41.    26       protected AbstractManageableImageCaptchaService(CaptchaStore captchaStore,  
  42.    27                                                       com.octo.captcha.engine.CaptchaEngine captchaEngine,  
  43.    28                                                       int minGuarantedStorageDelayInSeconds,  
  44.    29                                                       int maxCaptchaStoreSize,  
  45.    30                                                       int captchaStoreLoadBeforeGarbageCollection) {  
  46.    31           super(captchaStore, captchaEngine,  
  47.    32                   minGuarantedStorageDelayInSeconds, maxCaptchaStoreSize,  
  48.    33                   captchaStoreLoadBeforeGarbageCollection);  
  49.    34       }


  50.    73       protected Object getChallengeClone(Captcha captcha) {  
  51.    74           BufferedImage challenge = (BufferedImage) captcha.getChallenge();  
  52.    75           BufferedImage clone = new BufferedImage(challenge.getWidth(), challenge.getHeight(), challenge.getType());  
  53.    76     
  54.    77           clone.getGraphics().drawImage(challenge, 00, clone.getWidth(), clone.getHeight(), null);  
  55.    78           clone.getGraphics().dispose();  
  56.    79     
  57.    80     
  58.    81           return clone;  
  59.    82       }

  60. 在这个类中,只是定义了一种,Captcha也是一种接口。  
  61. 可以到内存中看一看有木有那个hashMap
  62. <span style="white-space:pre"><img src="http://img.my.csdn.net/uploads/201211/23/1353676134_4969.png" alt="">    </span>

  63. 内存中清楚显示了hashTable中的key和value,这样就证明验证码生成成功。但是为什么每次验证都是报错呢?
  64. 后来无奈看了看发送到 sessionId在hashMap中是否有,结果是不一样,就是再hashMap中没有,为什么?不是每一次在验证码生成的时候都把sessionId放进去了吗?为什么会不一样呢?

  65. 原因其实很简单,就是当点击登陆的时候服务器又给分配了一个sessionId,这样就和以前的sessionId不一样了,在hashMap中就找不到对应的验证码了。
  66. 原则上讲服务器在第一次访问的时候会给用户分配一个不重复的sessionId,如果服务器的session不超时就不会再给用户分配sessionId了,减少给服务器的压力,也带来了友好的体验。但是我的两次sessiId为什么不一样呢?

  67.  后来通过fiddler2这个软件(这个软件好强大可以获得发送到form表单的内容,甚至可以修改),可以看到本地存储的cookie,但是cookie是空的,就是nodata,汗啊,难怪每次都分配不同的sessionId,服务器怎么判断每次提交过去的是同一个用户呢?

  68. 通过sessionId,服务器会在客户端把sessionId写在Cookie中,这样用户再次提交请求的时候,服务器如果在内存中找到用户cookie中的sessionId而且没有超时,就不再重新分配sessionId,我看了下IE浏览器,cookie被禁止了,难怪每次都是一个新的sessionId,验证码就无法验证。就报错了。
  69. 学习中应该多去看源码,分析源码设计理念。

0 0