From eb1047a149112cd6751808b326cd449345453174 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子Li <15040126243@163.com>
Date: 星期日, 17 四月 2022 21:51:41 +0800
Subject: [PATCH] update 优化登录失败相关部分代码结构
---
ruoyi-system/src/main/java/com/ruoyi/system/service/SysLoginService.java | 97 ++++++++++++++------------------
ruoyi-common/src/main/java/com/ruoyi/common/enums/LoginType.java | 39 +++++++++++++
2 files changed, 81 insertions(+), 55 deletions(-)
diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/enums/LoginType.java b/ruoyi-common/src/main/java/com/ruoyi/common/enums/LoginType.java
new file mode 100644
index 0000000..c91a4b9
--- /dev/null
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/enums/LoginType.java
@@ -0,0 +1,39 @@
+package com.ruoyi.common.enums;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+/**
+ * 鐧诲綍绫诲瀷
+ *
+ * @author Lion Li
+ */
+@Getter
+@AllArgsConstructor
+public enum LoginType {
+
+ /**
+ * 瀵嗙爜鐧诲綍
+ */
+ PASSWORD("user.password.retry.limit.exceed", "user.password.retry.limit.count"),
+
+ /**
+ * 鐭俊鐧诲綍
+ */
+ SMS("sms.code.retry.limit.exceed", "sms.code.retry.limit.count"),
+
+ /**
+ * 灏忕▼搴忕櫥褰�
+ */
+ XCX("", "");
+
+ /**
+ * 鐧诲綍閲嶈瘯瓒呭嚭闄愬埗鎻愮ず
+ */
+ final String retryLimitExceed;
+
+ /**
+ * 鐧诲綍閲嶈瘯闄愬埗璁℃暟鎻愮ず
+ */
+ final String retryLimitCount;
+}
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/SysLoginService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/SysLoginService.java
index 34c8fe6..e0b53c1 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/SysLoginService.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/SysLoginService.java
@@ -11,6 +11,7 @@
import com.ruoyi.common.core.domain.model.XcxLoginUser;
import com.ruoyi.common.core.service.LogininforService;
import com.ruoyi.common.enums.DeviceType;
+import com.ruoyi.common.enums.LoginType;
import com.ruoyi.common.enums.UserStatus;
import com.ruoyi.common.exception.user.CaptchaException;
import com.ruoyi.common.exception.user.CaptchaExpireException;
@@ -28,6 +29,7 @@
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;
+import java.util.function.Supplier;
/**
* 鐧诲綍鏍¢獙鏂规硶
@@ -60,35 +62,8 @@
if (captchaOnOff) {
validateCaptcha(username, code, uuid, request);
}
- // 鑾峰彇鐢ㄦ埛鐧诲綍閿欒娆℃暟(鍙嚜瀹氫箟闄愬埗绛栫暐 渚嬪: key + username + ip)
- Integer errorNumber = RedisUtils.getCacheObject(Constants.LOGIN_ERROR + username);
- // 閿佸畾鏃堕棿鍐呯櫥褰� 鍒欒涪鍑�
- if (ObjectUtil.isNotNull(errorNumber) && errorNumber.equals(Constants.LOGIN_ERROR_NUMBER)) {
- asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.retry.limit.exceed", Constants.LOGIN_ERROR_LIMIT_TIME), request);
- throw new UserException("user.password.retry.limit.exceed", Constants.LOGIN_ERROR_LIMIT_TIME);
- }
-
SysUser user = loadUserByUsername(username);
-
- if (!BCrypt.checkpw(password, user.getPassword())) {
- // 鏄惁绗竴娆�
- errorNumber = ObjectUtil.isNull(errorNumber) ? 1 : errorNumber + 1;
- // 杈惧埌瑙勫畾閿欒娆℃暟 鍒欓攣瀹氱櫥褰�
- if (errorNumber.equals(Constants.LOGIN_ERROR_NUMBER)) {
- RedisUtils.setCacheObject(Constants.LOGIN_ERROR + username, errorNumber, Constants.LOGIN_ERROR_LIMIT_TIME, TimeUnit.MINUTES);
- asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.retry.limit.exceed", Constants.LOGIN_ERROR_LIMIT_TIME), request);
- throw new UserException("user.password.retry.limit.exceed", Constants.LOGIN_ERROR_LIMIT_TIME);
- } else {
- // 鏈揪鍒拌瀹氶敊璇鏁� 鍒欓�掑
- RedisUtils.setCacheObject(Constants.LOGIN_ERROR + username, errorNumber);
- asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.retry.limit.count", errorNumber), request);
- throw new UserException("user.password.retry.limit.count", errorNumber);
- }
- }
-
- // 鐧诲綍鎴愬姛 娓呯┖閿欒娆℃暟
- RedisUtils.deleteObject(Constants.LOGIN_ERROR + username);
-
+ checkLogin(LoginType.PASSWORD, username, () -> !BCrypt.checkpw(password, user.getPassword()));
// 姝ゅ鍙牴鎹櫥褰曠敤鎴风殑鏁版嵁涓嶅悓 鑷鍒涘缓 loginUser
LoginUser loginUser = buildLoginUser(user);
// 鐢熸垚token
@@ -104,33 +79,7 @@
SysUser user = loadUserByPhonenumber(phonenumber);
HttpServletRequest request = ServletUtils.getRequest();
- // 鑾峰彇鐢ㄦ埛鐧诲綍閿欒娆℃暟(鍙嚜瀹氫箟闄愬埗绛栫暐 渚嬪: key + username + ip)
- Integer errorNumber = RedisUtils.getCacheObject(Constants.LOGIN_ERROR + user.getUserName());
- // 閿佸畾鏃堕棿鍐呯櫥褰� 鍒欒涪鍑�
- if (ObjectUtil.isNotNull(errorNumber) && errorNumber.equals(Constants.LOGIN_ERROR_NUMBER)) {
- asyncService.recordLogininfor(user.getUserName(), Constants.LOGIN_FAIL, MessageUtils.message("sms.code.retry.limit.exceed", Constants.LOGIN_ERROR_LIMIT_TIME), request);
- throw new UserException("sms.code.retry.limit.exceed", Constants.LOGIN_ERROR_LIMIT_TIME);
- }
-
- if (!validateSmsCode(phonenumber, smsCode)) {
- // 鏄惁绗竴娆�
- errorNumber = ObjectUtil.isNull(errorNumber) ? 1 : errorNumber + 1;
- // 杈惧埌瑙勫畾閿欒娆℃暟 鍒欓攣瀹氱櫥褰�
- if (errorNumber.equals(Constants.LOGIN_ERROR_NUMBER)) {
- RedisUtils.setCacheObject(Constants.LOGIN_ERROR + user.getUserName(), errorNumber, Constants.LOGIN_ERROR_LIMIT_TIME, TimeUnit.MINUTES);
- asyncService.recordLogininfor(user.getUserName(), Constants.LOGIN_FAIL, MessageUtils.message("sms.code.retry.limit.exceed", Constants.LOGIN_ERROR_LIMIT_TIME), request);
- throw new UserException("sms.code.retry.limit.exceed", Constants.LOGIN_ERROR_LIMIT_TIME);
- } else {
- // 鏈揪鍒拌瀹氶敊璇鏁� 鍒欓�掑
- RedisUtils.setCacheObject(Constants.LOGIN_ERROR + user.getUserName(), errorNumber);
- asyncService.recordLogininfor(user.getUserName(), Constants.LOGIN_FAIL, MessageUtils.message("sms.code.retry.limit.count", errorNumber), request);
- throw new UserException("sms.code.retry.limit.count", errorNumber);
- }
- }
-
- // 鐧诲綍鎴愬姛 娓呯┖閿欒娆℃暟
- RedisUtils.deleteObject(Constants.LOGIN_ERROR + user.getUserName());
-
+ checkLogin(LoginType.SMS, user.getUserName(), () -> !validateSmsCode(phonenumber, smsCode));
// 姝ゅ鍙牴鎹櫥褰曠敤鎴风殑鏁版嵁涓嶅悓 鑷鍒涘缓 loginUser
LoginUser loginUser = buildLoginUser(user);
// 鐢熸垚token
@@ -275,4 +224,42 @@
sysUser.setUpdateBy(username);
userService.updateUserProfile(sysUser);
}
+
+ /**
+ * 鐧诲綍鏍¢獙
+ */
+ private void checkLogin(LoginType loginType, String username, Supplier<Boolean> supplier) {
+ HttpServletRequest request = ServletUtils.getRequest();
+ String errorKey = Constants.LOGIN_ERROR + username;
+ Integer errorLimitTime = Constants.LOGIN_ERROR_LIMIT_TIME;
+ Integer setErrorNumber = Constants.LOGIN_ERROR_NUMBER;
+ String loginFail = Constants.LOGIN_FAIL;
+
+ // 鑾峰彇鐢ㄦ埛鐧诲綍閿欒娆℃暟(鍙嚜瀹氫箟闄愬埗绛栫暐 渚嬪: key + username + ip)
+ Integer errorNumber = RedisUtils.getCacheObject(errorKey);
+ // 閿佸畾鏃堕棿鍐呯櫥褰� 鍒欒涪鍑�
+ if (ObjectUtil.isNotNull(errorNumber) && errorNumber.equals(setErrorNumber)) {
+ asyncService.recordLogininfor(username, loginFail, MessageUtils.message(loginType.getRetryLimitExceed(), errorLimitTime), request);
+ throw new UserException(loginType.getRetryLimitExceed(), errorLimitTime);
+ }
+
+ if (supplier.get()) {
+ // 鏄惁绗竴娆�
+ errorNumber = ObjectUtil.isNull(errorNumber) ? 1 : errorNumber + 1;
+ // 杈惧埌瑙勫畾閿欒娆℃暟 鍒欓攣瀹氱櫥褰�
+ if (errorNumber.equals(setErrorNumber)) {
+ RedisUtils.setCacheObject(errorKey, errorNumber, errorLimitTime, TimeUnit.MINUTES);
+ asyncService.recordLogininfor(username, loginFail, MessageUtils.message(loginType.getRetryLimitExceed(), errorLimitTime), request);
+ throw new UserException(loginType.getRetryLimitExceed(), errorLimitTime);
+ } else {
+ // 鏈揪鍒拌瀹氶敊璇鏁� 鍒欓�掑
+ RedisUtils.setCacheObject(errorKey, errorNumber);
+ asyncService.recordLogininfor(username, loginFail, MessageUtils.message(loginType.getRetryLimitCount(), errorNumber), request);
+ throw new UserException(loginType.getRetryLimitCount(), errorNumber);
+ }
+ }
+
+ // 鐧诲綍鎴愬姛 娓呯┖閿欒娆℃暟
+ RedisUtils.deleteObject(errorKey);
+ }
}
--
Gitblit v1.9.3