优化验证码效果

This commit is contained in:
synchronized
2018-07-30 13:10:51 +08:00
parent 74a1991352
commit 92af7138a9
9 changed files with 160 additions and 31 deletions

View File

@@ -1,5 +1,6 @@
package com.wf.captcha.utils;
import java.awt.*;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
@@ -7,6 +8,7 @@ import javax.servlet.http.HttpServletResponse;
import com.wf.captcha.Captcha;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.SpecCaptcha;
/**
* 图形验证码工具类
@@ -55,6 +57,20 @@ public class CaptchaUtil {
out(130, 48, len, request, response);
}
/**
* 输出验证码
*
* @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)
throws IOException {
out(130, 48, len, font, request, response);
}
/**
* 输出验证码
*
@@ -67,8 +83,119 @@ public class CaptchaUtil {
*/
public static void out(int width, int height, int len, HttpServletRequest request, HttpServletResponse response)
throws IOException {
out(width, height, len, null, request, response);
}
/**
* 输出验证码
*
* @param width 宽度
* @param height 高度
* @param len 长度
* @param font 字体
* @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)
throws IOException {
outCaptcha(width, height, len, font, 1, request, response);
}
/**
* 输出验证码
*
* @param request HttpServletRequest
* @param response HttpServletResponse
* @throws IOException IO异常
*/
public static void outPng(HttpServletRequest request, HttpServletResponse response)
throws IOException {
outPng(5, request, response);
}
/**
* 输出验证码
*
* @param len 长度
* @param request HttpServletRequest
* @param response HttpServletResponse
* @throws IOException IO异常
*/
public static void outPng(int len, HttpServletRequest request, HttpServletResponse response)
throws IOException {
outPng(130, 48, len, request, response);
}
/**
* 输出验证码
*
* @param len 长度
* @param font 字体
* @param request HttpServletRequest
* @param response HttpServletResponse
* @throws IOException IO异常
*/
public static void outPng(int len, Font font, HttpServletRequest request, HttpServletResponse response)
throws IOException {
outPng(130, 48, len, font, request, response);
}
/**
* 输出验证码
*
* @param width 宽度
* @param height 高度
* @param len 长度
* @param request HttpServletRequest
* @param response HttpServletResponse
* @throws IOException IO异常
*/
public static void outPng(int width, int height, int len, HttpServletRequest request, HttpServletResponse response)
throws IOException {
outPng(width, height, len, null, request, response);
}
/**
* 输出验证码
*
* @param width 宽度
* @param height 高度
* @param len 长度
* @param font 字体
* @param request HttpServletRequest
* @param response HttpServletResponse
* @throws IOException IO异常
*/
public static void outPng(int width, int height, int len, Font font, HttpServletRequest request, HttpServletResponse response)
throws IOException {
outCaptcha(width, height, len, font, 0, request, response);
}
/**
* 输出验证码
*
* @param width 宽度
* @param height 高度
* @param len 长度
* @param font 字体
* @param cType 类型
* @param request HttpServletRequest
* @param response HttpServletResponse
* @throws IOException IO异常
*/
private static void outCaptcha(int width, int height, int len, Font font, int cType, HttpServletRequest request, HttpServletResponse response)
throws IOException {
setHeader(response);
Captcha captcha = new GifCaptcha(width, height, len);
Captcha captcha = null;
if (cType == 0) {
captcha = new SpecCaptcha(width, height, len);
} else if (cType == 1) {
captcha = new GifCaptcha(width, height, len);
}
if (font != null) {
captcha.setFont(font);
}
request.getSession().setAttribute(SESSION_KEY, captcha.text().toLowerCase());
captcha.out(response.getOutputStream());
}