193 lines
4.5 KiB
Markdown
193 lines
4.5 KiB
Markdown
|
|
# 邮箱验证码登录功能总结
|
|||
|
|
|
|||
|
|
## 功能概述
|
|||
|
|
|
|||
|
|
已实现基于邮箱验证码的登录功能,用户可以通过邮箱接收验证码进行登录,无需记住密码。
|
|||
|
|
|
|||
|
|
## 后端实现
|
|||
|
|
|
|||
|
|
### 1. 核心组件
|
|||
|
|
|
|||
|
|
#### VerificationCodeService
|
|||
|
|
- **功能**:验证码生成、发送、验证
|
|||
|
|
- **位置**:`src/main/java/com/example/demo/service/VerificationCodeService.java`
|
|||
|
|
- **主要方法**:
|
|||
|
|
- `generateVerificationCode()`: 生成6位数字验证码
|
|||
|
|
- `sendEmailVerificationCode(String email)`: 发送邮件验证码
|
|||
|
|
- `verifyEmailCode(String email, String code)`: 验证邮箱验证码
|
|||
|
|
|
|||
|
|
#### VerificationCodeController
|
|||
|
|
- **功能**:验证码相关API接口
|
|||
|
|
- **位置**:`src/main/java/com/example/demo/controller/VerificationCodeController.java`
|
|||
|
|
- **API接口**:
|
|||
|
|
- `POST /api/verification/email/send`: 发送邮件验证码
|
|||
|
|
- `POST /api/verification/email/verify`: 验证邮件验证码
|
|||
|
|
|
|||
|
|
#### AuthApiController (扩展)
|
|||
|
|
- **功能**:认证相关API,新增邮箱验证码登录
|
|||
|
|
- **位置**:`src/main/java/com/example/demo/controller/AuthApiController.java`
|
|||
|
|
- **新增接口**:
|
|||
|
|
- `POST /api/auth/login/email`: 邮箱验证码登录
|
|||
|
|
|
|||
|
|
### 2. 数据存储
|
|||
|
|
|
|||
|
|
#### Redis配置
|
|||
|
|
- **用途**:存储验证码和发送频率限制
|
|||
|
|
- **配置类**:`src/main/java/com/example/demo/config/RedisConfig.java`
|
|||
|
|
- **存储结构**:
|
|||
|
|
- `email_code:{email}`: 存储验证码,5分钟过期
|
|||
|
|
- `email_rate_limit:{email}`: 发送频率限制,60秒过期
|
|||
|
|
|
|||
|
|
#### 数据库扩展
|
|||
|
|
- **UserRepository**: 新增 `findByPhone()` 和 `existsByPhone()` 方法
|
|||
|
|
- **UserService**: 新增 `findByPhone()` 方法
|
|||
|
|
|
|||
|
|
### 3. 安全机制
|
|||
|
|
|
|||
|
|
#### 验证码安全
|
|||
|
|
- **长度**:6位数字
|
|||
|
|
- **有效期**:5分钟
|
|||
|
|
- **发送频率限制**:同一邮箱60秒内只能发送一次
|
|||
|
|
- **一次性使用**:验证成功后立即删除
|
|||
|
|
|
|||
|
|
#### 用户验证
|
|||
|
|
- **邮箱格式验证**:前端和后端双重验证
|
|||
|
|
- **用户存在性检查**:登录时验证用户是否存在
|
|||
|
|
- **JWT Token生成**:验证成功后生成访问令牌
|
|||
|
|
|
|||
|
|
## API接口文档
|
|||
|
|
|
|||
|
|
### 1. 发送邮件验证码
|
|||
|
|
|
|||
|
|
**请求**
|
|||
|
|
```http
|
|||
|
|
POST /api/verification/email/send
|
|||
|
|
Content-Type: application/json
|
|||
|
|
|
|||
|
|
{
|
|||
|
|
"email": "user@example.com"
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**响应**
|
|||
|
|
```json
|
|||
|
|
{
|
|||
|
|
"success": true,
|
|||
|
|
"message": "验证码发送成功"
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 2. 验证邮件验证码
|
|||
|
|
|
|||
|
|
**请求**
|
|||
|
|
```http
|
|||
|
|
POST /api/verification/email/verify
|
|||
|
|
Content-Type: application/json
|
|||
|
|
|
|||
|
|
{
|
|||
|
|
"email": "user@example.com",
|
|||
|
|
"code": "123456"
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**响应**
|
|||
|
|
```json
|
|||
|
|
{
|
|||
|
|
"success": true,
|
|||
|
|
"message": "验证码验证成功"
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 3. 邮箱验证码登录
|
|||
|
|
|
|||
|
|
**请求**
|
|||
|
|
```http
|
|||
|
|
POST /api/auth/login/email
|
|||
|
|
Content-Type: application/json
|
|||
|
|
|
|||
|
|
{
|
|||
|
|
"email": "user@example.com",
|
|||
|
|
"code": "123456"
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**响应**
|
|||
|
|
```json
|
|||
|
|
{
|
|||
|
|
"success": true,
|
|||
|
|
"message": "登录成功",
|
|||
|
|
"data": {
|
|||
|
|
"user": {
|
|||
|
|
"id": 1,
|
|||
|
|
"username": "user",
|
|||
|
|
"email": "user@example.com",
|
|||
|
|
"role": "ROLE_USER",
|
|||
|
|
"points": 100
|
|||
|
|
},
|
|||
|
|
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 配置说明
|
|||
|
|
|
|||
|
|
### 1. 腾讯云配置 (可选)
|
|||
|
|
- **配置文件**:`src/main/resources/application-tencent.properties`
|
|||
|
|
- **用途**:集成腾讯云邮件推送服务
|
|||
|
|
- **当前状态**:暂时使用模拟发送,实际部署时需要配置
|
|||
|
|
|
|||
|
|
### 2. Redis配置
|
|||
|
|
- **默认配置**:localhost:6379
|
|||
|
|
- **用途**:验证码存储和频率限制
|
|||
|
|
- **生产环境**:建议配置密码和持久化
|
|||
|
|
|
|||
|
|
## 测试方法
|
|||
|
|
|
|||
|
|
### 1. 启动服务
|
|||
|
|
```bash
|
|||
|
|
# 启动Redis
|
|||
|
|
redis-server
|
|||
|
|
|
|||
|
|
# 启动应用
|
|||
|
|
mvn spring-boot:run
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 2. 测试流程
|
|||
|
|
```bash
|
|||
|
|
# 1. 发送验证码
|
|||
|
|
curl -X POST http://localhost:8080/api/verification/email/send \
|
|||
|
|
-H "Content-Type: application/json" \
|
|||
|
|
-d '{"email": "test@example.com"}'
|
|||
|
|
|
|||
|
|
# 2. 查看日志获取验证码(当前为模拟发送)
|
|||
|
|
# 3. 使用验证码登录
|
|||
|
|
curl -X POST http://localhost:8080/api/auth/login/email \
|
|||
|
|
-H "Content-Type: application/json" \
|
|||
|
|
-d '{"email": "test@example.com", "code": "123456"}'
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 待完成功能
|
|||
|
|
|
|||
|
|
### 1. 腾讯云集成
|
|||
|
|
- [ ] 配置腾讯云SES服务
|
|||
|
|
- [ ] 实现真实的邮件发送
|
|||
|
|
- [ ] 配置邮件模板
|
|||
|
|
|
|||
|
|
### 2. 前端集成
|
|||
|
|
- [ ] 修改登录页面支持邮箱验证码
|
|||
|
|
- [ ] 添加验证码输入框
|
|||
|
|
- [ ] 实现倒计时功能
|
|||
|
|
- [ ] 添加错误处理
|
|||
|
|
|
|||
|
|
### 3. 安全增强
|
|||
|
|
- [ ] 添加图形验证码
|
|||
|
|
- [ ] 实现IP限制
|
|||
|
|
- [ ] 添加设备指纹识别
|
|||
|
|
|
|||
|
|
## 注意事项
|
|||
|
|
|
|||
|
|
1. **开发环境**:当前使用模拟邮件发送,验证码会在日志中输出
|
|||
|
|
2. **生产环境**:需要配置真实的邮件服务
|
|||
|
|
3. **安全考虑**:验证码有效期和发送频率限制已实现
|
|||
|
|
4. **扩展性**:可以轻松添加短信验证码等其他验证方式
|