|
|
@@ -2,9 +2,7 @@ package com.izouma.jmrh.web;
|
|
|
|
|
|
import cn.licoy.encryptbody.annotation.encrypt.EncryptBody;
|
|
|
import cn.licoy.encryptbody.enums.EncryptBodyMethod;
|
|
|
-import com.izouma.jmrh.annotations.OperLog;
|
|
|
import com.izouma.jmrh.domain.LoginLog;
|
|
|
-import com.izouma.jmrh.domain.MailCode;
|
|
|
import com.izouma.jmrh.domain.User;
|
|
|
import com.izouma.jmrh.exception.AuthenticationException;
|
|
|
import com.izouma.jmrh.exception.BusinessException;
|
|
|
@@ -13,13 +11,12 @@ import com.izouma.jmrh.repo.UserRepo;
|
|
|
import com.izouma.jmrh.security.JwtTokenUtil;
|
|
|
import com.izouma.jmrh.security.JwtUserDetailsService;
|
|
|
import com.izouma.jmrh.security.JwtUserFactory;
|
|
|
-import com.izouma.jmrh.service.LoginLogService;
|
|
|
import com.izouma.jmrh.service.UserService;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.struts.chain.contexts.ActionContext;
|
|
|
-import org.springframework.http.HttpRequest;
|
|
|
+import org.springframework.cache.Cache;
|
|
|
+import org.springframework.cache.CacheManager;
|
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
|
import org.springframework.security.authentication.BadCredentialsException;
|
|
|
import org.springframework.security.authentication.DisabledException;
|
|
|
@@ -31,11 +28,11 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
-import javax.servlet.http.HttpServlet;
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
-import javax.servlet.http.HttpSession;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.util.Objects;
|
|
|
+import java.util.concurrent.Executors;
|
|
|
+import java.util.concurrent.ScheduledExecutorService;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@Slf4j
|
|
|
@AllArgsConstructor
|
|
|
@@ -49,6 +46,9 @@ public class AuthenticationController {
|
|
|
private UserService userService;
|
|
|
private LoginLogRepo loginLogRepo;
|
|
|
private UserRepo userRepo;
|
|
|
+ private CacheManager cacheManager;
|
|
|
+
|
|
|
+ private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(10);
|
|
|
|
|
|
@PostMapping("/registerByMail")
|
|
|
public String registerByMail(@RequestParam String mail, @RequestParam String username, @RequestParam String password) {
|
|
|
@@ -58,6 +58,13 @@ public class AuthenticationController {
|
|
|
|
|
|
@PostMapping("/loginByMail")
|
|
|
public String loginByMail(String mail, String password) {
|
|
|
+ Cache loginCache = cacheManager.getCache("loginCache");
|
|
|
+ Cache userLockCache = cacheManager.getCache("userLockCache");
|
|
|
+ LocalDateTime lockTime = userLockCache.get(mail, LocalDateTime.class);
|
|
|
+ if (lockTime != null && LocalDateTime.now().isBefore(lockTime)) {
|
|
|
+ throw new BusinessException("密码错误超过3次,请十分钟后再试");
|
|
|
+ }
|
|
|
+
|
|
|
try {
|
|
|
User user = userRepo.findByEmail(mail);
|
|
|
if (user == null) {
|
|
|
@@ -66,6 +73,7 @@ public class AuthenticationController {
|
|
|
if (!new BCryptPasswordEncoder().matches(password, user.getPassword())) {
|
|
|
throw new AuthenticationException("", null);
|
|
|
}
|
|
|
+
|
|
|
return jwtTokenUtil.generateToken(JwtUserFactory.create(user));
|
|
|
} catch (Exception e) {
|
|
|
log.error("loginByMail", e);
|
|
|
@@ -76,6 +84,15 @@ public class AuthenticationController {
|
|
|
.success(false)
|
|
|
.time(LocalDateTime.now())
|
|
|
.build());
|
|
|
+ loginCache.putIfAbsent(mail, Integer.valueOf(0));
|
|
|
+ loginCache.put(mail, loginCache.get(mail, Integer.class) + 1);
|
|
|
+ if (loginCache.get(mail, Integer.class) >= 3) {
|
|
|
+ userLockCache.put(mail, LocalDateTime.now().plusMinutes(10));
|
|
|
+ }
|
|
|
+ scheduler.schedule(() -> {
|
|
|
+ loginCache.putIfAbsent(mail, Integer.valueOf(0));
|
|
|
+ loginCache.put(mail, loginCache.get(mail, Integer.class) - 1);
|
|
|
+ }, 1, TimeUnit.SECONDS);
|
|
|
throw new AuthenticationException("用户名或密码错误", e);
|
|
|
}
|
|
|
}
|