SpringBoot实现验证码功能

SpringBoot实现验证码功能

1.引入maven依赖

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

2.生成验证码

@GetMapping("captcha")
	public Result captcha() throws IOException{
		
		//算术类型
		ArithmeticCaptcha captcha = new ArithmeticCaptcha();
		
		//中文类型验证吗  
		//ChineseCaptcha captcha = new ChineseCaptcha();
		
        // 英文与数字验证码
     // SpecCaptcha captcha = new SpecCaptcha();

		//英文与数字动态验证码
        //GifCaptcha captcha = new GifCaptcha();
 
        //中文动态验证码
	    //ChineseGifCaptcha captcha = new ChineseGifCaptcha();
		//几位数运算   默认是两位
		captcha.setLen(2);
		
		//获取运算结果
		String result = captcha.text();
		
		log.info("===============获取运算结果为=========:{}",result);
		
		String key = UUID.randomUUID().toString();
		redisTemplate.opsForValue().set(key,result,2,TimeUnit.MINUTES);
		Map map = new HashMap();
		map.put("key", key);
		map.put("img", captcha.toBase64());
		return Result.success("图片验证码", map);
}

3.前端

 <el-col>
    <el-form-item>
          <img
             style="width: 85%; height: 35px; float: right"
             class="pointer"
             :src="src"
              alt=""
              @click="refreshCaptcha"
           />
    </el-form-item>
</el-col>


<script>
   refreshCaptcha: function () {
      getImgCode().then((res) => {
        console.log(res);
        this.src = res.data.img;
        //这个 登录携带的参数 根据key 要从redis中  获取正确的验证码运算结果
        this.loginForm.key = res.data.key;
      });
    },
</script>


版权声明:本文为博主原创文章,遵循 CC 4.0 BY 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/HackerZhou/article/details/125511168