• spring boot3登录开发-2(1图形验证码接口实现)


    ⛰️个人主页:     蒾酒

    🔥系列专栏:《spring boot实战》

    🌊山高路远,行路漫漫,终有归途。


    目录

    前置条件

    内容简介

    图形验证码接口实现

    导入糊涂工具依赖

    接口分析

    编写验证码接口

    测试验证码接口


    前置条件

    本文衔接上文,请从上文开始

    spring boot3x登录开发-上(整合jwt)-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/qq_62262918/article/details/135964626?spm=1001.2014.3001.5502

    内容简介

    上文我们已经整合好了jwt,本文我们开始实现图形验证码接口的实现。

    • 通过糊涂工具包的图形验证码工具完成获取验证码接口
    • 通过redis缓存key(验证码id)-value(验证码内容)

    图形验证码接口实现

    导入糊涂工具依赖

    pom.xml:

    1. <dependency>
    2. <groupId>cn.hutoolgroupId>
    3. <artifactId>hutool-allartifactId>
    4. <version>5.8.25version>
    5. dependency>

    接口分析

    前端的登录表单有个验证码id字段,第一次打开登录页面默认会请求验证码接口,那么后端验证码接口将返回验证码图片的base64编码和验证码id,前端需要将验证码id保存到表单对象的验证码id字段,同时把验证码图片显示。用户填写账密、验证码点击登录,表单对象将携带账密和验证码id和用户键入的验证码内容提交到后端,后端需要根据此验证码id去查redis跟用户提交的比对

    分析完我们就可以知道怎样设计这个接口了。

    接口接收一个验证码id参数,判断这个参数如果是null则生成一个验证码id,不为null则直接拿它去生成redis缓存验证码内容的key,接着将验证码图片同id返回给前端。

     首先定义验证码接口数据对象

    1. import lombok.Builder;
    2. import lombok.Data;
    3. /**
    4. * @author mijiupro
    5. */
    6. @Data
    7. @Builder
    8. public class CaptchaVO {
    9. //验证码id
    10. private String captchaId;
    11. //验证码图片base64编码
    12. private String captchaImage;
    13. }

    编写验证码接口

    这里用到了redis,需要整合好:

    Spring Boot3整合Redis-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/qq_62262918/article/details/136067550?spm=1001.2014.3001.5501

    1. import cn.hutool.captcha.CaptchaUtil;
    2. import cn.hutool.captcha.CircleCaptcha;
    3. import com.mijiu.commom.model.vo.CaptchaVO;
    4. import io.swagger.v3.oas.annotations.Operation;
    5. import io.swagger.v3.oas.annotations.tags.Tag;
    6. import org.springframework.data.redis.core.StringRedisTemplate;
    7. import org.springframework.web.bind.annotation.GetMapping;
    8. import org.springframework.web.bind.annotation.RequestMapping;
    9. import org.springframework.web.bind.annotation.RestController;
    10. import java.util.Optional;
    11. import java.util.UUID;
    12. import java.util.concurrent.TimeUnit;
    13. /**
    14. * @author mijiupro
    15. */
    16. @RestController
    17. @RequestMapping("/Captcha")
    18. @Tag(name = "验证码接口", description = "验证码接口相关操作")
    19. public class CaptchaController {
    20. private final StringRedisTemplate stringRedisTemplate;
    21. public CaptchaController(StringRedisTemplate stringRedisTemplate) {
    22. this.stringRedisTemplate = stringRedisTemplate;
    23. }
    24. @GetMapping("/graph-captcha")
    25. @Operation(summary = "获取验证码")
    26. public CaptchaVO getCaptcha(String captchaId) {
    27. // 创建一个图像验证码宽度为130,高度为48,包含4个字符,干扰线10个
    28. CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(130, 48, 4, 10);
    29. // 获取验证码的文本
    30. String captchaText = circleCaptcha.getCode();
    31. // 获取验证码图片的Base64编码
    32. String captchaImageBase64Data = circleCaptcha.getImageBase64Data();
    33. // 如果没有传入captchaId,则生成一个随机字符串作为captchaId
    34. captchaId = Optional.ofNullable(captchaId).orElseGet(() -> UUID.randomUUID().toString());
    35. // 保存验证码文本到Redis中,有效期30秒
    36. stringRedisTemplate.opsForValue().set("captcha:" + captchaId, captchaText, 30, TimeUnit.SECONDS);
    37. return CaptchaVO.builder()
    38. .captchaId(captchaId)
    39. .captchaImage(captchaImageBase64Data)
    40. .build();
    41. }
    42. }

    测试验证码接口

    这里使用Knife4jConfig(swigger3)测试,也可以用浏览器地址栏、Postman等测试

    Spring Boot3整合knife4j(swagger3)_springboot3 knife4j-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/qq_62262918/article/details/135761392?spm=1001.2014.3001.5502

  • 相关阅读:
    Gson反序列化原理
    非线性有限元:基本理论与算法及基于Python、Fortran程序实现与案例分析
    XSS跨站脚本攻击
    docker介绍及入门举例
    Vue面试题-答案、例子
    JDBC学习篇(三)
    Win10无法访问移动硬盘怎么解决
    程序分析与优化 - 8 寄存器分配
    ElasticSearch(九):ELK 架构
    mysql 查询
  • 原文地址:https://blog.csdn.net/qq_62262918/article/details/136064820