SimpleCaptcha验证码组件使用

http://zhangzhaoaaa.iteye.com/blog/2197359
最近需要做一个验证码信息,在网上找了些验证码组件,选择了SimpleCaptcha组件,java服务器端代码,简单方便使用。
      对于此组件里的中文字的验证码,不是很合适,有时候会出现乱码情况,我认为如果想要加入中文字的验证,自己实现匹配就行了。
     我这里只做抛砖引玉,贴出主要代码,源码包,SimpleCaptcha组件的源码和DOCS供参考使用(全包超过10mb,不让上传啊)。如果只是用数字和英文字母使用验证码,不用考虑自定义的验证码SimpleCaptchaFilter.java。后台代码很容易改成SpringMVC等使用,我就不多说啦,show me the code.
      效果:


     附件:Test.zip是我已实现的源码工程
               docs.zip是组件文档
               examples.zip是组件例子
               Java.zip是组件源码
1.captcha.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"  
    pageEncoding="UTF-8"%>  
    <%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
<title>验证码</title>  
<script type="text/javascript" src="<%=basePath %>/js/jquery.js" ></script>  
</head>  
<body>  
 <img id="code" style="cursor:pointer;" alt="看不清楚?点图标可以换一个" title="看不清楚?点图标可以换一个" />    
    <form action="<%=basePath %>/CheckCode" method="post">    
        <input id="answer" name="answer" />    
        <input id="submitBtn" type="button" value="提交" name="submit"/>    
</form>  
<script type="text/javascript">  
    $(function(){  
        var imgCode = $('#code');   
        $(imgCode).attr('src',"<%=basePath %>/simpleImg");  
        $(imgCode).click(function(){  
            $(this).attr('src',"<%=basePath %>/simpleImg");  
        });  
        $('#submitBtn').click(function(){  
            $.post('<%=basePath %>/CheckCode?answer='+$('#answer').val(),function(data){  
                if (data=='true'){  
                    window.location.href="<%=basePath %>/captchaSuccess.jsp";  
                }else{  
                    alert("验证码错误!");      
                    return false;  
                }  
            });  
        });  
    });  
</script>  
</body>  
</html>
 

2.captchaSuccess.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"  
    pageEncoding="UTF-8"%>  
  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
<title>验证码</title>  
</head>  
<body>  
你成功了!  
</body>  
</html> 


3.CheckCode.java
import java.io.IOException;  
import java.io.PrintWriter;  
  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
  
import nl.captcha.Captcha;  
  
/** 
 * Servlet implementation class CheckCode 
 */  
public class CheckCode extends HttpServlet {  
    private static final long serialVersionUID = 1L;  
         
    /** 
     * @see HttpServlet#HttpServlet() 
     */  
    public CheckCode() {  
        super();  
        // TODO Auto-generated constructor stub  
    }  
  
    /** 
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
     */  
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
        this.doPost(request, response);  
    }  
  
    /** 
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 
     */  
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
        Captcha captcha = (Captcha) request.getSession().getAttribute(Captcha.NAME);    
        request.setCharacterEncoding("UTF-8");    
        String answer = request.getParameter("answer");  
        System.out.println(captcha.isCorrect(answer));  
        PrintWriter writer = response.getWriter();  
        writer.print(captcha.isCorrect(answer));  
        writer.flush();  
        writer.close();  
    }  
  
} 


4.SimpleCaptchaFilter.java
import static nl.captcha.Captcha.NAME;  
  
import java.awt.Color;  
import java.awt.Font;  
import java.awt.image.BufferedImage;  
import java.io.FileInputStream;  
import java.io.IOException;  
import java.util.ArrayList;  
import java.util.List;  
  
import javax.imageio.ImageIO;  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
  
import nl.captcha.Captcha;  
import nl.captcha.Captcha.Builder;  
import nl.captcha.backgrounds.FlatColorBackgroundProducer;  
import nl.captcha.gimpy.BlockGimpyRenderer;  
import nl.captcha.servlet.CaptchaServletUtil;  
import nl.captcha.text.producer.ChineseTextProducer;  
import nl.captcha.text.producer.DefaultTextProducer;  
import nl.captcha.text.renderer.ColoredEdgesWordRenderer;  
import nl.captcha.text.renderer.WordRenderer;  
  
