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

⛰️个人主页: 蒾酒

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

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


目录

前置条件

内容简介

图形验证码接口实现

导入糊涂工具依赖

接口分析

编写验证码接口

测试验证码接口


前置条件

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

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

内容简介

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

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

图形验证码接口实现

导入糊涂工具依赖

pom.xml:

XML 复制代码
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.25</version>
</dependency>

接口分析

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

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

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

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

java 复制代码
import lombok.Builder;
import lombok.Data;

/**
 * @author mijiupro
 */
@Data
@Builder
public class CaptchaVO {
    //验证码id
    private  String captchaId;
    //验证码图片base64编码
    private  String captchaImage;
}

编写验证码接口

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

Spring Boot3整合Redis-CSDN博客https://blog.csdn.net/qq_62262918/article/details/136067550?spm=1001.2014.3001.5501

java 复制代码
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.mijiu.commom.model.vo.CaptchaVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author mijiupro
 */
@RestController
@RequestMapping("/Captcha")
@Tag(name = "验证码接口", description = "验证码接口相关操作")
public class CaptchaController {
    private final StringRedisTemplate stringRedisTemplate;

    public CaptchaController(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }
    
    @GetMapping("/graph-captcha")
    @Operation(summary = "获取验证码")
    public CaptchaVO getCaptcha(String captchaId) {
        // 创建一个图像验证码宽度为130,高度为48,包含4个字符,干扰线10个
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(130, 48, 4, 10);
        // 获取验证码的文本
        String captchaText = circleCaptcha.getCode();
        // 获取验证码图片的Base64编码
        String captchaImageBase64Data = circleCaptcha.getImageBase64Data();
        // 如果没有传入captchaId,则生成一个随机字符串作为captchaId
        captchaId = Optional.ofNullable(captchaId).orElseGet(() -> UUID.randomUUID().toString());
        // 保存验证码文本到Redis中,有效期30秒
        stringRedisTemplate.opsForValue().set("captcha:" + captchaId, captchaText, 30, TimeUnit.SECONDS);

        return CaptchaVO.builder()
                .captchaId(captchaId)
                .captchaImage(captchaImageBase64Data)
                .build();
    }

}

测试验证码接口

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

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

相关推荐
小科先生8 小时前
Java springboot调用数据库的基本流程
java·数据库·spring boot
秋饼9 小时前
GPT-Live 全双工语音 API Java 服务端落地实战:事件协议、会话恢复与生产踩坑
java·ai·技术分享·后端开发
SL_staff9 小时前
为什么企业级自动化不能只靠AI编排?——逻辑引擎在确定性业务场景中的不可替代性
java·开源·产品
时间的拾荒人10 小时前
Redis 集合类型详解:Set 与 Zset
redis·缓存·面试
Pniubi10 小时前
MySQL靠什么实现事务的?事务隔离级别有哪些?
java·数据库·mysql
松仔log11 小时前
Kotlin中级——callbackFlow
java·前端·kotlin
鶴哥只手遮天11 小时前
《Redis 8.10.3 深度剖析》系列 ·第 17 章. 一个迟到十六年的类型
redis·后端·开源
知守观12 小时前
同一个团队两套分包:DDD 四层落地后,domain 层最大的类还是 5885 行
java·后端·领域驱动设计
文丑颜不良啊12 小时前
LeetCode2541~2560题目解析
java·leetcode
瑞雪兆丰年兮12 小时前
JavaWeb-day11-SpringBootWeb案例(文件上传)
java·运维·服务器·javaweb·oss