- 新增VerificationCodeService:验证码生成、发送、验证 - 新增VerificationCodeController:验证码相关API接口 - 扩展AuthApiController:支持邮箱验证码登录 - 扩展UserRepository和UserService:支持邮箱查找用户 - 使用内存存储验证码,无需Redis依赖 - 添加腾讯云配置支持(可选) - 实现安全机制:频率限制、有效期、一次性使用 - 添加详细文档说明
2.9 KiB
2.9 KiB
腾讯云邮箱验证码登录配置指南
1. 腾讯云服务开通
1.1 开通邮件推送服务(SES)
- 登录腾讯云控制台
- 进入"邮件推送"服务
- 开通邮件推送服务
- 配置发件人邮箱
- 申请邮件模板
1.2 获取API密钥
- 进入"访问管理" -> "API密钥管理"
- 创建密钥,获取SecretId和SecretKey
2. 配置参数
2.1 修改配置文件
编辑 src/main/resources/application-tencent.properties:
# 腾讯云配置
tencent.cloud.secret-id=你的SecretId
tencent.cloud.secret-key=你的SecretKey
# 邮件推送服务配置
tencent.cloud.ses.region=ap-beijing
tencent.cloud.ses.from-email=你的发件人邮箱
tencent.cloud.ses.from-name=你的应用名称
tencent.cloud.ses.template-id=你的邮件模板ID
2.2 邮件模板示例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>验证码</title>
</head>
<body>
<h2>验证码</h2>
<p>您的验证码是:<strong>{{code}}</strong></p>
<p>请在5分钟内输入,如非本人操作,请忽略此邮件。</p>
</body>
</html>
3. Redis配置
3.1 安装Redis
- Windows: 下载Redis for Windows
- Linux:
sudo apt-get install redis-server - macOS:
brew install redis
3.2 启动Redis
redis-server
3.3 配置Redis连接
修改 application-tencent.properties 中的Redis配置:
spring.data.redis.host=localhost
spring.data.redis.port=6379
spring.data.redis.password=
spring.data.redis.database=0
4. 测试验证码功能
4.1 发送邮件验证码
curl -X POST http://localhost:8080/api/verification/email/send \
-H "Content-Type: application/json" \
-d '{"email": "test@example.com"}'
4.2 验证码登录
# 邮箱验证码登录
curl -X POST http://localhost:8080/api/auth/login/email \
-H "Content-Type: application/json" \
-d '{"email": "test@example.com", "code": "123456"}'
5. 安全注意事项
- API密钥安全:不要将SecretId和SecretKey提交到代码仓库
- 验证码有效期:验证码5分钟过期
- 发送频率限制:同一邮箱60秒内只能发送一次
- 验证码长度:6位数字验证码
- Redis安全:生产环境建议设置Redis密码
6. 故障排除
6.1 常见错误
TencentCloudSDKException: 检查API密钥和配置Redis连接失败: 检查Redis服务是否启动验证码发送失败: 检查腾讯云服务配置
6.2 日志查看
查看应用日志中的验证码相关日志:
tail -f logs/application.log | grep "验证码"
7. 生产环境部署
7.1 环境变量配置
export TENCENT_SECRET_ID=你的SecretId
export TENCENT_SECRET_KEY=你的SecretKey
export REDIS_HOST=你的Redis主机
export REDIS_PORT=你的Redis端口
7.2 配置文件
使用环境变量覆盖配置文件中的敏感信息。