/** 
 * @className:SimpleCaptchaFilter.java 
 * @classDescription: 扩展默认的simpleCaptcha 
 * @author:zz 
 * @createTime:2015-3-30 
 */  
  
public class SimpleCaptchaFilter extends HttpServlet{  
       
      private static final String PARAM_HEIGHT = "height"; //高度 默认为50  
    
      private static final String PARAM_WIDTH = "width";//宽度 默认为200  
        
      private static final String PAEAM_NOISE="noise";//干扰线条 默认是没有干扰线条  
        
      private static final String PAEAM_TEXT="text";//文本  
  
      protected int _width = 200;  
      protected int _height = 50;  
      protected boolean _noise=false;  
      protected String _text=null;  
        
      /** 
       * 初始化过滤器.将配置文件的参数文件赋值 
       * @throws ServletException 
       */  
      @Override  
        public void init() throws ServletException {  
            if (getInitParameter(PARAM_HEIGHT) != null) {  
                _height = Integer.valueOf(getInitParameter(PARAM_HEIGHT));  
            }  
              
            if (getInitParameter(PARAM_WIDTH) != null) {  
                _width = Integer.valueOf(getInitParameter(PARAM_WIDTH));  
            }  
              
            if (getInitParameter(PAEAM_NOISE) != null) {  
                _noise = Boolean.valueOf(getInitParameter(PAEAM_NOISE));  
            }  
              
            if (getInitParameter(PAEAM_NOISE) != null) {  
                _text = String.valueOf(getInitParameter(PAEAM_TEXT));  
            }  
        }  
        
