refactor(all): change sql to postgresql
This commit is contained in:
@@ -8,8 +8,9 @@ import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import xin.merlin.myblog_server.entity.Account;
|
||||
import xin.merlin.myblog_server.entity.Code;
|
||||
import xin.merlin.myblog_server.entity.User;
|
||||
import xin.merlin.myblog_server.service.CacheService;
|
||||
import xin.merlin.myblog_server.service.impl.MailService;
|
||||
import xin.merlin.myblog_server.utils.RandomCode;
|
||||
import xin.merlin.myblog_server.utils.RequestBack;
|
||||
@@ -21,10 +22,9 @@ import java.util.concurrent.TimeUnit;
|
||||
@RestController
|
||||
@RequestMapping("/code")
|
||||
public class MailController {
|
||||
private static final Cache<String, String> waitingList = Caffeine.newBuilder()
|
||||
.expireAfterWrite(5, TimeUnit.MINUTES)
|
||||
.build();
|
||||
|
||||
@Autowired
|
||||
private CacheService cacheService;
|
||||
|
||||
// 冷却缓存:限制邮箱请求频率
|
||||
private static final Cache<String, Boolean> emailCooldown = Caffeine.newBuilder()
|
||||
.expireAfterWrite(60, TimeUnit.SECONDS) // 冷却 60 秒
|
||||
@@ -39,10 +39,10 @@ public class MailController {
|
||||
private MailService mailService;
|
||||
|
||||
@PostMapping("/sendcode")
|
||||
RequestBack sendcode(@RequestBody Account account) {
|
||||
if (account.getU_account() == null) return RequestBack.fail(ResultCode.BAD_REQUEST);
|
||||
System.out.println("发送验证码到:" + account.getU_account());
|
||||
String email = account.getU_account();
|
||||
RequestBack sendcode(@RequestBody User user) {
|
||||
if (user.getAccount() == null) return RequestBack.fail(ResultCode.BAD_REQUEST);
|
||||
System.out.println("发送验证码到:" + user.getAccount());
|
||||
String email = user.getAccount();
|
||||
|
||||
// 检查是否在冷却中
|
||||
if (emailCooldown.getIfPresent(email) != null) {
|
||||
@@ -52,12 +52,12 @@ public class MailController {
|
||||
do {
|
||||
tempId = RandomCode.generateCode();
|
||||
}
|
||||
while (waitingList.getIfPresent(tempId) != null);
|
||||
while (cacheService.getWaitingList().getIfPresent(tempId) != null);
|
||||
|
||||
try {
|
||||
waitingList.put(tempId, mailService.sendMail(account.getU_account()));
|
||||
cacheService.getWaitingList().put(tempId, mailService.sendMail(user.getAccount()));
|
||||
// 加入验证码冷却
|
||||
emailCooldown.put(account.getU_account(), true);
|
||||
emailCooldown.put(user.getAccount(), true);
|
||||
return RequestBack.success(ResultCode.SUCCESS, Map.of("c_id", tempId));
|
||||
} catch (Exception e) {
|
||||
return RequestBack.fail(ResultCode.SERVER_ERROR);
|
||||
@@ -81,13 +81,13 @@ public class MailController {
|
||||
}
|
||||
|
||||
|
||||
String tempCode = waitingList.getIfPresent(id);
|
||||
System.out.println("waitingList" + tempCode + "\nv_id:" + id + "\ncode:" + code.getCode());
|
||||
String tempCode = cacheService.getWaitingList().getIfPresent(id);
|
||||
System.out.println("cacheService.getWaitingList()" + tempCode + "\nv_id:" + id + "\ncode:" + code.getCode());
|
||||
if (tempCode == null) return RequestBack.success(ResultCode.USER_VERIFICATION_ERROR);
|
||||
if (!tempCode.equals(code.getCode())) return RequestBack.success(ResultCode.USER_VERIFICATION_ERROR);
|
||||
waitingList.invalidate(id);
|
||||
// cacheService.getWaitingList().invalidate(id);
|
||||
codeFailCount.invalidate(id);
|
||||
emailCooldown.invalidate(code.getU_account());
|
||||
emailCooldown.invalidate(code.getAccount());
|
||||
|
||||
return RequestBack.success(ResultCode.SUCCESS);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user