Files
AIGC/demo/EMAIL_VERIFICATION_SIMPLE.md
AIGC Developer 68574fe33f 实现邮箱验证码登录功能
- 新增VerificationCodeService:验证码生成、发送、验证
- 新增VerificationCodeController:验证码相关API接口
- 扩展AuthApiController:支持邮箱验证码登录
- 扩展UserRepository和UserService:支持邮箱查找用户
- 使用内存存储验证码,无需Redis依赖
- 添加腾讯云配置支持(可选)
- 实现安全机制:频率限制、有效期、一次性使用
- 添加详细文档说明
2025-10-23 10:27:36 +08:00

101 lines
2.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 邮箱验证码登录 - 简化版本
## 功能说明
已实现基于邮箱验证码的登录功能,**无需Redis**,使用内存存储验证码。
## 技术实现
### 存储方式
- **验证码存储**:使用 `ConcurrentHashMap` 内存存储
- **频率限制**:使用 `ConcurrentHashMap` 存储发送时间
- **自动过期**:使用 `ScheduledExecutorService` 定时清理过期验证码
### 安全机制
- ✅ 验证码6位数字
- ✅ 5分钟有效期
- ✅ 60秒发送频率限制
- ✅ 一次性使用(验证后删除)
- ✅ 线程安全存储
## 测试方法
### 1. 启动应用
```bash
cd demo
mvn spring-boot:run
```
### 2. 测试API
#### 发送验证码
```bash
curl -X POST http://localhost:8080/api/verification/email/send \
-H "Content-Type: application/json" \
-d '{"email": "test@example.com"}'
```
**响应**
```json
{
"success": true,
"message": "验证码发送成功"
}
```
**注意**:验证码会在应用日志中输出,格式如:
```
模拟发送邮件验证码到: test@example.com, 验证码: 123456
```
#### 验证码登录
```bash
curl -X POST http://localhost:8080/api/auth/login/email \
-H "Content-Type: application/json" \
-d '{"email": "test@example.com", "code": "123456"}'
```
**成功响应**
```json
{
"success": true,
"message": "登录成功",
"data": {
"user": {
"id": 1,
"username": "test",
"email": "test@example.com",
"role": "ROLE_USER",
"points": 100
},
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
}
```
## 优势
1. **无需外部依赖**不需要安装Redis
2. **简单部署**直接运行Spring Boot应用即可
3. **开发友好**:验证码在日志中可见,便于测试
4. **性能良好**:内存存储速度快
5. **线程安全**使用ConcurrentHashMap保证并发安全
## 限制
1. **单机部署**:验证码存储在内存中,多实例部署时无法共享
2. **重启丢失**:应用重启后验证码会丢失
3. **内存占用**:大量验证码会占用内存(通常不是问题)
## 生产环境建议
如果需要生产环境部署,建议:
1. **使用Redis**:多实例部署时共享验证码
2. **配置真实邮件服务**集成腾讯云SES或其他邮件服务
3. **添加监控**:监控验证码发送频率和成功率
## 下一步
现在可以开始修改前端登录页面,实现邮箱验证码登录界面。