提交代码

This commit is contained in:
synchronized 2018-05-14 09:59:41 +08:00
parent a7e43d9529
commit 668d881479
3 changed files with 151 additions and 37 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>EasyCaptcha</groupId>
<artifactId>EasyCaptcha</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.1.0-RELEASE</version>
<name>EasyCaptcha</name>
<dependencies>

View File

@ -2,45 +2,37 @@ package com.wf.captcha.servlet;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.wf.captcha.Captcha;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.utils.CaptchaUtil;
/**
* 验证码servlet
*
* @author wangfan
* @date 2018-5-14 上午9:53:01
*/
public class CaptchaServlet extends HttpServlet {
private static final long serialVersionUID = -90304944339413093L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取codeKey
String codeKey = request.getParameter("codeKey");
if(codeKey==null||codeKey.trim().isEmpty()){
return;
}
//设置输出图片
response.setContentType("image/gif");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// gif验证码, 位数
Captcha captcha = new GifCaptcha(130,38,5);
// 存入servletContext
ServletContext servletContext = request.getSession().getServletContext();
servletContext.setAttribute("code_"+codeKey, captcha.text().toLowerCase());
//输入图片
captcha.out(response.getOutputStream());
// 是否有key决定是存在session中还是servletContext中
String key = request.getParameter("key");
CaptchaUtil cu = new CaptchaUtil();
if (key != null && !key.trim().isEmpty()) {
cu.out(key, request, response);
} else {
cu.out(request, response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}

View File

@ -1,24 +1,146 @@
package com.wf.captcha.utils;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.wf.captcha.Captcha;
import com.wf.captcha.GifCaptcha;
/**
* 图形验证码工具类
*
* @author wangfan
* @date 2018-5-14 上午9:41:06
*/
public class CaptchaUtil {
private String codeName = "captcha";
private int width = 130;
private int height = 38;
private int len = 5;
public CaptchaUtil() {
}
/**
* 验证码的宽位数
*/
public CaptchaUtil(int width, int height, int len) {
set(width, height, len);
}
/**
* 验证验证码
*
* @param verKey
* @param verCode
* @param request
* @return
*/
public static boolean isVerified(String verKey, String verCode,
HttpServletRequest request) {
ServletContext servletContext = request.getSession()
.getServletContext();
String cacheVerCode = (String) servletContext.getAttribute("code_"
+ verKey);
return verCode.equals(cacheVerCode);
public boolean ver(String code, HttpServletRequest request) {
String captcha = (String) request.getSession().getAttribute(codeName);
return code.equals(captcha);
}
/**
* 验证验证码用于分离的项目
*/
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);
}
/**
* 输出验证码
*/
public void out(HttpServletRequest rq, HttpServletResponse rp)
throws IOException {
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);
}
}