Bläddra i källkod

修改异常日志

linyk 2 år sedan
förälder
incheckning
0a98186bd6

+ 33 - 17
site/src/main/java/com/mooctest/crowd/site/controller/advice/ExceptionAdvice.java

@@ -1,23 +1,30 @@
 package com.mooctest.crowd.site.controller.advice;
 
+import com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import com.mooctest.crowd.domain.domainobject.ExceptionLog;
 import com.mooctest.crowd.domain.exception.*;
 import com.mooctest.crowd.site.constants.ResponseConstant;
 import com.mooctest.crowd.site.service.ExceptionLogService;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.catalina.util.RequestUtil;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.exception.ExceptionUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpEntity;
 import org.springframework.http.HttpStatus;
 import org.springframework.web.HttpRequestMethodNotSupportedException;
-import org.springframework.web.bind.annotation.ExceptionHandler;
-import org.springframework.web.bind.annotation.ResponseBody;
-import org.springframework.web.bind.annotation.ResponseStatus;
-import org.springframework.web.bind.annotation.RestControllerAdvice;
+import org.springframework.web.bind.WebDataBinder;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.context.request.NativeWebRequest;
 import org.springframework.web.context.request.RequestContextHolder;
 import org.springframework.web.context.request.ServletRequestAttributes;
 
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.BufferedReader;
+import java.io.IOException;
 import java.util.Enumeration;
 
 /**
@@ -108,20 +115,29 @@ public class ExceptionAdvice {
         HttpServletRequest request = requestAttributes.getRequest();
         String exception = ExceptionUtils.getStackTrace(e);
         String uri = request.getRequestURI();
-        Enumeration<String> paramNames = request.getParameterNames();
-        StringBuilder paramSb = new StringBuilder();
-        while(paramNames.hasMoreElements()) {
-            String paramName = paramNames.nextElement();
-            String[] paramVals = request.getParameterValues(paramName);
-            if (paramName.equals("password")) {
-                paramVals[0] = "******";
-            }
-            String paramValues = StringUtils.joinWith("$$", paramVals);
-            paramSb.append(paramName).append(paramValues).append("&");
-        }
+        String contentType = request.getContentType();
         String params = "";
-        if (paramSb.length() > 1) {
-            params = paramSb.substring(0, paramSb.length() - 1);
+        if (contentType.toLowerCase().contains("application/json")) {
+//            params = (String)request.getAttribute("REQ_JSON_BODY");
+//            int index = params.indexOf("password:");
+//            if (index != -1) {
+//                params = params.replace(params.substring(index, params.indexOf("\r\n", index)), "password: ******");
+//            }
+        } else {
+            Enumeration<String> paramNames = request.getParameterNames();
+            StringBuilder paramSb = new StringBuilder();
+            while(paramNames.hasMoreElements()) {
+                String paramName = paramNames.nextElement();
+                String[] paramVals = request.getParameterValues(paramName);
+                if (paramName.equals("password")) {
+                    paramVals[0] = "******";
+                }
+                String paramValues = StringUtils.joinWith("$$", paramVals);
+                paramSb.append(paramName).append("=").append(paramValues).append("&");
+            }
+            if (paramSb.length() > 1) {
+                params = paramSb.substring(0, paramSb.length() - 1);
+            }
         }
         ExceptionLog exceptionLog = new ExceptionLog();
         exceptionLog.setException(exception);

+ 3 - 0
site/src/main/java/com/mooctest/crowd/site/controller/interceptor/SysAdminCheckInterceptor.java

@@ -6,7 +6,9 @@ import com.mooctest.crowd.domain.repository.UserRepo;
 import com.mooctest.crowd.site.annotation.SysAdminRequired;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.MethodParameter;
 import org.springframework.stereotype.Component;
+import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.method.HandlerMethod;
 import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
 
@@ -54,4 +56,5 @@ public class SysAdminCheckInterceptor extends HandlerInterceptorAdapter {
         }
         return false;
     }
+
 }