使用图片验证码进行登录验证

妖狐艹你老母 2022-08-28 01:47 392阅读 0赞

需求:后端系统登录时使用图片验证码验证登录

效果:

97cb7cdf761348639c5df974e66bd751.jpg

使用 4 位数字加字母组合验证码登录,相关代码为:

  1. import cn.hutool.captcha.CaptchaUtil;
  2. import cn.hutool.captcha.LineCaptcha;
  3. @RequestMapping("/getCode")
  4. public ResultVO getCode(HttpServletRequest request, HttpServletResponse response) {
  5. String imageBase64 = "";
  6. Map<String, String> result = new HashMap<>();
  7. try {
  8. // 定义图片大小
  9. LineCaptcha captcha = CaptchaUtil.createLineCaptcha(200, 100, 4, 300);
  10. response.setContentType("image/jpeg");
  11. response.setHeader("Pragma", "No-cache");
  12. imageBase64 = "data:image/png;base64," + captcha.getImageBase64();
  13. String code = captcha.getCode();
  14. logger.info("生成的图片验正码:{}", code);
  15. String codeKey = UUID.randomUUID().toString();
  16. redisSupport.set(codeKey, captcha.getCode());
  17. result.put("code",imageBase64);
  18. result.put("codeKey", codeKey);
  19. } catch (Exception e) {
  20. logger.error("生成图片验正码异常", e);
  21. return new ResultVO<>(ResultVO.FAIL, "获取图片失败");
  22. }
  23. return new ResultVO<>(result, ResultVO.SUCCESS, "获取图片成功");
  24. }

这部分代码实现的是生成图片验证码的功能,使用的是 hutool 的工具包,需要以 key-value 的形式将生成的图片和用户关联起来,所以我们将结果以 Map 的形式返回给前端。其中使用的 redisSupport 可根据实际情况进行替换,其实就是 Redis 的一个工具包,后续会单独贴出来。 Redis 工具包请参考文章:Redis 工具实用篇_jiaomubai的博客-CSDN博客

正式登录接口的代码如下:

  1. @PostMapping("/login")
  2. public void login(@RequestParam("username") String username, @RequestParam("password") String password, @RequestParam("code") String code, @RequestParam("codeKey") String codeKey, HttpServletResponse resp, HttpServletRequest request) {
  3. JSONObject result = new JSONObject();
  4. try {
  5. String redisCode = (String) redisSupport.get(codeKey);
  6. log.info("redisCode:{}",redisCode);
  7. if(StringUtils.isEmpty(code) || StringUtils.isEmpty(redisCode) || !code.equals(redisCode)){
  8. result.put("error_description", "验证码错误");
  9. result.put("error", "invalid_grant");
  10. resp.setStatus(400);
  11. redisSupport.del(codeKey);
  12. return;
  13. }
  14. } catch (Exception e) {
  15. log.error("登录接口错误,错误信息:{}", e);
  16. } finally {
  17. try {
  18. resp.setContentType(ContentType.JSON.getValue());
  19. ServletOutputStream out = resp.getOutputStream();
  20. OutputStreamWriter ow = new OutputStreamWriter(out, "UTF-8");
  21. ow.write(result.toJSONString());
  22. ow.flush();
  23. ow.close();
  24. } catch (Exception e) {
  25. log.error("登录接口错误,错误信息:{}", e);
  26. }
  27. }
  28. }

其中入参中 username 为登录的用户名,password 为登录密码,code 为用户输入的图片验证码的答案,codekey 为与 code 关联的 key,因为在获取验证码的接口中(/getCode)我们已经将 code 和 codekey 存储在了 Redis 中,所以此处可直接根据 codekey 去获取 Redis 中的与 codekey 对应的验证码,之后与用户输入的 code 进行比较即可。其中使用的 redisSupport 可根据实际情况进行替换,其实就是 Redis 的一个工具包,后续会单独贴出来。 Redis 工具包请参考文章:Redis 工具实用篇_jiaomubai的博客-CSDN博客

还有一种使用简单运算的登录验证方式在下一篇文章中说明~

发表评论

表情:
评论列表 (有 0 条评论,392人围观)

还没有评论,来说两句吧...

相关阅读

    相关 登录图片验证

    页面刷新时,自动发送获取验证码到服务端 输入验证码登录,登录成功返回token 通过token获取用户详情,跳转主页 //获取验证码图片 @GetMappi

    相关 登录验证

    验证码的作用是区分人与机器,防止机器刷数据; 验证码的验证分了两步,一步是请求获取验证码,一步是前台传的验证码与后台的验证码进行对比判断后进行后续操作; 在MyEclips