From 1613374fcca67381e9fcf6b3677527d66f6ea3db Mon Sep 17 00:00:00 2001 From: liquor <958142070@qq.com> Date: Wed, 26 Mar 2025 14:41:36 +0000 Subject: [PATCH] =?UTF-8?q?fix(log):=20=E4=BF=AE=E5=A4=8D=E6=9E=84?= =?UTF-8?q?=E5=BB=BA=E8=AF=B7=E6=B1=82=E5=8F=AF=E9=87=8D=E5=A4=8D=E8=AF=BB?= =?UTF-8?q?=E6=B5=81=E5=90=8E=E8=BF=87=E6=BB=A4=E6=96=87=E4=BB=B6=E6=B5=81?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E7=9A=84=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../log/http/servlet/RecordableServletHttpRequest.java | 8 ++++++-- .../top/continew/starter/log/util/AccessLogUtils.java | 8 +++++++- .../starter/web/util/RepeatReadRequestWrapper.java | 8 ++++---- .../starter/web/util/RepeatReadResponseWrapper.java | 5 +++-- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/http/servlet/RecordableServletHttpRequest.java b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/http/servlet/RecordableServletHttpRequest.java index ed0fe45e..fd90d950 100644 --- a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/http/servlet/RecordableServletHttpRequest.java +++ b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/http/servlet/RecordableServletHttpRequest.java @@ -23,6 +23,7 @@ import jakarta.servlet.http.HttpServletRequest; import org.springframework.web.util.UriUtils; import top.continew.starter.core.constant.StringConstants; import top.continew.starter.log.http.RecordableHttpRequest; +import top.continew.starter.web.util.RepeatReadRequestWrapper; import java.net.URI; import java.net.URISyntaxException; @@ -79,8 +80,11 @@ public final class RecordableServletHttpRequest implements RecordableHttpRequest @Override public String getBody() { - String body = JakartaServletUtil.getBody(request); - return JSONUtil.isTypeJSON(body) ? body : null; + if (request instanceof RepeatReadRequestWrapper wrapper && !wrapper.isMultipartContent(request)) { + String body = JakartaServletUtil.getBody(request); + return JSONUtil.isTypeJSON(body) ? body : null; + } + return null; } @Override diff --git a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/util/AccessLogUtils.java b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/util/AccessLogUtils.java index 1ab72b26..010180c2 100644 --- a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/util/AccessLogUtils.java +++ b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/util/AccessLogUtils.java @@ -48,7 +48,13 @@ public class AccessLogUtils { } // 参数为空返回空 - Map params = request.getParam(); + Map params; + try { + params = request.getParam(); + } catch (Exception e) { + return null; + } + if (ObjectUtil.isEmpty(params) || params.isEmpty()) { return null; } diff --git a/continew-starter-web/src/main/java/top/continew/starter/web/util/RepeatReadRequestWrapper.java b/continew-starter-web/src/main/java/top/continew/starter/web/util/RepeatReadRequestWrapper.java index e9c36f8c..5bb3a1d7 100644 --- a/continew-starter-web/src/main/java/top/continew/starter/web/util/RepeatReadRequestWrapper.java +++ b/continew-starter-web/src/main/java/top/continew/starter/web/util/RepeatReadRequestWrapper.java @@ -87,18 +87,18 @@ public class RepeatReadRequestWrapper extends HttpServletRequestWrapper { public BufferedReader getReader() throws IOException { // 如果是文件上传,直接返回原始Reader if (isMultipartContent(originalRequest)) { - return originalRequest.getReader(); + new BufferedReader(new InputStreamReader(originalRequest.getInputStream(), StandardCharsets.UTF_8)); } - return new BufferedReader(new InputStreamReader(new ByteArrayInputStream(cachedBody), StandardCharsets.UTF_8)); + return new BufferedReader(new InputStreamReader(getInputStream())); } /** * 检查是否为文件上传请求 - * + * * @param request 请求对象 * @return 是否为文件上传请求 */ - private boolean isMultipartContent(HttpServletRequest request) { + public boolean isMultipartContent(HttpServletRequest request) { return request.getContentType() != null && request.getContentType().toLowerCase().startsWith("multipart/"); } } diff --git a/continew-starter-web/src/main/java/top/continew/starter/web/util/RepeatReadResponseWrapper.java b/continew-starter-web/src/main/java/top/continew/starter/web/util/RepeatReadResponseWrapper.java index aa3080cb..c64e6bd3 100644 --- a/continew-starter-web/src/main/java/top/continew/starter/web/util/RepeatReadResponseWrapper.java +++ b/continew-starter-web/src/main/java/top/continew/starter/web/util/RepeatReadResponseWrapper.java @@ -20,6 +20,7 @@ import jakarta.servlet.ServletOutputStream; import jakarta.servlet.WriteListener; import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponseWrapper; +import org.springframework.http.MediaType; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -54,7 +55,7 @@ public class RepeatReadResponseWrapper extends HttpServletResponseWrapper { // 根据 Content-Type 判断是否为流式响应 if (type != null) { String lowerType = type.toLowerCase(); - isStreamingResponse = lowerType.contains("text/event-stream"); + isStreamingResponse = lowerType.contains(MediaType.TEXT_EVENT_STREAM_VALUE); } } @@ -62,7 +63,7 @@ public class RepeatReadResponseWrapper extends HttpServletResponseWrapper { String contentType = getContentType(); if (contentType != null) { String lowerType = contentType.toLowerCase(); - isStreamingResponse = lowerType.contains("text/event-stream"); + isStreamingResponse = lowerType.contains(MediaType.TEXT_EVENT_STREAM_VALUE); } }