feat(user): 新增邮箱注册与验证码发送功能

- 新增 UserRegisterDTO 及 /user/register 接口
- 集成 MailerSend,异步发送 6 位验证码邮件
- 添加 RedisUtil 缓存验证码 10 分钟
- 补充 SEND_MAIL_FAILED、CONFIRM_PASSWORD_NOT_MATCH 错误码
- 关闭 Spring Security CSRF 与表单登录,放行 /user/register
- AppleService 移除 @AllArgsConstructor,改用 @Resource 注入
This commit is contained in:
2025-12-03 21:48:27 +08:00
parent ba601d329c
commit a7273e4620
15 changed files with 1590 additions and 7 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,57 @@
package com.yolo.keyborad.utils;
import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.MailerSendResponse;
import com.mailersend.sdk.Recipient;
import com.mailersend.sdk.emails.Email;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.yolo.keyborad.common.ErrorCode;
import com.yolo.keyborad.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
/*
* @author: ziin
* @date: 2025/12/3 20:43
*/
@Component
@Slf4j
public class SendMailUtils {
@Value("${mail_access_token}")
private String accessToken;
@Async("mailTaskExecutor")
public void sendEmail(String userName,String userMail,Integer code ) {
Email email = new Email();
email.setFrom("verify code", "MS_JqR6MO@no-replay.loveamorkey.com");
Recipient recipient = new Recipient(userName, userMail);
email.addRecipient(userName,userMail);
email.setSubject("no-replay");
email.setTemplateId("k68zxl28q79lj905");
email.addPersonalization(recipient, "code", code);
MailerSend ms = new MailerSend();
ms.setToken(accessToken);
try {
MailerSendResponse response = ms.emails().send(email);
log.info("邮件发送成功 messageId={}", response.messageId);
} catch (MailerSendException e) {
log.error("邮件发送失败: {}", e.getMessage());
// 异步任务不能再抛业务异常,避免影响主业务流程
// 如果需要,你可以把失败记录写入 DB 或重试队列
}
}
}