修复验证码位数的bug,优化代码

This commit is contained in:
synchronized
2018-07-27 12:02:33 +08:00
parent a4e61fd633
commit a548f7af0b
16 changed files with 675 additions and 461 deletions

View File

@@ -2,7 +2,6 @@ package com.wf.captcha.utils;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -11,136 +10,75 @@ import com.wf.captcha.GifCaptcha;
/**
* 图形验证码工具类
*
* @author wangfan
* @date 2018-5-14 上午9:41:06
* Created by 王帆 on 2018-07-27 上午 10:08.
*/
public class CaptchaUtil {
private String codeName = "captcha";
private int width = 130;
private int height = 38;
private int len = 5;
private static final String SESSION_KEY = "captcha";
public CaptchaUtil() {
}
/**
* 验证验证码
*/
public static boolean ver(String code, HttpServletRequest request) {
if (code != null && !code.trim().isEmpty()) {
String captcha = (String) request.getSession().getAttribute(SESSION_KEY);
return code.trim().toLowerCase().equals(captcha);
}
return false;
}
/**
* 验证码的宽、高、位数
*/
public CaptchaUtil(int width, int height, int len) {
set(width, height, len);
}
/**
* 输出验证码
*
* @param request
* @param response
* @throws IOException
*/
public static void out(HttpServletRequest request, HttpServletResponse response)
throws IOException {
out(130, 38, 5, request, response);
}
/**
* 验证验证码
*/
public boolean ver(String code, HttpServletRequest request) {
String captcha = (String) request.getSession().getAttribute(codeName);
return code.equals(captcha);
}
/**
* 输出验证码
*
* @param len 长度
* @param request
* @param response
* @throws IOException
*/
public static void out(int len, HttpServletRequest request, HttpServletResponse response)
throws IOException {
out(130, 38, len, request, response);
}
/**
* 验证验证码,用于分离的项目
*/
public boolean ver(String key, String code, HttpServletRequest rq) {
ServletContext sc = rq.getServletContext();
String keyName = codeName + "-" + key;
String captcha = (String) sc.getAttribute(keyName);
return code.equals(captcha);
}
/**
* 输出验证码
*
* @param width 宽度
* @param height 高度
* @param len 长度
* @param request
* @param response
* @throws IOException
*/
public static void out(int width, int height, int len, HttpServletRequest request, HttpServletResponse response)
throws IOException {
setHeader(response);
Captcha captcha = new GifCaptcha(130, 38, 5);
request.getSession().setAttribute(SESSION_KEY, captcha.text().toLowerCase());
captcha.out(response.getOutputStream());
}
/**
* 输出验证码
*/
public void out(HttpServletRequest rq, HttpServletResponse rp)
throws IOException {
/**
* 设置相应头
*
* @param response
*/
private static void setHeader(HttpServletResponse response) {
response.setContentType("image/gif");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
}
setHeader(rp);
// 验证码的宽、高、位数
Captcha captcha = new GifCaptcha(width, height, len);
// 存入缓存
rq.getSession().setAttribute(codeName, captcha.text().toLowerCase());
// 输入图片
captcha.out(rp.getOutputStream());
}
/**
* 输出验证码,用于分离项目
*/
public void out(String key, HttpServletRequest rq, HttpServletResponse rp)
throws IOException {
setHeader(rp);
// 验证码的宽、高、位数
Captcha captcha = new GifCaptcha(width, height, len);
// 存入缓存
ServletContext sc = rq.getServletContext();
sc.setAttribute(codeName, captcha.text().toLowerCase());
// 输入图片
captcha.out(rp.getOutputStream());
}
private void setHeader(HttpServletResponse rp) {
rp.setContentType("image/gif");
rp.setHeader("Pragma", "No-cache");
rp.setHeader("Cache-Control", "no-cache");
rp.setDateHeader("Expires", 0);
}
public String getCodeName() {
return codeName;
}
/**
* 设置保存code的key
*/
public void setCodeName(String codeName) {
this.codeName = codeName;
}
public int getWidth() {
return width;
}
/**
* 设置验证码的宽度
*/
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
/**
* 设置验证码的高度
*/
public void setHeight(int height) {
this.height = height;
}
public int getLen() {
return len;
}
/**
* 设置验证码的位数
*/
public void setLen(int len) {
this.len = len;
}
/**
* 设置验证码的宽、高、位数
*/
public void set(int width, int height, int len) {
setWidth(width);
setHeight(height);
setLen(len);
}
}