1       人机验证码

1.1        配置默认的人机验证码功能

Marmot Framework中提供了默认的人机验证码的功能,只要访问/authentication.getCaptchaImage.d就可以获得人机验证码的图片资源。例如:

         人机验证码_Marmot

默认的人机验证处理器是org.marmot.framework.security.captcha.JCaptchaImageProcessor,它提供了下列一些属性供用户配置:

§           width 图片宽度

§           height 图片高度

§           minFontSize 最小字体尺寸

§           maxFontSize最大字体尺寸

§           chars 字符范围(etc:1234567890

§           charNumber 字符数量

§           p_w_picpathType 图像类型(gif/jpg/png

可以通过改变Spring配置文件中的设置来改变人机验证图片的生成规则,例如下面的配置将生成6位的数字验证码:

  1. <bean id="marmot.captchaProcessor" class="org.marmot.framework.security.captcha.JCaptchaImageProcessor"> 
  2.     <property name="chars" value="1234567890" /> 
  3.     <property name="charNumber" value="6" /> 
  4. </bean> 

 

1.2        与登录机制的结合方式

回顾一下org.marmot.framework.security.Authenticator提供的登录接口:

 

  1. /** 
  2. * 登录系统  
  3. * @param user 用户名 
  4. * @param password 口令 
  5. * @param attributies 附加的登录信息 
  6. * @throws Exception 
  7. */ 
  8. public void login(String user, String password, Object attributies) 
  9.     throws Exception;  

 

login方法中没有提供验证码的参数,因此在调用此login方法时需要将用户输入的验证码存放在attributies中,以下是取自org.marmot.framework.main.AuthenticationBackBean中的一段代码:

 

  1. … … … 
  2. Map attributies = new HashMap(); 
  3. attributies.put("captchaCode", inParams.get("captchaCode")); 
  4. getAuthenticator().login(user, password, attributies); 
  5. … … … 

 

可见,在Marmot Framework的默认实现中,框架会自动的取出名为captchaCode的参数作为人机验证码对其进行验证。所以,我们只要保证登录界面以captchaCode作为验证码的参数就可以不必再对其什么特殊的处理了。

 

 

1.3     扩展自己的人机验证处理器

如果有需要,开发者也完全可以根据自己的需要来实现自己的人机验证处理器,Marmot Framework中提供的人机验证处理器的接口如下:

 

  1. package org.marmot.framework.security.captcha; 
  2.  
  3. import javax.servlet.http.HttpServletRequest; 
  4. import javax.servlet.http.HttpServletResponse; 
  5.  
  6. /** 
  7.  * 验证码处理器的接口.  
  8.  * @author Benny Bao 
  9.  */ 
  10. public interface CaptchaProcessor { 
  11.     /** 
  12.      * 向response中输出验证码的人机识别测试信息 
  13.      * (completely automated public Turing test to tell computers and humans apart).  
  14.      * @param request 
  15.      * @param response 
  16.      * @throws Exception 
  17.      */ 
  18.     public void outputCaptcha(HttpServletRequest request, 
  19.             HttpServletResponse response) throws Exception; 
  20.  
  21.     /** 
  22.      * 验证captchaCode参数中的信息是否与之前输出的人机识别测试信息相匹配.  
  23.      * @param request 
  24.      * @param captchaCode 用户填写的验证码 
  25.      * @return 是否通过验证 
  26.      */ 
  27.     public boolean validateCaptcha(HttpServletRequest request, 
  28.             String captchaCode); 

在WEB-INF/configs/marmot-framework-context.xml进入如下配置:

  1. <bean id="marmot.captchaProcessor" 
  2.     class="org.marmot.framework.security.captcha.JCaptchaImageProcessor" /> 

111