赞
踩
Java图形验证码,支持gif、中文、算术等类型。
<!-- Java图形验证码 -->
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>
#图形验证码 配置
easyCaptcha:
enabled: true # 开启验证码
codeType: spec # 验证码类型 查看CaptchaCodeEnum类
expiration: 2 # 验证码过期时间 单位分钟
length: 4 # 验证码(内容)长度
width: 111 # 验证码宽度
height: 36 # 验证码高度
fontName: # 验证码字体
fontSize: 25 # 验证码字体大小
public enum CaptchaEnum {
spec, // 字母
arithmetic, // 算术
chinese, // 汉字
chinese_gif, // 中文闪图
gif, // 闪图
}
/** * TODO 图片验证码配置 */ @Data @Component public class EasyCaptchaConfig { /** * 是否启用验证码 */ @Value("${easyCaptcha.enabled}") private Boolean enabled = true; /** * 验证码配置 */ @Value("${easyCaptcha.codeType}") private CaptchaEnum codeType; /** * 验证码有效期 分钟 */ @Value("${easyCaptcha.expiration}") private Long expiration = 2L; /** * 验证码内容长度 */ @Value("${easyCaptcha.length}") private int length = 2; /** * 验证码宽度 */ @Value("${easyCaptcha.width}") private int width=111; /** * 验证码高度 */ @Value("${easyCaptcha.height}") private int height = 36; /** * 验证码字体 */ @Value("${easyCaptcha.fontName}") private String fontName; /** * 字体大小 */ @Value("${easyCaptcha.fontSize}") private int fontSize = 25; public CaptchaEnum getCodeType() { return codeType; } }
/** * TODO 图形验证码工具类 */ @Component public class EasyCaptChaUtil { @Autowired private EasyCaptchaConfig easyCaptchaConfig; //验证码配置 /** * 获取验证码 */ public Captcha getCaptcha() { if (Objects.isNull(easyCaptchaConfig)) { //验证码配置信息为空 easyCaptchaConfig = new EasyCaptchaConfig(); //new EasyCaptchaConfig() if (Objects.isNull(easyCaptchaConfig.getCodeType())) { //验证码类型为空 easyCaptchaConfig.setCodeType(CaptchaEnum.arithmetic);//默认算术类型 } } return switchCaptcha(easyCaptchaConfig);//返回验证码 } /** * 生成验证码 * * @param easyCaptcha 验证码配置 * @return Captcha */ private Captcha switchCaptcha(EasyCaptchaConfig easyCaptcha) { Captcha captcha; synchronized (this) { //一个线程访问一个对象中的synchronized(this)同步代码块时,其他试图访问该对象的线程将被阻塞,直到该线程执行完毕 switch (easyCaptcha.getCodeType()) { case arithmetic: // 算术类型 https://gitee.com/whvse/EasyCaptcha captcha = new FixedArithmeticCaptcha(easyCaptchaConfig.getWidth(), easyCaptchaConfig.getHeight()); // 几位数运算,默认是两位 captcha.setLen(easyCaptchaConfig.getLength()); break; case chinese: //中文类型 captcha = new ChineseCaptcha(easyCaptchaConfig.getWidth(), easyCaptchaConfig.getHeight()); captcha.setLen(easyCaptchaConfig.getLength()); break; case chinese_gif: //中文gif类型 captcha = new ChineseGifCaptcha(easyCaptchaConfig.getWidth(), easyCaptchaConfig.getHeight()); captcha.setLen(easyCaptchaConfig.getLength()); break; case gif://gif类型 captcha = new GifCaptcha(easyCaptchaConfig.getWidth(), easyCaptchaConfig.getHeight()); captcha.setLen(easyCaptchaConfig.getLength()); break; case spec://字母 captcha = new SpecCaptcha(easyCaptchaConfig.getWidth(), easyCaptchaConfig.getHeight()); captcha.setLen(easyCaptchaConfig.getLength()); break; default: throw new RuntimeException("验证码配置信息错误!正确配置查看 LoginCodeEnum "); } } if(StringUtils.isNotBlank(easyCaptchaConfig.getFontName())){//当字体为空 // 设置默认字体 captcha.setFont(new Font(easyCaptchaConfig.getFontName(), Font.PLAIN, easyCaptchaConfig.getFontSize())); } return captcha; } public static class FixedArithmeticCaptcha extends ArithmeticCaptcha { public FixedArithmeticCaptcha(int width, int height) { super(width, height); } @Override protected char[] alphas() { // 生成随机数字和运算符 int n1 = num(1, 10), n2 = num(1, 10); int opt = num(3); // 计算结果 int res = new int[]{n1 + n2, n1 - n2, n1 * n2}[opt]; // 转换为字符运算符 char optChar = "+-x".charAt(opt); this.setArithmeticString(String.format("%s%c%s=?", n1, optChar, n2)); this.chars = String.valueOf(res); return chars.toCharArray(); } } }
public ResponseEntity test() {
Captcha captcha = easyCaptChaUtil.getCaptcha();//获取验证码
String uuid = UUID.randomUUID().toString();//生成随机数
String captchaValue = captcha.text(); //获取验证码的值
//当验证码类型为 arithmetic时且长度 >= 2 时,captcha.text()的结果有几率为浮点型
if (captcha.getCharType() - 1 == CaptchaEnum.arithmetic.ordinal() && captchaValue.contains(".")) {//验证码类型为算术类型
captchaValue = captchaValue.split("\\.")[0]; //截取整数部分
}
Map map = new HashMap();
map.put("uuid", uuid);
map.put("captcha", captcha.toBase64());
map.put("captchaValue", captchaValue);
return ResponseModel.result(ResUtil.success(map));
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。