Java利用captcha实验图片验证码

Java利用captcha实验图片验证码

下载jar包patchca-0.5.0.jar 

引入到项目中

获取验证码图片类

package com.wamei.controller.jcaptcha;

import org.apache.log4j.Logger;
import org.patchca.color.ColorFactory;
import org.patchca.filter.predefined.*;
import org.patchca.service.ConfigurableCaptchaService;
import org.patchca.utils.encoder.EncoderHelper;
import org.patchca.word.RandomWordFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.io.IOException;
import java.util.Random;

/**
 * Created by qixuan.chen on 2017/5/18.
 */
@Controller
@RequestMapping("/captcha")
public class JcaptchaImageCreater {
    private final static Logger logger = Logger.getLogger(JcaptchaImageCreater.class);

    private static ConfigurableCaptchaService cs = new ConfigurableCaptchaService();
    private static Random random = new Random();
    static {
//        cs.setColorFactory(new SingleColorFactory(new Color(25, 60, 170)));
        cs.setColorFactory(new ColorFactory() {
            @Override
            public Color getColor(int x) {
                int[] c = new int[3];
                int i = random.nextInt(c.length);
                for (int fi = 0; fi < c.length; fi++) {
                    if (fi == i) {
                        c[fi] = random.nextInt(71);
                    } else {
                        c[fi] = random.nextInt(256);
                    }
                }
                return new Color(c[0], c[1], c[2]);
            }
        });
        RandomWordFactory wf = new RandomWordFactory();
        wf.setCharacters("23456789abcdefghigkmnpqrstuvwxyzABCDEFGHIGKLMNPQRSTUVWXYZ");
        wf.setMaxLength(4);
        wf.setMinLength(4);
        cs.setWordFactory(wf);
    }

    @RequestMapping("/pcrimg")
    public void crimg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer number = ServletRequestUtils.getIntParameter(request, "number", 4);
        Integer width = ServletRequestUtils.getIntParameter(request, "width", 200);
        Integer height = ServletRequestUtils.getIntParameter(request, "height", 60);
        cs.setHeight(height);
        cs.setWidth(width);
        RandomWordFactory rwf = new RandomWordFactory();
        rwf.setMinLength(number);
        rwf.setMaxLength(number);
        cs.setWordFactory(rwf);
        switch (random.nextInt(5)) {
            case 0:
                cs.setFilterFactory(new CurvesRippleFilterFactory(cs.getColorFactory()));
                break;
            case 1:
                cs.setFilterFactory(new MarbleRippleFilterFactory());
                break;
            case 2:
                cs.setFilterFactory(new DoubleRippleFilterFactory());
                break;
            case 3:
                cs.setFilterFactory(new WobbleRippleFilterFactory());
                break;
            case 4:
                cs.setFilterFactory(new DiffuseRippleFilterFactory());
                break;
        }
        HttpSession session = request.getSession(false);
        if (session == null) {
            session = request.getSession();
        }
        setResponseHeaders(response);
        String token = EncoderHelper.getChallangeAndWriteImage(cs, "png", response.getOutputStream());
        session.setAttribute("captchaToken", token);
        logger.info("当前的SessionID=" + session.getId() + ",验证码=" + token);
    }

    protected void setResponseHeaders(HttpServletResponse response) {
        response.setContentType("image/png");
        response.setHeader("Cache-Control", "no-cache, no-store");
        response.setHeader("Pragma", "no-cache");
        long time = System.currentTimeMillis();
        response.setDateHeader("Last-Modified", time);
        response.setDateHeader("Date", time);
        response.setDateHeader("Expires", time);
    }

}

页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>



	登录
    
	
	
    
    
    
    

    
    
    
    

    
    
    
    
    

    
    



    

	
    
    
    

    
    


效果:


你可能感兴趣的:(Java,Java,web,JavaEE)