-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feature/74
- Loading branch information
Showing
6 changed files
with
132 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/main/java/io/oduck/api/global/security/filter/HTTPLoggingFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package io.oduck.api.global.security.filter; | ||
|
||
import static io.oduck.api.global.utils.HttpHeaderUtils.getClientIP; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
import org.springframework.web.util.ContentCachingRequestWrapper; | ||
import org.springframework.web.util.ContentCachingResponseWrapper; | ||
|
||
@Slf4j | ||
@Component | ||
public class HTTPLoggingFilter extends OncePerRequestFilter { | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, | ||
FilterChain filterChain) throws ServletException, IOException { | ||
|
||
if (isAsyncDispatch(request)) { | ||
filterChain.doFilter(request, response); | ||
} else { | ||
doFilterWrapped(new ContentCachingRequestWrapper(request), | ||
new ContentCachingResponseWrapper(response), filterChain); | ||
} | ||
} | ||
|
||
protected void doFilterWrapped(ContentCachingRequestWrapper request, | ||
ContentCachingResponseWrapper response, | ||
FilterChain filterChain) throws IOException, ServletException { | ||
try { | ||
filterChain.doFilter(request, response); | ||
logRequest(request); | ||
} finally { | ||
logResponse(response); | ||
response.copyBodyToResponse(); | ||
} | ||
} | ||
|
||
private static void logRequest(ContentCachingRequestWrapper request) throws IOException { | ||
String queryString = request.getQueryString(); | ||
log.info("Request : \n {} uri=[{}]\n content-type[{}]\n client-ip[{}]\n user-agent[{}]", request.getMethod(), | ||
queryString == null ? request.getRequestURI() : request.getRequestURI() + queryString, | ||
request.getContentType(), getClientIP(request), request.getHeader("User-Agent")); | ||
logPayload("Request", request.getContentType(), request.getContentAsByteArray()); | ||
} | ||
|
||
private static void logResponse(ContentCachingResponseWrapper response) throws IOException { | ||
logPayload("Response", response.getContentType(), response.getContentAsByteArray()); | ||
} | ||
|
||
private static void logPayload(String prefix, String contentType, byte[] rowData) | ||
throws IOException { | ||
boolean visible = isVisible( | ||
MediaType.valueOf(contentType == null ? "application/json" : contentType)); | ||
|
||
if (visible) { | ||
if (rowData.length > 0) { | ||
String contentString = new String(rowData); | ||
log.info("{}\n Payload: {}", prefix, contentString); | ||
} | ||
} else { | ||
log.info("{} Payload: Binary Content", prefix); | ||
} | ||
} | ||
|
||
private static boolean isVisible(MediaType mediaType) { | ||
final List<MediaType> VISIBLE_TYPES = Arrays.asList( | ||
MediaType.valueOf("text/*"), | ||
MediaType.APPLICATION_FORM_URLENCODED, | ||
MediaType.APPLICATION_JSON, | ||
MediaType.APPLICATION_XML, | ||
MediaType.valueOf("application/*+json"), | ||
MediaType.valueOf("application/*+xml"), | ||
MediaType.MULTIPART_FORM_DATA | ||
); | ||
|
||
return VISIBLE_TYPES.stream() | ||
.anyMatch(visibleType -> visibleType.includes(mediaType)); | ||
} | ||
} |
37 changes: 0 additions & 37 deletions
37
src/main/java/io/oduck/api/global/security/filter/ReqLoggingFilter.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters