网站首页 > 教程文章 正文
依赖导入
<!-- 添加图形验证码依赖 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-captcha</artifactId>
<version>5.8.5</version>
</dependency>
————————————————
/**
* 生成验证码图片
* @return
*/
@ApiOperation("获取图形验证码")
@GetMapping("/identifyImage")
public Result<String> identifyImage(HttpServletResponse response,
@ApiParam(value = "图形验证码id,无值:生成验证码,有值:刷新验证码")
@RequestParam(name = "codeId", required = false) String codeId) throws IOException {
// 创建验证码,设置宽、高、长度、干扰线数量
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 90, 4, 100);
// 获取验证码字符串,赋值code
String code = lineCaptcha.getCode();
if (codeId == null) {
// IdWorker.getId():IdWorker工具类生成唯一ID,并转换成String类型
codeId = String.valueOf(IdWorker.getId());
// 将codeId、code.toUpperCase()、过期时间60秒:存储入Redis中
// code.toUpperCase():code装换成大写形式存储
redisOps.set(codeId,code.toUpperCase(),60);
} else {
redisOps.set(codeId,code.toUpperCase(),60);
}
// 将图片验证码codeId设置请求头中
response.setHeader("codeId", codeId);
// 获取向客户端发送响应数据的输出流
try (ServletOutputStream outputStream = response.getOutputStream()) {
// 验证码图片数据写入到输出流
lineCaptcha.write(outputStream);
} catch (Exception e) {
throw new AuthException("图形验证码输出错误");
}
return Result.succ(codeId);
}
————————————————
猜你喜欢
- 2025-05-27 还在为 Spring Boot3 整合 Easy Excel 发愁?一文搞定!
- 2025-05-27 7种方式,教你提升 SpringBoot 项目的吞吐量
- 2025-05-27 Tomcat处理HTTP请求流程解析
- 2025-05-27 SpringBoot中六种设计模式应用案例二
- 2025-05-27 生产实战:循环继承依赖 导致web 项目启动失败,总是超时
- 2025-05-27 Java Web应用调优线程池:没你想的那么复杂
- 2025-05-27 SpringMVC异常处理句柄这些细节你知道吗?
- 2025-05-27 惊呆了!Controller接口返回值支持17种逆天类型
- 2025-05-27 【干货】EasyExcel确实好用,Springboot+EasyExcel实操
- 2025-05-27 Spring Boot 应用如何防护 XSS 攻击
- 最近发表
- 标签列表
-
- location.href (44)
- document.ready (36)
- git checkout -b (34)
- 跃点数 (35)
- 阿里云镜像地址 (33)
- qt qmessagebox (36)
- mybatis plus page (35)
- vue @scroll (38)
- 堆栈区别 (33)
- 什么是容器 (33)
- sha1 md5 (33)
- navicat导出数据 (34)
- 阿里云acp考试 (33)
- 阿里云 nacos (34)
- redhat官网下载镜像 (36)
- srs服务器 (33)
- pico开发者 (33)
- https的端口号 (34)
- vscode更改主题 (35)
- 阿里云资源池 (34)
- os.path.join (33)
- redis aof rdb 区别 (33)
- 302跳转 (33)
- http method (35)
- js array splice (33)