pom.xml
com.github.whvcse
easy-captcha
1.6.2
生成验证码:
public NutMap getCode() {
String uuid = UUID.randomUUID().toString().replace("-", "");
ArithmeticCaptcha captcha = new ArithmeticCaptcha(120, 40);
captcha.getArithmeticString(); // 获取运算的公式:3+2=?
String text = captcha.text();
redisService.setex(RedisConstant.REDIS_CAPTCHA_KEY + uuid, 180, text);
return NutMap.NEW().addv("key", uuid).addv("codeUrl", captcha.toBase64());
}
验证验证码:(表单传递验证码及验证码key)
public void checkCode(String key, String code) throws CaptchaException {
String codeFromRedis = redisService.get(RedisConstant.REDIS_CAPTCHA_KEY + key);
if (Strings.isBlank(code)) {
throw new CaptchaException("请输入验证码");
}
if (Str