      /** 
       * 因为获取图片只会有get方法 
       */  
       @Override  
      public void doGet(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {  
             
           Builder builder=new Captcha.Builder(_width, _height);  
           //增加边框  
           builder.addBorder();  
             
           //干扰线  
           /*CurvedLineNoiseProducer nosi = new CurvedLineNoiseProducer(Color.green,5); 
           builder.addNoise(nosi);*/  
           //是否增加干扰线条  
           if(_noise==true)  
               builder.addNoise();  
           //----------------自定义字体大小-----------  
           //自定义设置字体颜色和大小 最简单的效果 多种字体随机显示  
           List<Font> fontList = new ArrayList<Font>();  
           fontList.add(new Font("Arial", Font.ITALIC, 40));//可以设置斜体之类的  
           //fontList.add(new Font("Courier", Font.BOLD, 40));    
           /*List<Color> colorList = new ArrayList<Color>(); 
           colorList.add(Color.green); 
           colorList.add(Color.BLUE); 
           DefaultWordRenderer dwr=new DefaultWordRenderer(colorList,fontList);*/  
             
           //加入多种颜色后会随机显示 字体空心  
           List<Color> colorList=new ArrayList<Color>();  
           colorList.add(Color.green);  
          // colorList.add(Color.white);  
           colorList.add(Color.blue);  
           ColoredEdgesWordRenderer cwr= new ColoredEdgesWordRenderer(colorList,fontList);  
             
           WordRenderer wr=cwr;  
           //增加文本,默认为5个随机字符.  
           if(_text==null){  
               builder.addText();  
           }else{  
               String[]ts=_text.split(",");  
               for(int i=0;i<ts.length;i++){  
                   String[] ts1=ts[i].split(":");  
                   if("chinese".equals(ts1[0])){  
                      builder.addText(new ChineseTextProducer(Integer.parseInt(ts1[1])),wr);  
                   }else if("number".equals(ts1[0])){  
                       //这里没有0和1是为了避免歧义 和字母I和O  
                       char[] numberChar = new char[] { '2', '3', '4', '5', '6', '7', '8' };  
                       builder.addText(new DefaultTextProducer(Integer.parseInt(ts1[1]),numberChar),wr);  
                   }else if("word".equals(ts1[0])){  
                       //原理同上  
                       char[] numberChar = new char[] {'a','A', 'b', 'B','c','C', 'd','D',  
                                'e','E', 'f', 'F','g','G', 'h','H', 'k','K', 'm','M', 'n','N', 'p','P', 'r','R', 'w','W', 'x','X', 'y','Y' };  
                       builder.addText(new DefaultTextProducer(Integer.parseInt(ts1[1]),numberChar),wr);  
                   }/*else{ 
                       builder.addText(new DefaultTextProducer(Integer.parseInt(ts1[1])),wr); 
                   }*/  
               }  
                 
           }  
             
         //--------------添加背景-------------  
           //设置背景渐进效果 以及颜色 form为开始颜色,to为结束颜色  
           //GradiatedBackgroundProducer gbp=new GradiatedBackgroundProducer();  
           //gbp.setFromColor(Color.yellow);  
          // gbp.setToColor(Color.red);  
           //无渐进效果,只是填充背景颜色  
//         FlatColorBackgroundProducer  fbp=new FlatColorBackgroundProducer(Color.red);  
           //加入网纹--一般不会用  
//         SquigglesBackgroundProducer  sbp=new SquigglesBackgroundProducer();  
           // 没发现有什么用,可能就是默认的  
//         TransparentBackgroundProducer tbp = new TransparentBackgroundProducer();  
           //FlatColorBackgroundProducer fbp = new FlatColorBackgroundProducer();  
           //BufferedImage bufferedImage = ImageIO.read(new FileInputStream("E:\\eclipsespace2\\oms_portal\\WebContent\\images\\content\\table-info.png"));  
           //fbp.addBackground(bufferedImage);  
             
           //builder.addBackground(fbp);  
           //---------装饰字体---------------  
           // 字体边框齿轮效果 默认是3  
           builder.gimp(new BlockGimpyRenderer(1));  
           //波纹渲染 相当于加粗  
//         builder.gimp(new RippleGimpyRenderer());  
           //修剪--一般不会用  
//         builder.gimp(new ShearGimpyRenderer(Color.red));  
           //加网--第一个参数是横线颜色,第二个参数是竖线颜色  
//         builder.gimp(new FishEyeGimpyRenderer(Color.red,Color.yellow));  
           //加入阴影效果 默认3,75   
//         builder.gimp(new DropShadowGimpyRenderer());  
           //创建对象  
          Captcha captcha =  builder .build();  
             
       CaptchaServletUtil.writeImage(resp, captcha.getImage());  
       req.getSession().setAttribute(NAME, captcha);  
    }  
} 


5.web.xml
<?xml version="1.0" encoding="UTF-8"?>  
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
    id="WebApp_ID" version="2.5">  
    <display-name>Test</display-name>  
    <!-- 默认简单的验证码处理 -->  
    <servlet>  
        <servlet-name>SimpleCaptcha</servlet-name>  
        <servlet-class>nl.captcha.servlet.SimpleCaptchaServlet</servlet-class>  
        <init-param>  
            <param-name>width</param-name>  
            <param-value>250</param-value>  
        </init-param>  
        <init-param>  
            <param-name>height</param-name>  
            <param-value>75</param-value>  
        </init-param>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>SimpleCaptcha</servlet-name>  
        <url-pattern>/simpleImg</url-pattern>  
    </servlet-mapping>  
    <!-- 自定义验证码 -->  
    <!-- <servlet> <servlet-name>SimpleCaptcha</servlet-name> <servlet-class>SimpleCaptchaFilter</servlet-class>   
        <init-param> <param-name>width</param-name> <param-value>200</param-value>   
        </init-param> <init-param> <param-name>height</param-name> <param-value>50</param-value>   
        </init-param> 干扰线 <init-param> <param-name>noise</param-name> <param-value>true</param-value>   
        </init-param> 意思是3个单词,3个数字 <init-param> <param-name>text</param-name> <param-value>word:3,number:3</param-value>   
        </init-param> </servlet> <servlet-mapping> <servlet-name>SimpleCaptcha</servlet-name>   
        <url-pattern>/simpleImg</url-pattern> </servlet-mapping> -->  
    <!-- 验证码检验 -->      
    <servlet>  
        <description></description>  
        <display-name>CheckCode</display-name>  
        <servlet-name>CheckCode</servlet-name>  
        <servlet-class>CheckCode</servlet-class>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>CheckCode</servlet-name>  
        <url-pattern>/CheckCode</url-pattern>  
    </servlet-mapping>  
</web-app>
 

   参考:
http://www.iteye.com/topic/790024
http://www.iteye.com/topic/790038

你可能感兴趣的:(SimpleCaptcha)