From 06cda3fb18a3241deff404f6948b02cc0d9a75a5 Mon Sep 17 00:00:00 2001 From: 疯狂的狮子li <15040126243@163.com> Date: 星期三, 07 十二月 2022 19:13:58 +0800 Subject: [PATCH] update 优化 使用 spring 事件机制 重构 OssConfig 缓存更新 --- ruoyi-system/src/main/java/com/ruoyi/system/service/SysLoginService.java | 44 ++++++++++++++++++++++++-------------------- 1 files changed, 24 insertions(+), 20 deletions(-) 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 540708f..a257fd9 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 @@ -5,6 +5,7 @@ import cn.dev33.satoken.stp.StpUtil; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.util.ObjectUtil; +import com.ruoyi.common.constant.CacheConstants; import com.ruoyi.common.constant.Constants; import com.ruoyi.common.core.domain.dto.RoleDTO; import com.ruoyi.common.core.domain.entity.SysUser; @@ -14,9 +15,7 @@ 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; -import com.ruoyi.common.exception.user.UserException; +import com.ruoyi.common.exception.user.*; import com.ruoyi.common.helper.LoginHelper; import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.MessageUtils; @@ -25,6 +24,7 @@ import com.ruoyi.common.utils.redis.RedisUtils; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import javax.servlet.http.HttpServletRequest; @@ -47,6 +47,12 @@ private final LogininforService asyncService; private final SysPermissionService permissionService; + @Value("${user.password.maxRetryCount}") + private Integer maxRetryCount; + + @Value("${user.password.lockTime}") + private Integer lockTime; + /** * 鐧诲綍楠岃瘉 * @@ -58,9 +64,9 @@ */ public String login(String username, String password, String code, String uuid) { HttpServletRequest request = ServletUtils.getRequest(); - boolean captchaOnOff = configService.selectCaptchaOnOff(); + boolean captchaEnabled = configService.selectCaptchaEnabled(); // 楠岃瘉鐮佸紑鍏� - if (captchaOnOff) { + if (captchaEnabled) { validateCaptcha(username, code, uuid, request); } SysUser user = loadUserByUsername(username); @@ -119,10 +125,10 @@ */ public void logout() { try { - String username = LoginHelper.getUsername(); + LoginUser loginUser = LoginHelper.getLoginUser(); StpUtil.logout(); - asyncService.recordLogininfor(username, Constants.LOGOUT, MessageUtils.message("user.logout.success"), ServletUtils.getRequest()); - } catch (NotLoginException e) { + asyncService.recordLogininfor(loginUser.getUsername(), Constants.LOGOUT, MessageUtils.message("user.logout.success"), ServletUtils.getRequest()); + } catch (NotLoginException ignored) { } } @@ -130,7 +136,7 @@ * 鏍¢獙鐭俊楠岃瘉鐮� */ private boolean validateSmsCode(String phonenumber, String smsCode, HttpServletRequest request) { - String code = RedisUtils.getCacheObject(Constants.CAPTCHA_CODE_KEY + phonenumber); + String code = RedisUtils.getCacheObject(CacheConstants.CAPTCHA_CODE_KEY + phonenumber); if (StringUtils.isBlank(code)) { asyncService.recordLogininfor(phonenumber, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"), request); throw new CaptchaExpireException(); @@ -146,7 +152,7 @@ * @param uuid 鍞竴鏍囪瘑 */ public void validateCaptcha(String username, String code, String uuid, HttpServletRequest request) { - String verifyKey = Constants.CAPTCHA_CODE_KEY + StringUtils.defaultString(uuid, ""); + String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.defaultString(uuid, ""); String captcha = RedisUtils.getCacheObject(verifyKey); RedisUtils.deleteObject(verifyKey); if (captcha == null) { @@ -242,27 +248,25 @@ */ 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 errorKey = CacheConstants.PWD_ERR_CNT_KEY + username; 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 (ObjectUtil.isNotNull(errorNumber) && errorNumber.equals(maxRetryCount)) { + asyncService.recordLogininfor(username, loginFail, MessageUtils.message(loginType.getRetryLimitExceed(), maxRetryCount, lockTime), request); + throw new UserException(loginType.getRetryLimitExceed(), maxRetryCount, lockTime); } if (supplier.get()) { // 鏄惁绗竴娆� errorNumber = ObjectUtil.isNull(errorNumber) ? 1 : errorNumber + 1; // 杈惧埌瑙勫畾閿欒娆℃暟 鍒欓攣瀹氱櫥褰� - if (errorNumber.equals(setErrorNumber)) { - RedisUtils.setCacheObject(errorKey, errorNumber, Duration.ofMinutes(errorLimitTime)); - asyncService.recordLogininfor(username, loginFail, MessageUtils.message(loginType.getRetryLimitExceed(), errorLimitTime), request); - throw new UserException(loginType.getRetryLimitExceed(), errorLimitTime); + if (errorNumber.equals(maxRetryCount)) { + RedisUtils.setCacheObject(errorKey, errorNumber, Duration.ofMinutes(lockTime)); + asyncService.recordLogininfor(username, loginFail, MessageUtils.message(loginType.getRetryLimitExceed(), maxRetryCount, lockTime), request); + throw new UserException(loginType.getRetryLimitExceed(), maxRetryCount, lockTime); } else { // 鏈揪鍒拌瀹氶敊璇鏁� 鍒欓�掑 RedisUtils.setCacheObject(errorKey, errorNumber); -- Gitblit v1.9.3