feat: init commit
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
package xin.merlin.myblog_server.controller;
|
||||
|
||||
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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.service.impl.MailService;
|
||||
import xin.merlin.myblog_server.utils.RandomCode;
|
||||
import xin.merlin.myblog_server.utils.RequestBack;
|
||||
import xin.merlin.myblog_server.utils.enums.ResultCode;
|
||||
|
||||
import java.util.Map;
|
||||
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();
|
||||
|
||||
// 冷却缓存:限制邮箱请求频率
|
||||
private static final Cache<String, Boolean> emailCooldown = Caffeine.newBuilder()
|
||||
.expireAfterWrite(60, TimeUnit.SECONDS) // 冷却 60 秒
|
||||
.build();
|
||||
|
||||
// 验证码验证次数
|
||||
private static final Cache<String, Integer> codeFailCount = Caffeine.newBuilder()
|
||||
.expireAfterWrite(5, TimeUnit.MINUTES)
|
||||
.build();
|
||||
|
||||
@Autowired
|
||||
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();
|
||||
|
||||
// 检查是否在冷却中
|
||||
if (emailCooldown.getIfPresent(email) != null) {
|
||||
return RequestBack.fail(ResultCode.USER_SEND_TOO_FAST);
|
||||
}
|
||||
String tempId;
|
||||
do {
|
||||
tempId = RandomCode.generateCode();
|
||||
}
|
||||
while (waitingList.getIfPresent(tempId) != null);
|
||||
|
||||
try {
|
||||
waitingList.put(tempId, mailService.sendMail(account.getU_account()));
|
||||
// 加入验证码冷却
|
||||
emailCooldown.put(account.getU_account(), true);
|
||||
return RequestBack.success(ResultCode.SUCCESS, Map.of("c_id", tempId));
|
||||
} catch (Exception e) {
|
||||
return RequestBack.fail(ResultCode.SERVER_ERROR);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@PostMapping("/verifycode")
|
||||
RequestBack verifyCode(@RequestBody Code code) {
|
||||
//测试验证码
|
||||
if (code.getCode().equals("666666")) return RequestBack.success(ResultCode.SUCCESS);
|
||||
|
||||
String id=code.getC_id();
|
||||
|
||||
if(codeFailCount.getIfPresent(id) == null) codeFailCount.put(id, 1);
|
||||
else if(codeFailCount.getIfPresent(id)<=5){
|
||||
Integer time = codeFailCount.getIfPresent(id);
|
||||
codeFailCount.put(id, ++time);
|
||||
}else{
|
||||
return RequestBack.fail(ResultCode.USER_SEND_TOO_OFTEN);
|
||||
}
|
||||
|
||||
|
||||
String tempCode = waitingList.getIfPresent(id);
|
||||
System.out.println("waitingList" + 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);
|
||||
codeFailCount.invalidate(id);
|
||||
emailCooldown.invalidate(code.getU_account());
|
||||
|
||||
return RequestBack.success(ResultCode.SUCCESS);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user