Bladeren bron

上下文拦截器

yucheng 3 jaren geleden
bovenliggende
commit
865ecfd76b

+ 22 - 0
src/main/java/com/persagy/proxy/adm/utils/AdmContextUtil.java

@@ -0,0 +1,22 @@
+package com.persagy.proxy.adm.utils;
+
+import com.persagy.dmp.common.context.AppContext;
+import com.persagy.proxy.common.entity.InstanceUrlParam;
+
+/**
+ * ADM上下文工具类
+ * @author Charlie Yu
+ * @date 2021-09-13
+ */
+public class AdmContextUtil {
+
+    /**
+     * 转换获取数据中台上下文参数
+     * @return
+     */
+    public static InstanceUrlParam toDmpContext() {
+        return InstanceUrlParam.builder().groupCode(AppContext.getContext().getGroupCode())
+                .projectId(AppContext.getContext().getProjectId())
+                .appId(AppContext.getContext().getAppId()).build();
+    }
+}

+ 31 - 0
src/main/java/com/persagy/proxy/common/config/AdmMiddlewareWebConfigurer.java

@@ -0,0 +1,31 @@
+package com.persagy.proxy.common.config;
+
+import com.persagy.proxy.common.handler.AdmContextHandler;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.core.annotation.Order;
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
+/**
+ * 拦截器
+ * @author Charlie Yu
+ * @date 2021-09-13
+ */
+@Configuration
+@Order(1)
+public class AdmMiddlewareWebConfigurer implements WebMvcConfigurer {
+
+    @Bean
+    public AdmContextHandler admContextHandler() {
+        return new AdmContextHandler();
+    }
+
+    @Override
+    public void addInterceptors(InterceptorRegistry registry) {
+        String[] includePaths = {"/**"};
+        String[] excludePaths = {"/**/*.html", "/**/*.js", "/**/*.css", "/swagger-resources"};
+        // 设置拦截的路径、不拦截的路径、优先级等等
+        registry.addInterceptor(admContextHandler()).order(11).addPathPatterns(includePaths).excludePathPatterns(excludePaths);
+    }
+}

+ 56 - 0
src/main/java/com/persagy/proxy/common/handler/AdmContextHandler.java

@@ -0,0 +1,56 @@
+package com.persagy.proxy.common.handler;
+
+import cn.hutool.core.util.StrUtil;
+import com.persagy.dmp.common.constant.CommonConstant;
+import com.persagy.dmp.common.context.AppContext;
+import org.springframework.web.servlet.ModelAndView;
+import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * 上下文 拦截器
+ * @author Charlie Yu
+ * @date 2021-09-13
+ */
+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
+        }
+        return true;
+    }
+
+    /**
+     * 从请求参数中获取上下文
+     * @param request
+     */
+    private void accessRequestInfo(HttpServletRequest request) {
+        AppContext.getContext().setGroupCode(request.getHeader("groupCode"));
+        AppContext.getContext().setProjectId(request.getHeader("projectId"));
+        AppContext.getContext().setAppId(request.getHeader("appId"));
+        String userId = request.getHeader("userId");
+        // 无用户时,默认为默认系统用户
+        if(StrUtil.isBlank(userId)) {
+            userId = CommonConstant.DEFAULT_ID;
+        }
+        AppContext.getContext().setAccountId(userId);
+    }
+
+    @Override
+    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
+        AppContext.unload();
+    }
+
+    @Override
+    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
+        AppContext.unload();
+    }
+}