Terry.Li-彬

虚其心,可解天下之问;专其心,可治天下之学;静其心,可悟天下之理;恒其心,可成天下之业。

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  143 随笔 :: 344 文章 :: 130 评论 :: 0 Trackbacks

jsp验证码制作

 

  介绍两个功能强大的开源制作验证码组件SimpleCaptcha,JCAPTCHA。

 

  一、SimpleCaptcha搜狐邮箱注册时就使用了该组件,主页:
  http://simplecaptcha.sourceforge.net/index.html

 

example


 1.配置
  将下载的jar文件放置工程的lib目录下,打开工程的web.xml文件加上相应的配置
 <servlet>
  <servlet-name>Captcha</servlet-name>
  <servlet-class>nl.captcha.servlet.CaptchaServlet</servlet-class>
  <!--边框显示参数-->
  <init-param>
   <param-name>cap.border</param-name>
   <param-value>yes</param-value>
  </init-param>

  <!-- 设置图片边框色 -->
  <init-param>
   <param-name>cap.border.c</param-name>
   <param-value>green</param-value>
  </init-param>

  <!-- 边框粗细-->
  <init-param>
   <param-name>cap.border.th</param-name>
   <param-value>1</param-value>
  </init-param>

  <!-- the image producer. Currently only one-->
  <init-param>
   <param-name>cap.cap.producer</param-name>
   <param-value>
    nl.captcha.obscurity.imp.DefaultCaptchaIml
   </param-value>
  </init-param>

  <!-- 设置验证码的内容 -->
  <init-param>
   <param-name>cap.text.producer</param-name>
   <!--<param-value>Default</param-value>-->
   <!-- 此处是我自定义的一个显示中文的类 -->
   <param-value>org.ChineseTextProducer</param-value>
  </init-param>

  <!-- 此处可不定义,可在自定义类中定义所有的要显示的内容 -->
  <init-param>
   <param-name>cap.char.arr</param-name>
   <param-value>
    2,3,4,6,7,8,9,a,b,c,d,e,f,g,h,j,k,l,m,n,o,p,q,r,s,t,u,v,x,y,z
   </param-value>
  </init-param>

  <!-- 设置图片显示内容长度 -->
  <init-param>
   <param-name>cap.char.arr.l</param-name>
   <param-value>4</param-value>
  </init-param>

  <init-param>
   <param-name>cap.font.arr</param-name>
   <!-- 如果使用中文时设置成中文的字体 -->
   <param-value>
    Arial,Helvetica,Courier,TimesRoman,宋体,黑体
   </param-value>
  </init-param>

  <!-- 设置字体大小 -->
  <init-param>
   <param-name>cap.font.size</param-name>
   <param-value>30</param-value>
  </init-param>

  <!-- 设置图片字体色 -->
  <init-param>
   <param-name>cap.font.color</param-name>
   <param-value>white</param-value>
  </init-param>
  
  <!-- 扭曲; 曲解; 变形 begin -->
  <!-- 图片效果1
  <init-param>
   <param-name>cap.obscurificator</param-name>
   以下任选一
   <param-value>nl.captcha.obscurity.imp.ShadowGimpyImp</param-value>
   <param-value>nl.captcha.obscurity.imp.WaterRiple</param-value>
   <param-value>nl.captcha.sandbox.TestGimpyImp</param-value>
  </init-param>
  -->
  <init-param>
   <param-name>cap.obscurificator</param-name>
   <param-value>org.FishEyeGimpyImp</param-value>
  </init-param>

  <init-param>
   <param-name>cap.border</param-name>
   <param-value>no</param-value>
  </init-param>

  <init-param>
   <param-name>cap.char.arr.l</param-name>
   <param-value>6</param-value>
  </init-param>

  <init-param>
   <param-name>cap.background.c.to</param-name>
   <param-value>white</param-value>
  </init-param>
  
  <init-param>
   <param-name>cap.background.c.from</param-name>
   <param-value>white</param-value>
  </init-param>
  <!--扭曲; 曲解; 变形 end 不想要这种效果可注释以上代码-->


  <init-param>
   <param-name>cap.distortionImp</param-name>
   <param-value />
  </init-param>

  <init-param>
   <param-name>cap.backgroundImp</param-name>
   <param-value />
  </init-param>

  <!-- 此处色为渐变的色 -->
  <init-param>
   <param-name>cap.background.c.to</param-name>
   <param-value>black</param-value>
  </init-param>

  <!-- 图片左边色 -->
  <init-param>
   <param-name>cap.background.c.from</param-name>
   <param-value>black</param-value>
  </init-param>

  <!--干扰线实现类-->
  <init-param>
   <param-name>cap.noiseImp</param-name>
   <param-value>nl.captcha.obscurity.imp.DefaultNoiseImp</param-value>
  </init-param>

  <!-- 干扰线色
  <init-param>
   <param-name>cap.noise.c</param-name>
   <param-value>255,245,219</param-value>
  </init-param>
   -->
 </servlet>
 <servlet-mapping>
  <servlet-name>Captcha</servlet-name>
  <url-pattern>/Captcha.jpg</url-pattern>
 </servlet-mapping>
  以上配置可根据喜好设置。

 

 2、调用显示

 在要显示验证码的页面中加入<img src="Captcha.jpg">即可显示生成的验证码

 

 3、后台验证
 验证非常简单,通过session.getAttribute(nl.captcha.servlet.Constants.SIMPLE_CAPCHA_SESSION_KEY);即可得到组件生成
