CaptchaMaker.java 6.8 KB
Newer Older
Z
1.0.0  
zhuangqian 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/**
 * Copyright (c) 2011-2016, James Zhan 詹波 (jfinal@126.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

S
smallchill 已提交
17
package org.springblade.core.toolbox.captcha;
Z
1.0.0  
zhuangqian 已提交
18

19 20 21 22 23
import org.springblade.core.constant.Cst;
import org.springblade.core.toolbox.kit.CookieKit;
import org.springblade.core.toolbox.kit.HashKit;
import org.springblade.core.toolbox.kit.LogKit;
import org.springblade.core.toolbox.kit.StrKit;
Z
1.0.0  
zhuangqian 已提交
24 25 26 27 28 29

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
30 31 32 33 34
import java.awt.*;
import java.awt.geom.QuadCurve2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
Z
1.0.0  
zhuangqian 已提交
35 36 37

/**
 * CaptchaRender.
38
 * @author jfinal
Z
1.0.0  
zhuangqian 已提交
39 40 41 42 43 44 45
 */
public class CaptchaMaker {

	private HttpServletResponse response;
	
	private static String captchaName = "_tframework_captcha";

46 47 48
    /**
     * 默认的验证码大小
     */
Z
1.0.0  
zhuangqian 已提交
49
	private static final int WIDTH = 108, HEIGHT = 40;
50 51 52
    /**
     * 验证码随机字符数组
     */
Z
1.0.0  
zhuangqian 已提交
53
	private static final String[] strArr = {"3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y"};
54 55 56
    /**
     * 验证码字体
     */
Z
1.0.0  
zhuangqian 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	private static final Font[] RANDOM_FONT = new Font[] {
		new Font("nyala", Font.BOLD, 38),
		new Font("Arial", Font.BOLD, 32),
		new Font("Bell MT", Font.BOLD, 32),
		new Font("Credit valley", Font.BOLD, 34),
		new Font("Impact", Font.BOLD, 32),
		new Font(Font.MONOSPACED, Font.BOLD, 40)
	};
	
	public static CaptchaMaker init(HttpServletResponse response) {
		return new CaptchaMaker(response);
	}
	
	private CaptchaMaker(HttpServletResponse response){
		this.response = response;
	}
	
	/**
	 * 设置 captchaName
	 */
	public static void setCaptchaName(String captchaName) {
		if (StrKit.isBlank(captchaName)) {
			throw new IllegalArgumentException("captchaName can not be blank.");
		}
		CaptchaMaker.captchaName = captchaName;
	}
	
