Skip to content

Commit

Permalink
Merge pull request #95 from Billing-Wise/K5P-76/feat/로깅
Browse files Browse the repository at this point in the history
[feat] api 호출만 로깅 및 민감정보 마스킹
  • Loading branch information
dtd1614 authored Jul 31, 2024
2 parents 5af6618 + 0b1f944 commit 86e33c2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package site.billingwise.api.serverapi.global.log;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.util.ContentCachingRequestWrapper;
Expand All @@ -13,7 +15,7 @@

@Value
@JsonInclude(JsonInclude.Include.NON_NULL)
public class HttpLogMessage {
public class ApiLogMessage {
String httpMethod;
String requestUri;
String httpStatus;
Expand All @@ -26,7 +28,7 @@ public class HttpLogMessage {

private static final ObjectMapper objectMapper = new ObjectMapper();

public static HttpLogMessage createFrom(
public static ApiLogMessage createFrom(
ContentCachingRequestWrapper requestWrapper,
ContentCachingResponseWrapper responseWrapper,
Double elapsedTime
Expand All @@ -38,10 +40,10 @@ public static HttpLogMessage createFrom(

String headers = getRequestHeaders(requestWrapper);
String requestParam = getRequestParams(requestWrapper);
String requestBody = getRequestBody(requestWrapper);
String requestBody = maskSensitiveInfo(getRequestBody(requestWrapper));
String responseBody = getResponseBody(responseWrapper);

return new HttpLogMessage(
return new ApiLogMessage(
httpMethod,
requestUri,
httpStatus,
Expand Down Expand Up @@ -74,6 +76,19 @@ private static String getResponseBody(ContentCachingResponseWrapper response) {
return new String(response.getContentAsByteArray(), StandardCharsets.UTF_8);
}

private static String maskSensitiveInfo(String content) {
try {
JsonNode jsonNode = objectMapper.readTree(content);
if (jsonNode.has("password")) {
((ObjectNode) jsonNode).put("password", "*****");
}
return objectMapper.writeValueAsString(jsonNode);
} catch (Exception e) {
// JSON 파싱에 실패한 경우, 간단한 문자열 치환
return content.replaceAll("\"password\"\\s*:\\s*\"[^\"]*\"", "\"password\":\"*****\"");
}
}

public String toJsonLog() {
try {
return objectMapper.writeValueAsString(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ReqResLoggingFilter extends OncePerRequestFilter {
private static final Logger log = LoggerFactory.getLogger(ReqResLoggingFilter.class);
public class ApiLoggingFilter extends OncePerRequestFilter {
private static final Logger log = LoggerFactory.getLogger(ApiLoggingFilter.class);
private static final String REQUEST_ID = "request_id";
private static final String ACTUATOR_PATH = "/actuator";
private static final String API_PATH = "/api";

@Override
protected void doFilterInternal(
HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain
) throws ServletException, IOException {
if (isActuatorRequest(request)) {
if (!isApiRequest(request)) {
filterChain.doFilter(request, response);
return;
}
Expand All @@ -51,8 +51,8 @@ protected void doFilterInternal(
}
}

private boolean isActuatorRequest(HttpServletRequest request) {
return request.getRequestURI().startsWith(ACTUATOR_PATH);
private boolean isApiRequest(HttpServletRequest request) {
return request.getRequestURI().startsWith(API_PATH);
}

private String generateRequestId() {
Expand All @@ -64,7 +64,7 @@ private void logRequest(ContentCachingRequestWrapper requestWrapper,
long startTime, long endTime) {
try {
double elapsedTime = (endTime - startTime) / 1000.0;
HttpLogMessage logMessage = HttpLogMessage.createFrom(requestWrapper, responseWrapper, elapsedTime);
ApiLogMessage logMessage = ApiLogMessage.createFrom(requestWrapper, responseWrapper, elapsedTime);
log.info("REQUEST_LOG: " + logMessage.toJsonLog());
responseWrapper.copyBodyToResponse();
} catch (Exception e) {
Expand Down

0 comments on commit 86e33c2

Please sign in to comment.