的验证码,然后与前台传递的用户填写的内容比较即可。
 
 注:如果要自定义验证码的内容可以继承DefaultTextCreator类,覆写getText()方法即可,方法返回的是一个字符串。

 

  二、JCAPTCHA,该组件功能非常强大,可以生成word、图片、声音等验证码。主页:
 http://forge.octo.com/jcaptcha/confluence/display/general/Home
 http://jcaptcha.sourceforge.net/

 

example
 更多实例可以查看:http://forge.octo.com/jcaptcha/confluence/display/general/Samples+tests


 1、配置
 下载JCAPTCHA后将jcaptcha.jar和commons-collections-2.1以上.jar(单独下载,地址:http://commons.apache.org/collections/)放置工程lib目录下。

 

 2、Implement a CaptchaService

 import com.octo.captcha.service.image.ImageCaptchaService;
 import com.octo.captcha.service.image.DefaultManageableImageCaptchaService;

 public class CaptchaServiceSingleton {

     private static ImageCaptchaService instance = new DefaultManageableImageCaptchaService();

     public static ImageCaptchaService getInstance(){
         return instance;
       }
 }

 

 3、编写一个产生验证码的servlet
 
 public class ImageCaptchaServlet extends HttpServlet {
 
 protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
       throws ServletException, IOException {

        byte[] captchaChallengeAsJpeg = null;
     
        ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
        try {
        String captchaId = httpServletRequest.getSession().getId();
       
         BufferedImage challenge =
                    CaptchaServiceSingleton.getInstance().getImageChallengeForID(captchaId,
                            httpServletRequest.getLocale());
            JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(jpegOutputStream);
            jpegEncoder.encode(challenge);
        } catch (IllegalArgumentException e) {
            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        } catch (CaptchaServiceException e) {
            httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        }

        captchaChallengeAsJpeg = jpegOutputStream.toByteArray();

        httpServletResponse.setHeader("Cache-Control", "no-store");
        httpServletResponse.setHeader("Pragma", "no-cache");
        httpServletResponse.setDateHeader("Expires", 0);
        httpServletResponse.setContentType("image/jpeg");
        ServletOutputStream responseOutputStream =
                httpServletResponse.getOutputStream();
        responseOutputStream.write(captchaChallengeAsJpeg);
        responseOutputStream.flush();
        responseOutputStream.close();
    }
}


 4、将servlet添加到web.xml文件中
 <servlet>
         <servlet-name>jcaptcha</servlet-name>
         <servlet-class>ImageCaptchaServlet</servlet-class>
         <load-on-startup>0</load-on-startup>
        </servlet>

 <servlet-mapping>
         <servlet-name>jcaptcha</servlet-name>
         <url-pattern>/jcaptcha</url-pattern>
 </servlet-mapping>

 

 5、在页面中显示
 <img src="jcaptcha">
 <input type='text' name='captcha_code' value=''>

 

 6、后台验证
 Boolean isResponseCorrect =Boolean.FALSE;
        String captchaId = httpServletRequest.getSession().getId();
        String response = httpServletRequest.getParameter("captcha_code");
        try {
                isResponseCorrect = CaptchaServiceSingleton.getInstance().validateResponseForID(captchaId,response);
        } catch (CaptchaServiceException e) {
  //TODO
        }
 最简单的开发过程结束,试运行一下效果!
 
 注:要实现自己的验证引擎可以继承ListImageCaptchaEngine实现抽象方法buildInitialFactories()。

posted on 2009-07-29 10:37 礼物 阅读(1999) 评论(1)  编辑  收藏 所属分类: Jsp

评论

# re: 利用开源组件制作验证码 Captcha 2013-08-29 12:09 ssss
ggg  回复  更多评论