From 17f0c20242c6ee3d35b9ee8f677d747ed012423c Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期五, 02 九月 2022 16:30:20 +0800
Subject: [PATCH] remove 移除maven docker插件 过于老旧功能缺陷大 使用idea自带的docker插件替代

---
 ruoyi-system/src/main/java/com/ruoyi/system/service/SysLoginService.java |  148 ++++++++++++++++++++++++++-----------------------
 1 files changed, 78 insertions(+), 70 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 be5aac2..ea53635 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
@@ -1,19 +1,21 @@
 package com.ruoyi.system.service;
 
+import cn.dev33.satoken.exception.NotLoginException;
 import cn.dev33.satoken.secure.BCrypt;
 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;
 import com.ruoyi.common.core.domain.model.LoginUser;
+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;
-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;
@@ -22,11 +24,13 @@
 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;
+import java.time.Duration;
 import java.util.List;
-import java.util.concurrent.TimeUnit;
+import java.util.function.Supplier;
 
 /**
  * 鐧诲綍鏍¢獙鏂规硶
@@ -43,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;
+
     /**
      * 鐧诲綍楠岃瘉
      *
@@ -54,40 +64,13 @@
      */
     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);
         }
-        // 鑾峰彇鐢ㄦ埛鐧诲綍閿欒娆℃暟(鍙嚜瀹氫箟闄愬埗绛栫暐 渚嬪: 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
@@ -103,33 +86,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, request));
         // 姝ゅ鍙牴鎹櫥褰曠敤鎴风殑鏁版嵁涓嶅悓 鑷鍒涘缓 loginUser
         LoginUser loginUser = buildLoginUser(user);
         // 鐢熸垚token
@@ -150,7 +107,11 @@
         SysUser user = loadUserByOpenid(openid);
 
         // 姝ゅ鍙牴鎹櫥褰曠敤鎴风殑鏁版嵁涓嶅悓 鑷鍒涘缓 loginUser
-        LoginUser loginUser = buildLoginUser(user);
+        XcxLoginUser loginUser = new XcxLoginUser();
+        loginUser.setUserId(user.getUserId());
+        loginUser.setUsername(user.getUserName());
+        loginUser.setUserType(user.getUserType());
+        loginUser.setOpenid(openid);
         // 鐢熸垚token
         LoginHelper.loginByDevice(loginUser, DeviceType.XCX);
 
@@ -159,17 +120,28 @@
         return StpUtil.getTokenValue();
     }
 
-
-    public void logout(String loginName) {
-        asyncService.recordLogininfor(loginName, Constants.LOGOUT, MessageUtils.message("user.logout.success"), ServletUtils.getRequest());
+    /**
+     * 閫�鍑虹櫥褰�
+     */
+    public void logout() {
+        try {
+            String username = LoginHelper.getUsername();
+            StpUtil.logout();
+            asyncService.recordLogininfor(username, Constants.LOGOUT, MessageUtils.message("user.logout.success"), ServletUtils.getRequest());
+        } catch (NotLoginException e) {
+        }
     }
 
     /**
      * 鏍¢獙鐭俊楠岃瘉鐮�
      */
-    private boolean validateSmsCode(String phonenumber, String smsCode) {
-        // todo 姝ゅ浣跨敤鎵嬫満鍙锋煡璇edis楠岃瘉鐮佷笌鍙傛暟楠岃瘉鐮佹槸鍚︿竴鑷� 鐢ㄦ埛鑷瀹炵幇
-        return true;
+    private boolean validateSmsCode(String phonenumber, String smsCode, HttpServletRequest request) {
+        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();
+        }
+        return code.equals(smsCode);
     }
 
     /**
@@ -180,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) {
@@ -251,7 +223,7 @@
         loginUser.setUserType(user.getUserType());
         loginUser.setMenuPermission(permissionService.getMenuPermission(user));
         loginUser.setRolePermission(permissionService.getRolePermission(user));
-        loginUser.setDeptName(user.getDept().getDeptName());
+        loginUser.setDeptName(ObjectUtil.isNull(user.getDept()) ? "" : user.getDept().getDeptName());
         List<RoleDTO> roles = BeanUtil.copyToList(user.getRoles(), RoleDTO.class);
         loginUser.setRoles(roles);
         return loginUser;
@@ -270,4 +242,40 @@
         sysUser.setUpdateBy(username);
         userService.updateUserProfile(sysUser);
     }
+
+    /**
+     * 鐧诲綍鏍¢獙
+     */
+    private void checkLogin(LoginType loginType, String username, Supplier<Boolean> supplier) {
+        HttpServletRequest request = ServletUtils.getRequest();
+        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(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(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);
+                asyncService.recordLogininfor(username, loginFail, MessageUtils.message(loginType.getRetryLimitCount(), errorNumber), request);
+                throw new UserException(loginType.getRetryLimitCount(), errorNumber);
+            }
+        }
+
+        // 鐧诲綍鎴愬姛 娓呯┖閿欒娆℃暟
+        RedisUtils.deleteObject(errorKey);
+    }
 }

--
Gitblit v1.9.3