Browse Source

处理前端传入的用户数据

lixing 3 years ago
parent
commit
31725587c2

+ 5 - 5
apm-common/src/main/java/com/persagy/apm/common/config/CommonWebConfigurer.java

@@ -18,15 +18,15 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 @Order(1)
 public class CommonWebConfigurer implements WebMvcConfigurer {
 
-    @Bean
-    public AppContextHandler appContextHandler() {
-        return new AppContextHandler();
-    }
+//    @Bean
+//    public AppContextHandler appContextHandler() {
+//        return new AppContextHandler();
+//    }
 
     @Override
     public void addInterceptors(InterceptorRegistry registry) {
         // 设置拦截的路径、不拦截的路径、优先级等等
-        registry.addInterceptor(appContextHandler()).order(10).addPathPatterns("/**");
+//        registry.addInterceptor(appContextHandler()).order(10).addPathPatterns("/**");
     }
 
 }

+ 33 - 0
apm-common/src/main/java/com/persagy/apm/common/context/poems/PoemsContext.java

@@ -0,0 +1,33 @@
+package com.persagy.apm.common.context.poems;
+
+import com.persagy.apm.common.context.AppContext;
+import com.persagy.apm.common.handler.PoemsContextHandler;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+/**
+ * poems上下文
+ *
+ * @author lixing
+ * @version V1.0 2021/5/19 7:14 下午
+ **/
+public class PoemsContext {
+    /** 线程级的上下文 */
+    private static ThreadLocal<PoemsContextContent> context = new ThreadLocal<>();
+
+    public static void unloadContext() {
+        context.remove();
+    }
+
+    public static void setContext(String userId, String loginDevice, String pd) {
+        PoemsContextContent poemsContextContent = context.get();
+        poemsContextContent.setUserId(userId);
+        poemsContextContent.setLoginDevice(loginDevice);
+        poemsContextContent.setPd(pd);
+    }
+
+    public static PoemsContextContent getContext() {
+        return context.get();
+    }
+}

+ 22 - 0
apm-common/src/main/java/com/persagy/apm/common/context/poems/PoemsContextContent.java

@@ -0,0 +1,22 @@
+package com.persagy.apm.common.context.poems;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+/**
+ * poems上下文
+ *
+ * @author lixing
+ * @version V1.0 2021/5/19 7:14 下午
+ **/
+@ApiModel
+@Data
+public class PoemsContextContent {
+    @ApiModelProperty(value = "当前用户id")
+    private String userId;
+    @ApiModelProperty(value = "登录设备")
+    private String loginDevice;
+    @ApiModelProperty(value = "用户密码MD5值")
+    private String pd;
+}

+ 53 - 0
apm-common/src/main/java/com/persagy/apm/common/handler/PoemsContextHandler.java

@@ -0,0 +1,53 @@
+package com.persagy.apm.common.handler;
+
+import com.alibaba.fastjson.JSONObject;
+import com.persagy.apm.common.constant.AppContextConstants;
+import com.persagy.apm.common.context.AppContext;
+import com.persagy.apm.common.context.DefaultAppContext;
+import com.persagy.apm.common.context.poems.PoemsContext;
+import com.persagy.apm.common.utils.SecureAES;
+import com.persagy.security.exception.AESDecryptException;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.web.servlet.ModelAndView;
+import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.UnsupportedEncodingException;
+
+/**
+ * @description:
+ * @author: lixing
+ * @company: Persagy Technology Co.,Ltd
+ * @since: 2021/3/8 9:35 上午
+ * @version: V1.0
+ */
+public class PoemsContextHandler extends HandlerInterceptorAdapter {
+
+    /** 忽略的url - swagger的文档不校验 */
+    private static final String[] IGNORE_URL = {".html", ".js", ".css", "/swagger-resources"};
+
+    @Override
+    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
+        String requestURI = request.getRequestURI();
+        if (StringUtils.containsAny(requestURI, IGNORE_URL)) {
+            return true;
+        }
+        String userId = request.getParameter("userId");
+        String loginDevice = request.getParameter("loginDevice");
+        String pd = request.getParameter("pd");
+
+        PoemsContext.setContext(userId, loginDevice, pd);
+        return true;
+    }
+
+    @Override
+    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
+        PoemsContext.unloadContext();
+    }
+
+    @Override
+    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
+        PoemsContext.unloadContext();
+    }
+}