问题 : 登录所需验证码
1, 引入依赖 hutool 工具包
hutool工具官网地址
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
2,简单案例
@ApiOperation("获取验证码")
@GetMapping("/captchaImage")
public R getCode(HttpServletResponse response) throws IOException {
boolean enabled = captchaProperties.getEnabled();
// 定义图形验证码的长和宽
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(116, 36,4,5);
//code值
String code = lineCaptcha.getCode();
// 保存验证码信息
// verifyKey 作为Redis 中的key值
String uuid = IdUtils.simpleUUID();
String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
//存入Redis,用于校验code值
redisService.setCacheObject(verifyKey , code, 5L, TimeUnit.MINUTES);
System.out.println(code);
String imageBase64 ="data:image/jpg;base64,";
imageBase64 += lineCaptcha.getImageBase64();
return R.success().data("data",imageBase64).data("uuid", uuid).data("captchaEnabled", enabled);
}
运行结果 :