SpringBoot实现验证码功能

SpringBoot实现验证码功能

1.引入maven依赖

        
			com.github.whvcse
			easy-captcha
			1.6.2
		

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.前端

 
    
          
    



你可能感兴趣的:(spring,boot,vue.js)