	/**
	 * 生成验证码
	 */
	public void start() {
		BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
		String vCode = drawGraphic(image);
90 91 92 93
        /**
         * 转成大写重要
         */
		vCode = vCode.toUpperCase();
Z
1.0.0  
zhuangqian 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
		vCode = HashKit.md5(vCode);
		Cookie cookie = new Cookie(captchaName, vCode);
		cookie.setMaxAge(-1);
		cookie.setPath("/");
		try {
			// try catch 用来兼容不支持 httpOnly 的 tomcat、jetty
			cookie.setHttpOnly(true);
		} catch (Exception e) {
			LogKit.logNothing(e);
		}
		response.addCookie(cookie);
		response.setHeader("Pragma","no-cache");
		response.setHeader("Cache-Control","no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/jpeg");

		ServletOutputStream sos = null;
		try {
			sos = response.getOutputStream();
			ImageIO.write(image, "jpeg", sos);
		} catch (IOException e) {
			if (Cst.me().isDevMode()) {
				throw new RuntimeException(e);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			if (sos != null) {
				try {sos.close();} catch (IOException e) {LogKit.logNothing(e);}
			}
		}
	}

	private String drawGraphic(BufferedImage image){
		// 获取图形上下文
		Graphics2D g = image.createGraphics();

		g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
		// 图形抗锯齿
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		// 字体抗锯齿
		g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

		// 设定背景色
		g.setColor(getRandColor(200, 250));
		g.fillRect(0, 0, WIDTH, HEIGHT);

		//生成随机类
		Random random = new Random();
		//设定字体
		g.setFont(RANDOM_FONT[random.nextInt(RANDOM_FONT.length)]);

		// 画蛋蛋,有蛋的生活才精彩
		Color color;
148 149
		Integer colorNum = 10;
		for(int i = 0; i < colorNum; i++){
Z
1.0.0  
zhuangqian 已提交
150 151 152 153 154 155 156 157
			color = getRandColor(120, 200);
			g.setColor(color);
			g.drawOval(random.nextInt(WIDTH), random.nextInt(HEIGHT), 5 + random.nextInt(10), 5 + random.nextInt(10));
			color = null;
		}

		// 取随机产生的认证码(4位数字)
		String sRand = "";
158 159
        Integer codeNum = 4;
		for (int i = 0; i < codeNum; i++){
Z
1.0.0  
zhuangqian 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
			String rand = String.valueOf(strArr[random.nextInt(strArr.length)]);
			sRand += rand;
			//旋转度数 最好小于45度
			int degree = random.nextInt(28);
			if (i % 2 == 0) {
				degree = degree * (-1);
			}
			//定义坐标
			int x = 22 * i, y = 21;
			//旋转区域
			g.rotate(Math.toRadians(degree), x, y);
			//设定字体颜色
			color = getRandColor(20, 130);
			g.setColor(color);
			//将认证码显示到图象中
			g.drawString(rand, x + 8, y + 10);
			//旋转之后,必须旋转回来
			g.rotate(-Math.toRadians(degree), x, y);
			color = null;
		}
		//图片中间线
		g.setColor(getRandColor(0, 60));
		//width是线宽,float型
		BasicStroke bs = new BasicStroke(3);
		g.setStroke(bs);
		//画出曲线
		QuadCurve2D.Double curve = new QuadCurve2D.Double(0d, random.nextInt(HEIGHT - 8) + 4, WIDTH / 2, HEIGHT / 2, WIDTH, random.nextInt(HEIGHT - 8) + 4);
		g.draw(curve);
		// 销毁图像
		g.dispose();
		return sRand;
	}

193
	/**
Z
1.0.0  
zhuangqian 已提交
194 195 196 197
	 * 给定范围获得随机颜色
	 */
	private Color getRandColor(int fc, int bc) {
		Random random = new Random();
198 199 200 201 202 203 204
		Integer num = 255;
		if (fc > num) {
            fc = num;
        }
		if (bc > num) {
            bc = num;
        }
Z
1.0.0  
zhuangqian 已提交
205 206 207 208 209 210 211 212
		int r = fc + random.nextInt(bc - fc);
		int g = fc + random.nextInt(bc - fc);
		int b = fc + random.nextInt(bc - fc);
		return new Color(r, g, b);
	}

	/**
	 * 仅能验证一次,验证后立即销毁 cookie
213 214 215
	 * @param request
     * @param response
     * @param userInputCaptcha 用户输入的验证码
Z
1.0.0  
zhuangqian 已提交
216 217 218 219 220 221
	 * @return 验证通过返回 true, 否则返回 false
	 */
	public static boolean validate(HttpServletRequest request, HttpServletResponse response, String userInputCaptcha) {
		if (StrKit.isBlank(userInputCaptcha)) {
			return false;
		}
222 223 224 225
        /**
         * 转成大写重要
         */
		userInputCaptcha = userInputCaptcha.toUpperCase();
Z
1.0.0  
zhuangqian 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238
		userInputCaptcha = HashKit.md5(userInputCaptcha);
		String cookieValue = CookieKit.getCookie(captchaName, request);
		boolean result = userInputCaptcha.equals(cookieValue);
		if (result == true) {
			CookieKit.removeCookie(captchaName, response);
		}
		return result;
	}
}