diff --git a/pom.xml b/pom.xml
index f1cb2f4..f8a9e1c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -3,7 +3,7 @@
4.0.0
EasyCaptcha
EasyCaptcha
- 1.0.0-SNAPSHOT
+ 1.1.0-RELEASE
EasyCaptcha
diff --git a/src/main/java/com/wf/captcha/servlet/CaptchaServlet.java b/src/main/java/com/wf/captcha/servlet/CaptchaServlet.java
index 68047ba..94a657c 100644
--- a/src/main/java/com/wf/captcha/servlet/CaptchaServlet.java
+++ b/src/main/java/com/wf/captcha/servlet/CaptchaServlet.java
@@ -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);
}
-
+
}
diff --git a/src/main/java/com/wf/captcha/utils/CaptchaUtil.java b/src/main/java/com/wf/captcha/utils/CaptchaUtil.java
index bbbb7c3..ea11f7f 100644
--- a/src/main/java/com/wf/captcha/utils/CaptchaUtil.java
+++ b/src/main/java/com/wf/captcha/utils/CaptchaUtil.java
@@ -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);
}
}