From 00520818ec5c7f019dcc1c359bb8a062f09b12ae Mon Sep 17 00:00:00 2001 From: kdomo Date: Mon, 16 Jan 2023 02:12:01 +0900 Subject: [PATCH] =?UTF-8?q?[BE-168]=20fix:=20HTTPLogFilter=EC=97=90?= =?UTF-8?q?=EC=84=9C=20body=EA=B0=92=20=EC=B6=9C=EB=A0=A5=20=EC=95=88?= =?UTF-8?q?=EB=90=98=EB=8D=98=20=EB=AC=B8=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../recordit/server/logger/HTTPLogFilter.java | 49 +++++++++---------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/recordit/server/logger/HTTPLogFilter.java b/src/main/java/com/recordit/server/logger/HTTPLogFilter.java index 270afba1..b8ab47ba 100644 --- a/src/main/java/com/recordit/server/logger/HTTPLogFilter.java +++ b/src/main/java/com/recordit/server/logger/HTTPLogFilter.java @@ -17,7 +17,6 @@ import org.springframework.stereotype.Component; import org.springframework.web.util.ContentCachingRequestWrapper; import org.springframework.web.util.ContentCachingResponseWrapper; -import org.springframework.web.util.WebUtils; import lombok.extern.slf4j.Slf4j; @@ -39,10 +38,11 @@ public void doFilter( ); long start = System.currentTimeMillis(); - chain.doFilter(request, response); + chain.doFilter(requestWrapper, responseWrapper); long end = System.currentTimeMillis(); - if (isInExcludeURIForLog(request)) { + if (isInExcludeURIForLog(requestWrapper)) { + responseWrapper.copyBodyToResponse(); return; } @@ -56,35 +56,35 @@ public void doFilter( ((HttpServletRequest)request).getRequestURI(), responseWrapper.getStatus(), (end - start), - getHeaders((HttpServletRequest)request), + getHeaders(requestWrapper), getRequestParameter(requestWrapper), getRequestBody(requestWrapper), getResponseBody(responseWrapper)); } - private boolean isInExcludeURIForLog(ServletRequest request) { - if (((HttpServletRequest)request).getRequestURI().contains("/api/swagger")) { + private boolean isInExcludeURIForLog(ContentCachingRequestWrapper requestWrapper) { + if ((requestWrapper.getRequestURI().contains("/api/swagger"))) { return true; } - if ("/v2/api-docs".equals(((HttpServletRequest)request).getRequestURI())) { + if ("/v2/api-docs".equals(requestWrapper.getRequestURI())) { return true; } return false; } - private Map getHeaders(HttpServletRequest request) { + private Map getHeaders(ContentCachingRequestWrapper requestWrapper) { Map headerMap = new HashMap<>(); - Enumeration headerArray = request.getHeaderNames(); + Enumeration headerArray = requestWrapper.getHeaderNames(); while (headerArray.hasMoreElements()) { String headerName = (String)headerArray.nextElement(); - headerMap.put(headerName, request.getHeader(headerName)); + headerMap.put(headerName, requestWrapper.getHeader(headerName)); } return headerMap; } - private String getRequestParameter(ContentCachingRequestWrapper request) { - Map parameterMap = request.getParameterMap(); + private String getRequestParameter(ContentCachingRequestWrapper requestWrapper) { + Map parameterMap = requestWrapper.getParameterMap(); if (parameterMap.size() == 0) { return "-"; } @@ -101,13 +101,12 @@ private String getRequestParameter(ContentCachingRequestWrapper request) { return sb.toString(); } - private String getRequestBody(ContentCachingRequestWrapper request) { - ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class); - if (wrapper != null) { - byte[] buf = wrapper.getContentAsByteArray(); + private String getRequestBody(ContentCachingRequestWrapper requestWrapper) { + if (requestWrapper != null) { + byte[] buf = requestWrapper.getContentAsByteArray(); if (buf.length > 0) { try { - return new String(buf, 0, buf.length, wrapper.getCharacterEncoding()); + return new String(buf, 0, buf.length, requestWrapper.getCharacterEncoding()); } catch (UnsupportedEncodingException e) { return " - "; } @@ -116,18 +115,14 @@ private String getRequestBody(ContentCachingRequestWrapper request) { return " - "; } - private String getResponseBody(final HttpServletResponse response) throws IOException { + private String getResponseBody(ContentCachingResponseWrapper responseWrapper) throws IOException { String payload = null; - ContentCachingResponseWrapper wrapper = WebUtils.getNativeResponse( - response, - ContentCachingResponseWrapper.class - ); - if (wrapper != null) { - wrapper.setCharacterEncoding("UTF-8"); - byte[] buf = wrapper.getContentAsByteArray(); + if (responseWrapper != null) { + responseWrapper.setCharacterEncoding("UTF-8"); + byte[] buf = responseWrapper.getContentAsByteArray(); if (buf.length > 0) { - payload = new String(buf, 0, buf.length, wrapper.getCharacterEncoding()); - wrapper.copyBodyToResponse(); + payload = new String(buf, 0, buf.length, responseWrapper.getCharacterEncoding()); + responseWrapper.copyBodyToResponse(); } } return null == payload ? " - " : payload;