ps: 自己研究的工具类,目前都还不算完善,有些只是个笔记的作用,如果有问题,希望可以联系,我麻溜去修改,谢谢。
验证码相关的工具类
1、旧版
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>
<!-- 解决easy-captcha算术验证码报错问题 -->
<dependency>
<groupId>org.openjdk.nashorn</groupId>
<artifactId>nashorn-core</artifactId>
<version>15.4</version>
</dependency>
public class CaptchaCodeUtil {
private static final int WIDTH = 200;
private static final int HEIGHT = 40;
private static final int CAPTCHA_LEN = 6;
private static final int OPERATION_LEN = 3;
private static final int FONT_SIZE = 40;
private static final Random rm = new Random();
// TYPE_DEFAULT 数字和字母混合 TYPE_ONLY_NUMBER 纯数字
// TYPE_ONLY_CHAR 纯字母 TYPE_ONLY_UPPER 纯大写字母
// TYPE_ONLY_LOWER 纯小写字母 TYPE_NUM_AND_UPPER 数字和大写字母
private static final int[] captchaCharType = {TYPE_DEFAULT, TYPE_ONLY_NUMBER,
TYPE_ONLY_CHAR, TYPE_ONLY_UPPER, TYPE_ONLY_LOWER, TYPE_NUM_AND_UPPER};
private static final int[] captchaFont = {Captcha.FONT_1,
Captcha.FONT_2, Captcha.FONT_3, Captcha.FONT_4,
Captcha.FONT_5, Captcha.FONT_6, Captcha.FONT_7,
Captcha.FONT_8, Captcha.FONT_9, Captcha.FONT_10};
public static Captcha code(){
try {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletResponse response = requestAttributes.getResponse();
// 设置请求头为输出图片类型
response.setContentType("image/gif");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// 输出图片流
int captchaType = rm.nextInt(3);
Captcha targetCaptcha = null;
if (captchaType == 0) {
// 三个参数分别为宽、高、位数
targetCaptcha = new SpecCaptcha(WIDTH,HEIGHT,CAPTCHA_LEN);
} else if (captchaType == 1) {
// gif类型
targetCaptcha = new GifCaptcha(WIDTH, HEIGHT);
} else {
// 算术类型
targetCaptcha = new ArithmeticCaptcha(WIDTH, HEIGHT);
targetCaptcha.setLen(OPERATION_LEN); // 几位数运算,默认是两位
}
// 设置字体
targetCaptcha.setFont(new Font(Font.DIALOG_INPUT, Font.PLAIN, FINT_SIZE)); // 有默认字体,可以不用设置
// 设置类型,纯数字、纯字母、字母数字混合
targetCaptcha.setFont(captchaFont[rm.nextInt(captchaFont.length)]);
targetCaptcha.setCharType(captchaCharType[rm.nextInt(captchaCharType.length)]);
targetCaptcha.out(response.getOutputStream());
String content = captcha.text().toLowerCase();
return content;
} catch (Exception e) {
e.printStackTrace();
throw new MyException("图灵码生成出现问题,请刷新重试或者联系管理员");
}
}
}
2、新版
新版本新加了中文验证,并且解决旧版本的一些乱码问题
<!--图灵验证码 开始-->
<dependency>
<groupId>io.github.eternalstone</groupId>
<artifactId>captcha-core</artifactId>
<version>3.0.0</version>
</dependency>
<!--图灵验证码 结束-->
/**
* 验证码相关的工具类 官网 https://gitee.com/eternalstone/EasyCaptchaBoot
*/
public class CaptchaCodeUtil {
private static final int WIDTH = 200;
private static final int HEIGHT = 40;
private static final int CAPTCHA_LEN = 6;
private static final int OPERATION_LEN = 3;
private static final int FONT_SIZE = 40;
private static final Random rm = new Random();
// TYPE_DEFAULT 数字和字母混合 TYPE_ONLY_NUMBER 纯数字
// TYPE_ONLY_CHAR 纯字母 TYPE_ONLY_UPPER 纯大写字母
// TYPE_ONLY_LOWER 纯小写字母 TYPE_NUM_AND_UPPER 数字和大写字母
private static final int[] captchaCharType = {TYPE_DEFAULT, TYPE_ONLY_NUMBER,
TYPE_ONLY_CHAR, TYPE_ONLY_UPPER, TYPE_ONLY_LOWER, TYPE_NUM_AND_UPPER};
private static final int[] captchaFont = {Captcha.FONT_1,
Captcha.FONT_2, Captcha.FONT_3, Captcha.FONT_4,
Captcha.FONT_5, Captcha.FONT_6, Captcha.FONT_7,
Captcha.FONT_8, Captcha.FONT_9, Captcha.FONT_10};
public static String code() {
try {
HttpServletResponse response = getHttpServletResponse();
Captcha captcha = CaptchaFactory.getCaptcha(getCaptcha());//创建验证码实例对象
TextEntry text = captcha.createText();
captcha.out(response.getOutputStream(), text);
return text.getKey().toLowerCase();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 获取设置好的response对象=
*/
private static HttpServletResponse getHttpServletResponse() {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
assert requestAttributes != null;
HttpServletResponse response = requestAttributes.getResponse();
// 设置请求头为输出图片类型
assert response != null;
response.setContentType("image/gif");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
return response;
}
/**
* 获取实体的配置对象
*/
private static CaptchaProperty getCaptcha() {
CaptchaProperty property = new CaptchaProperty();
property.setWidth(WIDTH);//配置验证码宽度
property.setHeight(HEIGHT);//配置验证码高度
property.setCharType(captchaCharType[rm.nextInt(captchaCharType.length)]);//配置验证码字符类性,纯数字、纯字母、字母数字混合
property.setFont(captchaFont[rm.nextInt(captchaFont.length)]);//配置内置字体
property.setLength(CAPTCHA_LEN);//配置验证码几个字符
setCaptchaType(property);
return property;
}
/**
* 设置验证码类型
*/
private static void setCaptchaType(CaptchaProperty property) {
int captchaType = rm.nextInt(5);
CaptchaEnum type;
switch (captchaType) {
case 0:
type = CaptchaEnum.SPEC;//普通字符验证码
break;
case 1:
type = CaptchaEnum.GIF;//GIF动图验证码
break;
case 2:
//Captcha.FONT_8 FONT_3 算术字体8 和 3会乱码
int i = captchaFont[rm.nextInt(captchaFont.length)];
while (i == Captcha.FONT_8 || i == Captcha.FONT_3){
i = captchaFont[rm.nextInt(captchaFont.length)];
}
type = CaptchaEnum.ARITHMETIC;//算术验证码
property.setLength(OPERATION_LEN);//算术三位算
break;
case 3:
type = CaptchaEnum.CHINESE;//中文验证码
property.setLength(OPERATION_LEN);//中文三位
property.setFont(new Font("楷体", Font.PLAIN, FONT_SIZE));//设置系统字体 防止中文乱码
break;
default:
type = CaptchaEnum.CHINESE_GIF;//中文GIF动图验证码
property.setLength(OPERATION_LEN);//中文动态三位
property.setFont(new Font("楷体", Font.PLAIN, FONT_SIZE));//设置系统字体 防止中文乱码
break;
}
property.setCaptcha(type);
}
}