springboot 生成登录验证码

265 阅读1分钟

大家好,我是泉城IT,这里跟大家分享一下,如何在springboot中,生成登录的验证码图片

electronics-hi-tech-telasm-wallpaper-tl114.jpg

引入jar包

<dependency>
            <groupId>com.github.whvcse</groupId>
            <artifactId>easy-captcha</artifactId>
        </dependency>

配置验证码生成的图片大小

#登录图形验证码有效时间/分钟
loginCode:
 expiration: 2
 codeKey: "validate_code"
 width: 108
 height: 28
 length: 4

后台获取验证码接口

  @ApiOperation("获取验证码")
   @GetMapping(value = "/code")
   public DataResponseBody getCode() {
       Captcha captcha = new SpecCaptcha(width, height, length);
       String createText = captcha.text();
       String uuid = codeKey + IdUtil.simpleUUID();
       // 保存
       redisUtils.set(uuid, createText, expiration, TimeUnit.MINUTES);//存到redis中
       // 验证码信息
       Map<String, Object> imgResult = new HashMap<String, Object>(4) {{
           put("img", captcha.toBase64());
           put("uuid", uuid);
       }};
       return new DataResponseBody(imgResult);
   }

完结

代码很简单,希望可以帮到大家,有好的技术一起分享给大家,不见不散