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>

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值