Browse Source

从body里获取用户信息

lixing 4 years ago
parent
commit
51bdc59fea

+ 20 - 0
apm-common/src/main/java/com/persagy/apm/common/config/JacksonConfig.java

@@ -52,11 +52,21 @@ public class JacksonConfig {
         }
     }
 
+    public static class MyNullNumberJsonSerializer extends JsonSerializer {
+        @Override
+        public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
+                throws IOException {
+            jsonGenerator.writeNull();
+        }
+    }
+
     public static class MyBeanSerializerModifier extends BeanSerializerModifier {
         // 数组,集合类型 null -> []
         private JsonSerializer nullArrayJsonSerializer = new MyNullArrayJsonSerializer();
         // 字符串等类型 null -> ""
         private JsonSerializer nullObjJsonSerializer = new MyNullObjJsonSerializer();
+        // 数字 null -> null;
+        private JsonSerializer nullNumberJsonSerializer = new MyNullNumberJsonSerializer();
 
         @Override
         public List<BeanPropertyWriter> changeProperties(
@@ -69,6 +79,8 @@ public class JacksonConfig {
                     writer.assignNullSerializer(this.nullArrayJsonSerializer);
                 } else if (!isBasicType(writer)){
                     writer.assignNullSerializer(this.nullObjJsonSerializer);
+                } else if (isNumber(writer)) {
+                    writer.assignNullSerializer(nullNumberJsonSerializer);
                 }
             }
             return beanProperties;
@@ -91,5 +103,13 @@ public class JacksonConfig {
                     clazz.equals(Date.class) ||
                     clazz.equals(String.class);
         }
+
+        boolean isNumber(BeanPropertyWriter writer) {
+            Class clazz = writer.getPropertyType();
+            return clazz.equals(Integer.class) ||
+                    clazz.equals(Long.class) ||
+                    clazz.equals(Double.class) ||
+                    clazz.equals(Float.class);
+        }
     }
 }

+ 21 - 12
apm-common/src/main/java/com/persagy/apm/common/handler/PoemsContextHandler.java

@@ -1,19 +1,18 @@
 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.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
 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.io.IOUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.web.servlet.ModelAndView;
 import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
 
+import javax.servlet.ServletInputStream;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-import java.io.UnsupportedEncodingException;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * @description:
@@ -24,7 +23,9 @@ import java.io.UnsupportedEncodingException;
  */
 public class PoemsContextHandler extends HandlerInterceptorAdapter {
 
-    /** 忽略的url - swagger的文档不校验 */
+    /**
+     * 忽略的url - swagger的文档不校验
+     */
     private static final String[] IGNORE_URL = {".html", ".js", ".css", "/swagger-resources"};
 
     @Override
@@ -33,11 +34,19 @@ public class PoemsContextHandler extends HandlerInterceptorAdapter {
         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);
+        ServletInputStream inputStream = request.getInputStream();
+        String reqBodyStr = IOUtils.toString(inputStream, "utf-8");
+        if (StringUtils.isNotBlank(reqBodyStr)) {
+            //reqBodyStr转为Map对象
+            Map<String, Object> paramMap = new ObjectMapper().readValue(reqBodyStr, new TypeReference<HashMap<String, Object>>() {
+            });
+            String userId = (String) paramMap.get("userId");
+            String loginDevice = (String) paramMap.get("loginDevice");
+            String pd = (String) paramMap.get("pd");
+            PoemsContext.setContext(userId, loginDevice, pd);
+        }
+
         return true;
     }