EasyCaptcha/src/main/java/com/wf/captcha/utils/CaptchaUtil.java

162 lines
4.8 KiB
Java
Raw Normal View History

2018-02-11 14:53:59 +08:00
package com.wf.captcha.utils;
2018-07-30 13:10:51 +08:00
import java.awt.*;
2018-05-14 09:59:41 +08:00
import java.io.IOException;
2018-02-11 14:53:59 +08:00
import javax.servlet.http.HttpServletRequest;
2018-05-14 09:59:41 +08:00
import javax.servlet.http.HttpServletResponse;
import com.wf.captcha.base.Captcha;
2018-07-30 13:10:51 +08:00
import com.wf.captcha.SpecCaptcha;
2018-02-11 14:53:59 +08:00
2018-05-14 09:59:41 +08:00
/**
* 图形验证码工具类
* Created by 王帆 on 2018-07-27 上午 10:08.
2018-05-14 09:59:41 +08:00
*/
2018-02-11 14:53:59 +08:00
public class CaptchaUtil {
private static final String SESSION_KEY = "captcha";
private static final int DEFAULT_LEN = 4; // 默认长度
private static final int DEFAULT_WIDTH = 130; // 默认宽度
private static final int DEFAULT_HEIGHT = 48; // 默认高度
/**
* 输出验证码
*
2018-07-27 15:11:34 +08:00
* @param request HttpServletRequest
* @param response HttpServletResponse
* @throws IOException IO异常
*/
public static void out(HttpServletRequest request, HttpServletResponse response)
throws IOException {
out(DEFAULT_LEN, request, response);
}
/**
* 输出验证码
*
* @param len 长度
2018-07-27 15:11:34 +08:00
* @param request HttpServletRequest
* @param response HttpServletResponse
* @throws IOException IO异常
*/
public static void out(int len, HttpServletRequest request, HttpServletResponse response)
throws IOException {
out(DEFAULT_WIDTH, DEFAULT_HEIGHT, len, request, response);
2018-07-30 13:10:51 +08:00
}
/**
* 输出验证码
*
* @param width 宽度
* @param height 高度
* @param len 长度
2018-07-27 15:11:34 +08:00
* @param request HttpServletRequest
* @param response HttpServletResponse
* @throws IOException IO异常
*/
public static void out(int width, int height, int len, HttpServletRequest request, HttpServletResponse response)
throws IOException {
2018-07-30 13:10:51 +08:00
out(width, height, len, null, request, response);
}
/**
* 输出验证码
*
* @param font 字体
* @param request HttpServletRequest
* @param response HttpServletResponse
* @throws IOException IO异常
*/
public static void out(Font font, HttpServletRequest request, HttpServletResponse response)
2018-07-30 13:10:51 +08:00
throws IOException {
out(DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_LEN, font, request, response);
2018-07-30 13:10:51 +08:00
}
/**
* 输出验证码
*
* @param len 长度
* @param font 字体
* @param request HttpServletRequest
* @param response HttpServletResponse
* @throws IOException IO异常
*/
public static void out(int len, Font font, HttpServletRequest request, HttpServletResponse response)
2018-07-30 13:10:51 +08:00
throws IOException {
out(DEFAULT_WIDTH, DEFAULT_HEIGHT, len, font, request, response);
2018-07-30 13:10:51 +08:00
}
/**
* 输出验证码
*
* @param width 宽度
* @param height 高度
* @param len 长度
* @param font 字体
2018-07-30 13:10:51 +08:00
* @param request HttpServletRequest
* @param response HttpServletResponse
* @throws IOException IO异常
*/
public static void out(int width, int height, int len, Font font, HttpServletRequest request, HttpServletResponse response)
2018-07-30 13:10:51 +08:00
throws IOException {
SpecCaptcha specCaptcha = new SpecCaptcha(width, height, len);
if (font != null) {
specCaptcha.setFont(font);
}
out(specCaptcha, request, response);
2018-07-30 13:10:51 +08:00
}
2018-07-30 13:10:51 +08:00
/**
* 输出验证码
*
* @param captcha Captcha
2018-07-30 13:10:51 +08:00
* @param request HttpServletRequest
* @param response HttpServletResponse
* @throws IOException IO异常
*/
private static void out(Captcha captcha, HttpServletRequest request, HttpServletResponse response)
2018-07-30 13:10:51 +08:00
throws IOException {
setHeader(response);
request.getSession().setAttribute(SESSION_KEY, captcha.text().toLowerCase());
captcha.out(response.getOutputStream());
2018-07-30 13:10:51 +08:00
}
/**
* 验证验证码
2018-07-30 13:10:51 +08:00
*
* @param code 用户输入的验证码
* @param request HttpServletRequest
* @return 是否正确
2018-07-30 13:10:51 +08:00
*/
public static boolean ver(String code, HttpServletRequest request) {
if (code != null) {
String captcha = (String) request.getSession().getAttribute(SESSION_KEY);
return code.trim().toLowerCase().equals(captcha);
2018-07-30 13:10:51 +08:00
}
return false;
}
2018-07-28 00:22:49 +08:00
/**
* 清除session中的验证码
*
* @param request HttpServletRequest
*/
public static void clear(HttpServletRequest request) {
request.getSession().removeAttribute(SESSION_KEY);
}
/**
* 设置相应头
*
2018-07-27 15:11:34 +08:00
* @param response HttpServletResponse
*/
2018-07-28 00:22:49 +08:00
public static void setHeader(HttpServletResponse response) {
response.setContentType("image/gif");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
}
2018-05-14 09:59:41 +08:00
2018-02-11 14:53:59 +08:00
}