Browse Source

登录接口调整

1811872455@163.com 3 tuần trước cách đây
mục cha
commit
8c40c46fc8

+ 0 - 18
storlead-framework/storlead-auth/src/main/java/com/storlead/framework/auth/login/CurrentEmployeeInfo.java

@@ -1,18 +0,0 @@
-package com.storlead.framework.auth.login;
-
-import com.storlead.framework.common.constant.UserCacheKeyConstants;
-import com.storlead.framework.core.context.Context;
-import lombok.Data;
-import lombok.extern.log4j.Log4j2;
-
-/**
- * @program: storlead-storlead-centre-platform
- * @description:
- * @author: chenkq
- * @create: 2025-07-10 10:58
- */
-@Log4j2
-@Data
-public class CurrentEmployeeInfo {
-    private String acount;
-}

+ 0 - 41
storlead-framework/storlead-auth/src/main/java/com/storlead/framework/auth/login/LoginEmployee.java

@@ -1,41 +0,0 @@
-package com.storlead.framework.auth.login;
-
-import com.storlead.framework.common.constant.UserCacheKeyConstants;
-import com.storlead.framework.core.context.Context;
-import lombok.extern.log4j.Log4j2;
-
-/**
- * @program: storlead-storlead-centre-platform
- * @description:
- * @author: chenkq
- * @create: 2025-07-10 11:00
- */
-@Log4j2
-public class LoginEmployee {
-
-    /**
-     * 获取当前用户信息
-     * @return
-     */
-    public static CurrentEmployeeInfo getCurrentEmpInfo(){
-        try {
-            CurrentEmployeeInfo employeeInfo = Context.getContext().getAttribute(UserCacheKeyConstants.LOGIN_USER_INFO_KEY, CurrentEmployeeInfo.class);
-            return employeeInfo;
-        } catch (Exception e) {
-            return null;
-        }
-    }
-    /**
-     * 获取当前用户信息
-     * @return
-     */
-    public static Long getCurrentUserId(){
-        try {
-            Long currentUserId = Context.getContext().getAttribute(UserCacheKeyConstants.LOGIN_USER_INFO_ID_KEY, Long.class);
-            return currentUserId;
-        } catch (Exception e) {
-            log.error("error - getCurrentUserId",e);
-            return null;
-        }
-    }
-}

+ 6 - 0
storlead-framework/storlead-web/pom.xml

@@ -27,6 +27,12 @@
             <version>${revision}</version>
         </dependency>
 
+        <dependency>
+            <groupId>com.storlead.boot</groupId>
+            <artifactId>storlead-redis</artifactId>
+            <version>${revision}</version>
+        </dependency>
+
         <dependency>
             <groupId>com.storlead.boot</groupId>
             <artifactId>storlead-core</artifactId>

+ 69 - 0
storlead-framework/storlead-web/src/main/java/com/storlead/framework/web/filter/AuthRequestFilter.java

@@ -9,9 +9,14 @@ import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.ArrayNode;
 import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.storlead.framework.auth.vo.LoginUser;
+import com.storlead.framework.common.constant.DefContants;
+import com.storlead.framework.common.constant.UserCacheKeyConstants;
 import com.storlead.framework.common.util.SystemUtils;
+import com.storlead.framework.common.util.UrlChainBlackAndWhiteUtil;
 import com.storlead.framework.core.context.Context;
 import com.storlead.framework.core.context.IContext;
+import com.storlead.framework.redis.RedisService;
 import com.storlead.framework.web.assemble.Result;
 import com.storlead.framework.web.enums.ResultCode;
 import com.storlead.framework.web.wrapper.JsonRequestWrapper;
@@ -41,6 +46,12 @@ import java.util.concurrent.TimeUnit;
 @Component
 public class AuthRequestFilter implements Filter, Ordered {
 
+    @Resource
+    private RedisService redisService;
+
+    @Resource
+    private UrlChainBlackAndWhiteUtil chainBlackAndWhiteUtil;
+
     @Override
     public void init(FilterConfig filterConfig) throws ServletException {
         Filter.super.init(filterConfig);
@@ -109,6 +120,64 @@ public class AuthRequestFilter implements Filter, Ordered {
 
     private Boolean loginHandle(HttpServletRequest req) {
         // 获取URL
+        String url = req.getRequestURI();
+        log.error("login--WebSocket ---- url---"+url);
+        Boolean isNeedLogin = chainBlackAndWhiteUtil.IsWhiteUri(url);
+        if (isNeedLogin) {
+            return isNeedLogin;
+        }
+        log.error("login--WebSocket ---- isNeedLogin---"+url);
+        String token = req.getHeader(DefContants.ACCESS_TOKEN);
+        if (StrUtil.isBlank(token)) {
+            token = req.getHeader("Sec-WebSocket-Protocol");
+            if (StrUtil.isNotBlank(token)) {
+                log.error("login--WebSocket ---- token---"+token);
+                req.setAttribute("token",token);
+            }
+        }
+
+        if (StringUtils.isNotBlank(token)) {
+            String jsonInfo = redisService.getCacheObject(token);
+            if (StrUtil.isNotBlank(jsonInfo) && jsonInfo.length() < "1efb52db4da5d2b92516a4eba0b3145b34234234".length()) {
+                jsonInfo = redisService.getCacheObject(jsonInfo);
+            }
+            if (StringUtils.isNotBlank(jsonInfo)) {
+                try {
+                    IContext context = Context.getContext();
+                    LoginUser loginUserInfo = JSONUtil.toBean(jsonInfo, LoginUser.class);
+
+                    String  scopeMenuId =  context.getAttribute("scopeMenuId",String.class);
+                    Object  commonScope =  context.getAttribute("commonScope",Object.class);
+                    Object  commonScopeType =  context.getAttribute("commonScopeType",Object.class);
+                    // 当前接口的地址
+                    loginUserInfo.setApiUrl(url);
+                    loginUserInfo.setScopeMenuId(scopeMenuId);
+                    if (Objects.nonNull(commonScope)) {
+                        loginUserInfo.setCommonScope((Integer)commonScope);
+                    }
+                    if (Objects.nonNull(commonScopeType)) {
+                        loginUserInfo.setCommonScopeType(commonScopeType.toString());
+                    }
+                    context.setAttribute(UserCacheKeyConstants.LOGIN_USER_INFO_KEY, loginUserInfo);
+                    context.setAttribute(UserCacheKeyConstants.LOGIN_USER_INFO_TOKEN_KEY, token);
+                    context.setAttribute(UserCacheKeyConstants.LOGIN_USER_INFO_ID_KEY, loginUserInfo.getId());
+
+                    // 修改失效时间,超过24小时没有使用,
+                    Long expire = redisService.getCacheExpire(token,TimeUnit.SECONDS);
+                    log.debug("getCacheExpire-----------当前用户信息 = "+loginUserInfo);
+                    if (expire < 3600) {
+                        redisService.setCacheExpire(token,60 * 60 * 24L * 7, TimeUnit.SECONDS);
+                    }
+                } catch (Exception e) {
+                    log.error("loginHandle error",e);
+                    return false;
+                }
+            } else {
+                return false;
+            }
+        } else {
+            return false;
+        }
         return true;
     }