#
Junjie
1 天以前 c34f9c17fde14f78b3663803e9776d438e8481b9
src/main/java/com/zy/common/config/AdminInterceptor.java
@@ -16,9 +16,12 @@
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.ContentCachingRequestWrapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.LinkedHashMap;
@@ -99,6 +102,7 @@
            return;
        }
        OperateLog operateLog = (OperateLog) obj;
        operateLog.setRequest(buildRequestContent(request));
        operateLog.setResponse(buildResponseContent(response, ex));
        try {
            operateLogService.save(operateLog);
@@ -188,11 +192,38 @@
        operateLog.setAction(Cools.isEmpty(memo) ? request.getRequestURI() : memo);
        operateLog.setIp(request.getRemoteAddr());
        operateLog.setUserId(userId);
        operateLog.setRequest(JSON.toJSONString(request.getParameterMap()));
        operateLog.setCreateTime(new Date());
        request.setAttribute(ATTR_OPERATE_LOG, operateLog);
    }
    private String buildRequestContent(HttpServletRequest request) {
        String body = readCachedBody(request);
        if (!Cools.isEmpty(body)) {
            return body;
        }
        return JSON.toJSONString(request.getParameterMap());
    }
    private String readCachedBody(HttpServletRequest request) {
        if (!(request instanceof ContentCachingRequestWrapper)) {
            return null;
        }
        ContentCachingRequestWrapper wrapper = (ContentCachingRequestWrapper) request;
        byte[] content = wrapper.getContentAsByteArray();
        if (content == null || content.length == 0) {
            return null;
        }
        Charset charset = StandardCharsets.UTF_8;
        if (!Cools.isEmpty(wrapper.getCharacterEncoding())) {
            try {
                charset = Charset.forName(wrapper.getCharacterEncoding());
            } catch (Exception ignored) {
            }
        }
        String body = new String(content, charset).trim();
        return body.isEmpty() ? null : body;
    }
    private String buildResponseContent(HttpServletResponse response, Exception ex) {
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("status", response.getStatus());