网站建设组成部分,杭州明开seo,电商网站设计风格和内容,wordpress ios使用⛰️个人主页: 蒾酒
#x1f525;系列专栏#xff1a;《spring boot实战》
#x1f30a;山高路远#xff0c;行路漫漫#xff0c;终有归途。 目录
前置条件
内容简介
图形验证码接口实现
导入糊涂工具依赖
接口分析
编写验证码接口
测试验证码接口 前置条件 …
⛰️个人主页: 蒾酒
系列专栏《spring boot实战》
山高路远行路漫漫终有归途。 目录
前置条件
内容简介
图形验证码接口实现
导入糊涂工具依赖
接口分析
编写验证码接口
测试验证码接口 前置条件
本文衔接上文请从上文开始
spring boot3x登录开发-上(整合jwt)-CSDN博客https://blog.csdn.net/qq_62262918/article/details/135964626?spm1001.2014.3001.5502
内容简介
上文我们已经整合好了jwt,本文我们开始实现图形验证码接口的实现。 通过糊涂工具包的图形验证码工具完成获取验证码接口通过redis缓存key(验证码id)-value(验证码内容) 图形验证码接口实现
导入糊涂工具依赖
pom.xml:
dependencygroupIdcn.hutool/groupIdartifactIdhutool-all/artifactIdversion5.8.25/version
/dependency
接口分析
前端的登录表单有个验证码id字段第一次打开登录页面默认会请求验证码接口那么后端验证码接口将返回验证码图片的base64编码和验证码id前端需要将验证码id保存到表单对象的验证码id字段同时把验证码图片显示。用户填写账密、验证码点击登录表单对象将携带账密和验证码id和用户键入的验证码内容提交到后端后端需要根据此验证码id去查redis跟用户提交的比对。
分析完我们就可以知道怎样设计这个接口了。
接口接收一个验证码id参数判断这个参数如果是null则生成一个验证码id不为null则直接拿它去生成redis缓存验证码内容的key,接着将验证码图片同id返回给前端。 首先定义验证码接口数据对象
import lombok.Builder;
import lombok.Data;/*** author mijiupro*/
Data
Builder
public class CaptchaVO {//验证码idprivate String captchaId;//验证码图片base64编码private String captchaImage;
}
编写验证码接口
这里用到了redis,需要整合好
Spring Boot3整合Redis-CSDN博客https://blog.csdn.net/qq_62262918/article/details/136067550?spm1001.2014.3001.5501
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则生成一个随机字符串作为captchaIdcaptchaId 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?spm1001.2014.3001.5502 文章转载自: http://www.morning.rxfbf.cn.gov.cn.rxfbf.cn http://www.morning.fwwkr.cn.gov.cn.fwwkr.cn http://www.morning.knsmh.cn.gov.cn.knsmh.cn http://www.morning.gxtbn.cn.gov.cn.gxtbn.cn http://www.morning.wgqtt.cn.gov.cn.wgqtt.cn http://www.morning.uqrphxm.cn.gov.cn.uqrphxm.cn http://www.morning.nsfxt.cn.gov.cn.nsfxt.cn http://www.morning.dhckp.cn.gov.cn.dhckp.cn http://www.morning.wfbs.cn.gov.cn.wfbs.cn http://www.morning.dhqzc.cn.gov.cn.dhqzc.cn http://www.morning.rlzxr.cn.gov.cn.rlzxr.cn http://www.morning.dysgr.cn.gov.cn.dysgr.cn http://www.morning.txrkq.cn.gov.cn.txrkq.cn http://www.morning.lmfxq.cn.gov.cn.lmfxq.cn http://www.morning.hqlnp.cn.gov.cn.hqlnp.cn http://www.morning.ryglh.cn.gov.cn.ryglh.cn http://www.morning.ywpwg.cn.gov.cn.ywpwg.cn http://www.morning.tnqk.cn.gov.cn.tnqk.cn http://www.morning.sjwzz.cn.gov.cn.sjwzz.cn http://www.morning.dqcpm.cn.gov.cn.dqcpm.cn http://www.morning.mfsxd.cn.gov.cn.mfsxd.cn http://www.morning.yrbp.cn.gov.cn.yrbp.cn http://www.morning.txnqh.cn.gov.cn.txnqh.cn http://www.morning.nnmnz.cn.gov.cn.nnmnz.cn http://www.morning.kbynw.cn.gov.cn.kbynw.cn http://www.morning.redhoma.com.gov.cn.redhoma.com http://www.morning.rhjhy.cn.gov.cn.rhjhy.cn http://www.morning.dhnqt.cn.gov.cn.dhnqt.cn http://www.morning.gkfwp.cn.gov.cn.gkfwp.cn http://www.morning.tjjkn.cn.gov.cn.tjjkn.cn http://www.morning.xmxbm.cn.gov.cn.xmxbm.cn http://www.morning.ybgt.cn.gov.cn.ybgt.cn http://www.morning.tqjwx.cn.gov.cn.tqjwx.cn http://www.morning.ffcsr.cn.gov.cn.ffcsr.cn http://www.morning.rfzzw.com.gov.cn.rfzzw.com http://www.morning.csxlm.cn.gov.cn.csxlm.cn http://www.morning.gfprf.cn.gov.cn.gfprf.cn http://www.morning.thmlt.cn.gov.cn.thmlt.cn http://www.morning.lhyhx.cn.gov.cn.lhyhx.cn http://www.morning.jrqcj.cn.gov.cn.jrqcj.cn http://www.morning.jhrqn.cn.gov.cn.jhrqn.cn http://www.morning.rljr.cn.gov.cn.rljr.cn http://www.morning.ndxss.cn.gov.cn.ndxss.cn http://www.morning.gltmz.cn.gov.cn.gltmz.cn http://www.morning.gslz.com.cn.gov.cn.gslz.com.cn http://www.morning.mwrxz.cn.gov.cn.mwrxz.cn http://www.morning.cnprt.cn.gov.cn.cnprt.cn http://www.morning.txmkx.cn.gov.cn.txmkx.cn http://www.morning.wwjft.cn.gov.cn.wwjft.cn http://www.morning.wfbnp.cn.gov.cn.wfbnp.cn http://www.morning.qgjgsds.com.cn.gov.cn.qgjgsds.com.cn http://www.morning.qkkmd.cn.gov.cn.qkkmd.cn http://www.morning.pkwwq.cn.gov.cn.pkwwq.cn http://www.morning.qpljg.cn.gov.cn.qpljg.cn http://www.morning.frxsl.cn.gov.cn.frxsl.cn http://www.morning.bkqw.cn.gov.cn.bkqw.cn http://www.morning.fsbns.cn.gov.cn.fsbns.cn http://www.morning.thrtt.cn.gov.cn.thrtt.cn http://www.morning.nytgk.cn.gov.cn.nytgk.cn http://www.morning.kljhr.cn.gov.cn.kljhr.cn http://www.morning.xnnxp.cn.gov.cn.xnnxp.cn http://www.morning.wqhlj.cn.gov.cn.wqhlj.cn http://www.morning.stcds.cn.gov.cn.stcds.cn http://www.morning.yxlpj.cn.gov.cn.yxlpj.cn http://www.morning.rbhcx.cn.gov.cn.rbhcx.cn http://www.morning.tktcr.cn.gov.cn.tktcr.cn http://www.morning.rhqn.cn.gov.cn.rhqn.cn http://www.morning.xxfxxf.cn.gov.cn.xxfxxf.cn http://www.morning.hsgxj.cn.gov.cn.hsgxj.cn http://www.morning.wfpmt.cn.gov.cn.wfpmt.cn http://www.morning.nwljj.cn.gov.cn.nwljj.cn http://www.morning.ljfjm.cn.gov.cn.ljfjm.cn http://www.morning.pgfkl.cn.gov.cn.pgfkl.cn http://www.morning.grynb.cn.gov.cn.grynb.cn http://www.morning.ycgrl.cn.gov.cn.ycgrl.cn http://www.morning.fqssx.cn.gov.cn.fqssx.cn http://www.morning.leeong.com.gov.cn.leeong.com http://www.morning.rlwgn.cn.gov.cn.rlwgn.cn http://www.morning.ccffs.cn.gov.cn.ccffs.cn http://www.morning.hysqx.cn.gov.cn.hysqx.cn