分类目录

链接

2022 年 4 月
 123
45678910
11121314151617
18192021222324
252627282930  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > JAVA > 正文
springboot登录失败3次后需要验证码的设计及实现
JAVA 暂无评论 阅读(155)

前言:

不用验证码是方便登陆,用验证码是为了防止暴力破解。为了即能满足方便,同时防止暴力破解,需要使用用户登陆失败3次后出现验证码。

 

1. 登陆失败次数记录, 在login 中查询并记录用户登陆失败次数

WARNING: 无论验证成功还是失败,前端都刷新验证码。后端验证码用一次就失效

  1. @RequestMapping("/login")
  2.  
  3. public Result login(@RequestBody LoginBody loginBody){
  4.  
  5. int errorTimes = redis.get(loginBody.getUsername()+"-loginError");
  6.      bool needKaptcha = false;
  7.  
  8.     if(errorTimes>2){
  9.         needKaptcha = true;
  10.     }
  11.  
  12.      if(needKaptcha){
  13.           String kaptchaCode = redis.get("kaptcha-"+loginBody.getKaptchaUUID());
  14.              redis.remove("kaptcha-"+loginBody.getKaptchaUUID());//验证码用一次就作废,防止被利用
  15.           if( ! kaptchaCode.equels(loginBody.getKaptchaCode())){
  16.                return Result.error("验证码错误!");
  17.      }
  18. }
  19.  
  20. //to do
  21. //验证用户账号密码
  22. if(!userService.login(loginBody)){
  23.      int errorTimes = redis.get(loginBody.getUsername()+"-loginError");
  24.      errorTimes++;
  25.      redis.set(loginBody.getUsername()+"-loginError",  errorTimes);
  26.      return Result.error("用户密码错误", errorTimes=errorTimes);
  27.  
  28.      }
  29.  
  30. }

2.  生成验证码

增加依赖

<!-- 验证码 -->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>

 

  1.  
  2. //KaptchaConfig
  3. @Configuration
  4. public class KaptchaConfig {
  5. @Bean
  6. public Producer kaptchaProducer(){
  7. Properties properties = new Properties();
  8. properties.setProperty("kaptcha.image.width","100");
  9. properties.setProperty("kaptcha.image.height","40");
  10. properties.setProperty("kaptcha.textproducer.font.size","32");
  11. properties.setProperty("kaptcha.textproducer.font.color","black");
  12. properties.setProperty("kaptcha.textproducer.char.string","0123456789QWERTYUIOPASSDFGHJKLZXCVBNM");//文本集合
  13. properties.setProperty("kaptcha.textproducer.char.length","4");//验证码长度
  14. properties.setProperty("kaptcha.noise.impl","com.google.code.kaptcha.impl.NoNoise"); //选择哪个干扰类
  15. DefaultKaptcha kaptcha = new DefaultKaptcha();
  16. Config config = new Config(properties);
  17. kaptcha.setConfig(config);
  18. return kaptcha;
  19. }
  20. }

 

  1.  
  2.  
  3.  
  4. @GetMapping("/kaptcha")
  5. public AjaxResult getKaptcha() throws IOException {
  6. String uuid = UUID.randomUUID().toString();
  7. //生成验证码
  8. String text = kaptchaProduce.createText();
  9. //将验证码保存到redis,有效期5分钟
  10. redisService.set(RedisKey.getKaptcha("kaptchaUUID-"+uuid),text,5, TimeUnit.MINUTES);
  11. //将验证码转换为图片
  12. BufferedImage image = kaptchaProduce.createImage(text);
  13. // 转换流信息写出
  14. FastByteArrayOutputStream os = new FastByteArrayOutputStream();
  15. ImageIO.write(image, "jpg", os);
  16. Map<String,String> res=new HashMap<>();
  17. res.put("kaptchaUUID",uuid);
  18. //res.put("kaptchaCode",text);
  19. res.put("img", "data:image/jpeg;base64,"+Base64.encode(os.toByteArray()));
  20. return toAjax(res);
  21. }

============ 欢迎各位老板打赏~ ===========

【上篇】
【下篇】

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:springboot登录失败3次后需要验证码的设计及实现 | Bruce's Blog

发表评论

留言无头像?