# captcha_demo
**Repository Path**: MoBinMing/captcha_demo
## Basic Information
- **Project Name**: captcha_demo
- **Description**: 基于使用 Spring Boot、Hutool 和 Redis 实现图片验证码
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2023-07-21
- **Last Updated**: 2023-07-21
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
文章出处:https://mp.weixin.qq.com/s/8c1cKLGRhOJ4gAi7R3v58Q
# captcha_demo

#### 介绍
使用 Spring Boot、Hutool 和 Redis 实现图片验证码
原创 路条编程 路条编程 2023-07-19 19:30 发表于北京
下面是使用 Spring Boot、Hutool 和 Redis 实现图片验证码生成和缓存的示例:
添加依赖
在你的 Maven 项目中添加以下依赖:
Maven:
```
cn.hutool
hutool-all
5.7.4
org.springframework.boot
spring-boot-starter-data-redis
```
配置 Redis
application.yml 文件中添加 Redis 的配置:
```
spring:
redis:
host: localhost
port: 6379
```
生成图片验证码
创建一个用于生成图片验证码的 Controller,并使用 Hutool 的 CaptchaUtil 生成验证码图像:
```
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
@RestController
public class CaptchaController {
private final RedisTemplate redisTemplate;
@Autowired
public CaptchaController(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
@GetMapping("/captcha")
public void generateCaptcha(HttpServletResponse response) throws IOException {
// 生成验证码
CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);
String code = captcha.getCode();
// 将验证码存入 Redis,设置过期时间为 5 分钟
ValueOperations valueOperations = redisTemplate.opsForValue();
valueOperations.set("captcha:" + code, code, 5, TimeUnit.MINUTES);
// 将验证码图像写入 HTTP 响应
response.setContentType("image/png");
captcha.write(response.getOutputStream());
}
}
```
验证图片验证码
创建一个用于验证图片验证码的 Controller 方法,并从 Redis 中获取验证码进行验证:
```
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class VerificationController {
private final RedisTemplate redisTemplate;
@Autowired
public VerificationController(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
@PostMapping("/verify")
public String verifyCaptcha(@RequestParam String captchaCode) {
ValueOperations valueOperations = redisTemplate.opsForValue();
String redisKey = "captcha:" + captchaCode;
// 从 Redis 中获取验证码并验证
String storedCaptcha = valueOperations.get(redisKey);
if (storedCaptcha != null && storedCaptcha.equals(captchaCode)) {
// 验证成功
redisTemplate.delete(redisKey);
return " 验证成功!";
} else {
// 验证失败
return "验证失败!";
}
}
}
```
这样,你就可以通过访问 /captcha 接口获取生成的图片验证码,并通过提交 /verify 接口来验证用户输入的验证码。验证码将被存储在 Redis 中,以便进行验证和过期处理。