来一个我们公司用的登录验证码。很普通啦,大家借鉴一下

不说废话,直接来代码:

生产验证码

/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package cn.creditharmoney.common.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * 生成随机验证码

 */
@SuppressWarnings("serial")
public class ValidateCodeServlet extends HttpServlet {
   
    public static final String VALIDATE_CODE = "validateCode";
   
    private int w = 70;
    private int h = 26;
   
    public ValidateCodeServlet() {
        super();
    }
   
    public void destroy() {
        super.destroy();
    }
   
    public static boolean validate(HttpServletRequest request, String validateCode){
        String code = (String)request.getSession().getAttribute(VALIDATE_CODE);
        return validateCode.toUpperCase().equals(code);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String validateCode = request.getParameter(VALIDATE_CODE); // AJAX验证,成功返回true
        if (StringUtils.isNotBlank(validateCode)){
            response.getOutputStream().print(validate(request, validateCode)?"true":"false");
        }else{
            this.doPost(request, response);
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        createImage(request,response);
    }
   
    private void createImage(HttpServletRequest request,
            HttpServletResponse response) throws IOException {
       
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
       
        /*
         * 得到参数高,宽,都为数字时,则使用设置高宽,否则使用默认值
         */
        String width = request.getParameter("width");
        String height = request.getParameter("height");
        if (StringUtils.isNumeric(width) && StringUtils.isNumeric(height)) {
            w = NumberUtils.toInt(width);
            h = NumberUtils.toInt(height);
        }
       
        BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();

        /*
         * 生成背景
         */
        createBackground(g);

        /*
         * 生成字符
         */
        String s = createCharacter(g);
        request.getSession().setAttribute(VALIDATE_CODE, s);

        g.dispose();
        OutputStream out = response.getOutputStream();
        ImageIO.write(image, "JPEG", out);
        out.close();

    }
   
    private Color getRandColor(int fc,int bc) {
        int f = fc;
        int b = bc;
        Random random=new Random();
        if(f>255) {
            f=255;
        }
        if(b>255) {
            b=255;
        }
        return new Color(f+random.nextInt(b-f),f+random.nextInt(b-f),f+random.nextInt(b-f));
    }
   
    private void createBackground(Graphics g) {
        // 填充背景
        g.setColor(getRandColor(220,250));
        g.fillRect(0, 0, w, h);
        // 加入干扰线条
        for (int i = 0; i < 8; i++) {
            g.setColor(getRandColor(40,150));
            Random random = new Random();
            int x = random.nextInt(w);
            int y = random.nextInt(h);
            int x1 = random.nextInt(w);
            int y1 = random.nextInt(h);
            g.drawLine(x, y, x1, y1);
        }
    }

    private String createCharacter(Graphics g) {
        char[] codeSeq = {'9' };
        String[] fontTypes = {"\u5b8b\u4f53","\u65b0\u5b8b\u4f53","\u9ed1\u4f53","\u6977\u4f53","\u96b6\u4e66"};
        Random random = new Random();
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            String r = String.valueOf(codeSeq[random.nextInt(codeSeq.length)]);//random.nextInt(10));
            g.setColor(new Color(50 + random.nextInt(100), 50 + random.nextInt(100), 50 + random.nextInt(100)));
            g.setFont(new Font(fontTypes[random.nextInt(fontTypes.length)],Font.BOLD,26));
            g.drawString(r, 15 * i + 5, 19 + random.nextInt(8));
//            g.drawString(r, i*w/4, h-5);
            s.append(r);
        }
        return s.toString();
    }
   
}
验证随机验证码:

package cn.creditharmoney.common.servlet;

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;

public class ValidateCodeSuccessServlet extends HttpServlet {
    public static final String VALIDATE_CODE = "validateCode";
    @Override
    protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        arg0.setCharacterEncoding("utf-8");
        arg1.setContentType("text/html;charset=utf-8");
        String str = (String) arg0.getParameter("captcha");
        String code = (String)arg0.getSession().getAttribute(VALIDATE_CODE);
        PrintWriter out = arg1.getWriter();
        if(code.equals(str.toUpperCase())){
            out.write("1");
        }else{
            out.write("2");
        }
        out.flush();
        out.close();
    }
       
}
前台验证:

 $(function() {
         $(".dsucess").hide();
           $("#captcha").keyup(function() {
               var captcha = $("#captcha").val();
               if(captcha.length == 4){
               $.ajax({
                        type : "POST", 
                        url: "${ctx}/servlet/validateCodeSuccessServlet",
                        data : "captcha="+captcha,
                        dataType : 'json',
                        success: function(data,status){
                            if(data=="1"){
                                $(".dsucess").show();
                            }else{
                                $(".dsucess").hide();
                            }
                          }
               });
               }else{
                   $(".dsucess").hide();
               }
            });
        }); 

web.xml配置

 <!-- 随机验证码 -->
  <servlet>
    <servlet-name>ValidateCodeServlet</servlet-name>
    <servlet-class>cn.creditharmoney.common.servlet.ValidateCodeServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ValidateCodeServlet</servlet-name>
    <url-pattern>/servlet/validateCodeServlet</url-pattern>
  </servlet-mapping>
 
    <!-- 验证随机验证码 -->
  <servlet>
    <servlet-name>ValidateCodeSuccessServlet</servlet-name>
    <servlet-class>cn.creditharmoney.common.servlet.ValidateCodeSuccessServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ValidateCodeSuccessServlet</servlet-name>
    <url-pattern>/servlet/validateCodeSuccessServlet</url-pattern>
  </servlet-mapping>

ok。搞定。大家有需要的就可以看看,有更好的验证码拿出来大家分享分享。

你可能感兴趣的:(验证,随机验证码,前台)