算式验证码实现

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

效果图

算式验证码实现_第1张图片

项目结构

算式验证码实现_第2张图片

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>



    
    randomcode
    


random

check.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>



    
    check


<%
    // 检查是否是正确的验证码
    String k = (String) session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
    String str = request.getParameter("r");
    if (k.equals(str)) {
        out.print("验证码输入正确");
    } else {
        out.print("验证码输入错误");
    }
%>

<% out.print(k + "---" + str); %>

web.xml





    
    
        Kaptcha
        KaptchaServlet
        
            图片边框,合法值:yes , no
            kaptcha.border
            yes
        
        
            边框颜色,合法值: r,g,b (and optional alpha) 或者white,black,blue.
            kaptcha.border.color
            black
        
        
            边框厚度,合法值:>0
            kaptcha.border.thickness
            1
        
        
            图片宽 200
            kaptcha.image.width
            200
        
        
            图片高 50
            kaptcha.image.height
            50
        
        
            图片实现类
            kaptcha.producer.impl
            com.google.code.kaptcha.impl.DefaultKaptcha
        
        
            文本实现类
            kaptcha.textproducer.impl
            com.google.code.kaptcha.text.impl.DefaultTextCreator
        
        
            文本集合,验证码值从此集合中获取
            kaptcha.textproducer.char.string
            1234567890
        
        
            验证码长度 5
            kaptcha.textproducer.char.length
            3
        
        
            字体 Arial, Courier
            kaptcha.textproducer.font.names
            Arial, Courier
        
        
            字体大小 40px.
            kaptcha.textproducer.font.size
            40
        
        
            字体颜色,合法值: r,g,b 或者 white,black,blue.
            kaptcha.textproducer.font.color
            black
        
        
            文字间隔 2
            kaptcha.textproducer.char.space
            2
        
        
            干扰实现类
            kaptcha.noise.impl
            com.google.code.kaptcha.impl.DefaultNoise
        
        
            干扰颜色,合法值: r,g,b 或者 white,black,blue.
            kaptcha.noise.color
            black
        
        
            
                图片样式:
                水纹com.google.code.kaptcha.impl.WaterRipple
                鱼眼com.google.code.kaptcha.impl.FishEyeGimpy
                阴影com.google.code.kaptcha.impl.ShadowGimpy
            
            kaptcha.obscurificator.impl
            com.google.code.kaptcha.impl.WaterRipple
        
        
            背景实现类
            kaptcha.background.impl
            com.google.code.kaptcha.impl.DefaultBackground
        
        
            背景颜色渐变,开始颜色
            kaptcha.background.clear.from
            green
        
        
            背景颜色渐变,结束颜色
            kaptcha.background.clear.to
            white
        
        
            文字渲染器
            kaptcha.word.impl
            com.google.code.kaptcha.text.impl.DefaultWordRenderer
        
        
            session中存放验证码的key键
            kaptcha.session.key
            KAPTCHA_SESSION_KEY
        
        
            生成日期kaptcha放入HttpSession。这是该项目的关键值会话。
            kaptcha.session.date
            KAPTCHA_SESSION_DATE
        
    
    
        Kaptcha
        /randomcode.jpg
    


    
        index.jsp
    

KaptchaServlet

import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.util.Config;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class KaptchaServlet extends HttpServlet implements Servlet {
    private Properties props;
    private Producer kaptchaProducer;
    private String sessionKeyValue;

    public KaptchaServlet() {
        this.props = new Properties();
        this.kaptchaProducer = null;
        this.sessionKeyValue = null;
    }

    public void init(ServletConfig conf) throws ServletException {
        super.init(conf);
        ImageIO.setUseCache(false);
        Enumeration initParams = conf.getInitParameterNames();
        while (initParams.hasMoreElements()) {
            String key = (String) initParams.nextElement();
            String value = conf.getInitParameter(key);
            this.props.put(key, value);
        }
        Config config = new Config(this.props);
        this.kaptchaProducer = config.getProducerImpl();
        this.sessionKeyValue = config.getSessionKey();
    }

    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.setDateHeader("Expires", 0L);
        resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        resp.addHeader("Cache-Control", "post-check=0, pre-check=0");
        resp.setHeader("Pragma", "no-cache");
        resp.setContentType("image/jpeg");

        String capText = this.kaptchaProducer.createText();
        String s1 = capText.substring(0, (new Random().nextInt(3) + 1));
        String s2 = capText.substring((new Random().nextInt(2) + 1), 3);
        int r = Integer.valueOf(s1) + Integer.valueOf(s2);

        req.getSession().setAttribute(this.sessionKeyValue, String.valueOf(r));
        BufferedImage bi = this.kaptchaProducer.createImage(Integer.valueOf(s1) + "+" + Integer.valueOf(s2) + "=?");
        ServletOutputStream out = resp.getOutputStream();
        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }
    }
}

转载于:https://my.oschina.net/mcyy568/blog/718469

你可能感兴趣的:(算式验证码实现)