Browse Source

功能:请求接口添加补充集团编码的逻辑

lijie 3 năm trước cách đây
mục cha
commit
9c8a5ef2e9

+ 11 - 0
pom.xml

@@ -68,6 +68,12 @@
 		<dependency>
 			<groupId>com.persagy</groupId>
 			<artifactId>dmp-server</artifactId>
+			<exclusions>
+				<exclusion>
+					<groupId>com.persagy</groupId>
+					<artifactId>dmp-auth</artifactId>
+				</exclusion>
+			</exclusions>
 		</dependency>
 		<dependency>
 			<groupId>javax.ws.rs</groupId>
@@ -98,5 +104,10 @@
 			<artifactId>gt-main</artifactId>
 			<version>23.0</version>
 		</dependency>
+		<dependency>
+			<groupId>com.persagy</groupId>
+			<artifactId>dmp-auth-starter</artifactId>
+			<version>1.0.0</version>
+		</dependency>
 	</dependencies>
 </project>

+ 2 - 0
src/main/java/com/persagy/proxy/adm/constant/AdmCommonConstant.java

@@ -63,6 +63,8 @@ public interface AdmCommonConstant {
 
     /** 楼层标高 */
     String FLOOR_ELEVATION = "floorElevation";
+    /** 集团编码的code */
+    String MIDDLEWARE_GROUP_CODE = "middleware.group.code";
     
     /** 识别码类型 */
 	static Map<String, String> codeColMap = new HashMap<String,String>(){

+ 27 - 3
src/main/java/com/persagy/proxy/common/config/AdmMiddlewareWebConfigurer.java

@@ -1,6 +1,12 @@
 package com.persagy.proxy.common.config;
 
+import com.persagy.dmp.auth.client.EmsSaasWebFallbackFactory;
+import com.persagy.dmp.auth.handler.AppContextHandler;
+import com.persagy.dmp.auth.service.AuthService;
 import com.persagy.proxy.common.handler.AdmContextHandler;
+import com.persagy.proxy.common.service.impl.EmsAuthServiceImpl;
+import com.persagy.proxy.common.service.impl.NoneAuthServiceImpl;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.core.annotation.Order;
@@ -17,8 +23,8 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 public class AdmMiddlewareWebConfigurer implements WebMvcConfigurer {
 
     @Bean
-    public AdmContextHandler admContextHandler() {
-        return new AdmContextHandler();
+    public AppContextHandler appContextHandler() {
+        return new AppContextHandler();
     }
 
     @Override
@@ -26,6 +32,24 @@ public class AdmMiddlewareWebConfigurer implements WebMvcConfigurer {
         String[] includePaths = {"/**"};
         String[] excludePaths = {"/**/*.html", "/**/*.js", "/**/*.css", "/swagger-resources"};
         // 设置拦截的路径、不拦截的路径、优先级等等
-        registry.addInterceptor(admContextHandler()).order(11).addPathPatterns(includePaths).excludePathPatterns(excludePaths);
+        registry.addInterceptor(appContextHandler()).order(11).addPathPatterns(includePaths).excludePathPatterns(excludePaths);
+    }
+
+    @Bean
+    @ConditionalOnProperty(value = "persagy.common.auth.channel", havingValue = "1")
+    public EmsSaasWebFallbackFactory emsSaasWebFallbackFactory(){
+        return new EmsSaasWebFallbackFactory();
+    }
+
+    @Bean
+    @ConditionalOnProperty(value = "persagy.common.auth.channel", havingValue = "1")
+    public AuthService emsAuthServiceImpl(){
+        return new EmsAuthServiceImpl();
+    }
+
+    @Bean
+    @ConditionalOnProperty(value = "persagy.common.auth.channel", havingValue = "-1",matchIfMissing = true)
+    public AuthService noneAuthServiceImpl(){
+        return new NoneAuthServiceImpl();
     }
 }

+ 13 - 7
src/main/java/com/persagy/proxy/common/handler/AdmContextHandler.java

@@ -1,12 +1,15 @@
 package com.persagy.proxy.common.handler;
 
 import cn.hutool.core.util.StrUtil;
+import com.persagy.dmp.auth.service.AuthService;
+import com.persagy.dmp.auth.service.AuthServiceFactory;
 import com.persagy.dmp.common.constant.CommonConstant;
 import com.persagy.dmp.common.context.AppContext;
 import com.persagy.dmp.common.helper.SpringHelper;
 import org.springframework.web.servlet.ModelAndView;
 import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
 
+import javax.security.auth.message.AuthException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
@@ -21,13 +24,16 @@ public class AdmContextHandler extends HandlerInterceptorAdapter {
 
     @Override
     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
-        // 从header中获取上下文
-//        if(StrUtil.isNotBlank(request.getHeader("groupCode"))) {
-            // 请求参数中包含groupCode,由于没有标识位,暂时用这种方式判断吧
-            accessRequestInfo(request);
-//        } else {
-//            // 其他情况,按现有系统默认不验证的逻辑,其他情况就是默认放行,不必处理。最好是抛401
-//        }
+        AuthService authService = AuthServiceFactory.getAuthService();
+        // 登录校验
+        boolean isSuccess = authService.validateLogin(request, response);
+        if(!isSuccess) {
+            // 鉴权器处理
+            authService.loginFailure(request, response);
+            throw new AuthException("登录认证失败!");
+        }
+        // 登录成功处理
+        authService.loginSuccess(request, response);
         return true;
     }
 

+ 95 - 0
src/main/java/com/persagy/proxy/common/service/impl/EmsAuthServiceImpl.java

@@ -0,0 +1,95 @@
+package com.persagy.proxy.common.service.impl;
+
+import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.util.StrUtil;
+import com.alibaba.fastjson.JSONObject;
+import com.persagy.dmp.auth.client.EmsSaasWebClient;
+import com.persagy.dmp.auth.domain.Group;
+import com.persagy.dmp.auth.domain.Project;
+import com.persagy.dmp.auth.domain.ResultReturn;
+import com.persagy.dmp.auth.service.impl.AbstractAuthServiceImpl;
+import com.persagy.dmp.common.constant.CommonConstant;
+import com.persagy.dmp.common.constant.ResponseCode;
+import com.persagy.dmp.common.context.AppContext;
+import com.persagy.dmp.common.exception.BusinessException;
+import com.persagy.dmp.common.helper.SpringHelper;
+import com.persagy.proxy.adm.constant.AdmCommonConstant;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * 运维平台2.0 鉴权实现类
+ * 从head中获取上下文信息,并校验集团、项目是否有效
+ * @author Charlie Yu
+ * @date 2021-11-02
+ */
+public class EmsAuthServiceImpl extends AbstractAuthServiceImpl {
+    @Autowired
+    private EmsSaasWebClient emsSaasWebClient;
+
+    @Override
+    public void loginSuccess(HttpServletRequest request, HttpServletResponse response) {
+        super.loginSuccess(request, response);
+        loadContextByRequest(request);
+        if (StrUtil.isBlank(AppContext.getContext().getGroupCode())
+                && StrUtil.isBlank(AppContext.getContext().getProjectId())){
+            // 两者都为空时不做校验
+            return;
+        }
+        ResultReturn<Group> queryResult = emsSaasWebClient.queryGroupProjectList(new JSONObject());
+        if (!CommonConstant.QUERY_SUCCESS.equals(queryResult.getResult())){
+            throw new BusinessException(queryResult.getResultMsg());
+        }
+        List<Group> content = queryResult.getContent();
+        if (CollUtil.isEmpty(content)){
+            throw new BusinessException(ResponseCode.A0402.getDesc(), ResponseCode.A0402.getDesc());
+        }
+        Map<String, List<Project>> groupMap = content.stream()
+                .collect(Collectors.toMap(Group::getGroupCode, Group::getProjects, (k1, k2) -> k1));
+        // 1.没有集团编码时根据项目匹配集团
+        if (StrUtil.isBlank(AppContext.getContext().getGroupCode())){
+            // 如果集团编码为空则通过项目id查找
+            Set<Map.Entry<String, List<Project>>> entries = groupMap.entrySet();
+            flag:
+            for (Map.Entry<String, List<Project>> entry : entries) {
+                if (CollUtil.isEmpty(entry.getValue())){
+                    continue;
+                }
+                for (Project project : entry.getValue()) {
+                    if (AppContext.getContext().getProjectId().equals(project.getProjectId())){
+                        AppContext.getContext().setGroupCode(entry.getKey());
+                        break flag;
+                    }
+                }
+            }
+        }
+        if (StrUtil.isBlank(AppContext.getContext().getGroupCode())){
+            // 无集团编码时,默认为配置的集团编码
+            AppContext.getContext().setGroupCode(SpringHelper.getString(AdmCommonConstant.MIDDLEWARE_GROUP_CODE));
+        }
+        // 2.验证集团是否有效
+        if (StrUtil.isBlank(AppContext.getContext().getGroupCode())
+                || !groupMap.containsKey(AppContext.getContext().getGroupCode())){
+            throw new BusinessException(ResponseCode.A0402.getCode(), ResponseCode.A0402.getDesc());
+        }
+    }
+
+    @Override
+    protected void loadContextByRequest(HttpServletRequest request) {
+        AppContext context = AppContext.getContext();
+        context.setGroupCode(request.getHeader("groupCode"));
+        context.setProjectId(request.getHeader("projectId"));
+        context.setAppId(request.getHeader("appId"));
+        String userId = request.getHeader("userId");
+        // 无用户时,默认为默认系统用户
+        if(StrUtil.isBlank(userId)) {
+            context.setAccountId(CommonConstant.DEFAULT_ID);
+        }
+    }
+}

+ 44 - 0
src/main/java/com/persagy/proxy/common/service/impl/NoneAuthServiceImpl.java

@@ -0,0 +1,44 @@
+package com.persagy.proxy.common.service.impl;
+
+import cn.hutool.core.util.StrUtil;
+import com.persagy.dmp.auth.service.impl.AbstractAuthServiceImpl;
+import com.persagy.dmp.common.constant.CommonConstant;
+import com.persagy.dmp.common.context.AppContext;
+import com.persagy.dmp.common.helper.SpringHelper;
+import com.persagy.proxy.adm.constant.AdmCommonConstant;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * 不鉴权 实现类
+ * 直接从head中获取上下文信息
+ * @author Charlie Yu
+ * @date 2021-11-02
+ */
+public class NoneAuthServiceImpl extends AbstractAuthServiceImpl {
+
+    @Override
+    public void loginSuccess(HttpServletRequest request, HttpServletResponse response) {
+        super.loginSuccess(request, response);
+        loadContextByRequest(request);
+    }
+
+    @Override
+    protected void loadContextByRequest(HttpServletRequest request) {
+        AppContext context = AppContext.getContext();
+        context.setGroupCode(request.getHeader("groupCode"));
+        context.setProjectId(request.getHeader("projectId"));
+        context.setAppId(request.getHeader("appId"));
+        String userId = request.getHeader("userId");
+        // 无用户时,默认为默认系统用户
+        if(StrUtil.isBlank(userId)) {
+            context.setAccountId(CommonConstant.DEFAULT_ID);
+        }
+        // 无集团编码时,默认为配置的集团编码
+        String groupCode = request.getHeader("groupCode");
+        if(StrUtil.isBlank(groupCode)) {
+            context.setGroupCode(SpringHelper.getString(AdmCommonConstant.MIDDLEWARE_GROUP_CODE));
+        }
+    }
+}