|
| 1 | +package com.example.solidconnection.common.filter; |
| 2 | + |
| 3 | +import jakarta.servlet.FilterChain; |
| 4 | +import jakarta.servlet.ServletException; |
| 5 | +import jakarta.servlet.http.HttpServletRequest; |
| 6 | +import jakarta.servlet.http.HttpServletResponse; |
| 7 | +import java.io.IOException; |
| 8 | +import java.net.URLDecoder; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | +import java.util.List; |
| 11 | +import lombok.RequiredArgsConstructor; |
| 12 | +import lombok.extern.slf4j.Slf4j; |
| 13 | +import org.slf4j.MDC; |
| 14 | +import org.springframework.http.HttpStatus; |
| 15 | +import org.springframework.stereotype.Component; |
| 16 | +import org.springframework.util.AntPathMatcher; |
| 17 | +import org.springframework.web.filter.OncePerRequestFilter; |
| 18 | + |
| 19 | +@Slf4j |
| 20 | +@RequiredArgsConstructor |
| 21 | +@Component |
| 22 | +public class HttpLoggingFilter extends OncePerRequestFilter { |
| 23 | + |
| 24 | + private static final AntPathMatcher PATH_MATCHER = new AntPathMatcher(); |
| 25 | + private static final List<String> EXCLUDE_PATTERNS = List.of("/actuator/**"); |
| 26 | + private static final List<String> EXCLUDE_QUERIES = List.of("token"); |
| 27 | + private static final String MASK_VALUE = "****"; |
| 28 | + |
| 29 | + @Override |
| 30 | + protected void doFilterInternal( |
| 31 | + HttpServletRequest request, |
| 32 | + HttpServletResponse response, |
| 33 | + FilterChain filterChain |
| 34 | + ) throws ServletException, IOException { |
| 35 | + |
| 36 | + // 1) traceId 부여 |
| 37 | + String traceId = generateTraceId(); |
| 38 | + MDC.put("traceId", traceId); |
| 39 | + |
| 40 | + boolean excluded = isExcluded(request); |
| 41 | + |
| 42 | + // 2) 로깅 제외 대상이면 그냥 통과 (traceId는 유지: 추후 하위 레이어 로그에도 붙음) |
| 43 | + if (excluded) { |
| 44 | + try { |
| 45 | + filterChain.doFilter(request, response); |
| 46 | + } finally { |
| 47 | + MDC.clear(); |
| 48 | + } |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + printRequestUri(request); |
| 53 | + |
| 54 | + try { |
| 55 | + filterChain.doFilter(request, response); |
| 56 | + printResponse(request, response); |
| 57 | + } finally { |
| 58 | + MDC.clear(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private boolean isExcluded(HttpServletRequest req) { |
| 63 | + String path = req.getRequestURI(); |
| 64 | + for (String p : EXCLUDE_PATTERNS) { |
| 65 | + if (PATH_MATCHER.match(p, path)) { |
| 66 | + return true; |
| 67 | + } |
| 68 | + } |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + private String generateTraceId() { |
| 73 | + return java.util.UUID.randomUUID().toString().replace("-", "").substring(0, 16); |
| 74 | + } |
| 75 | + |
| 76 | + private void printRequestUri(HttpServletRequest request) { |
| 77 | + String methodType = request.getMethod(); |
| 78 | + String uri = buildDecodedRequestUri(request); |
| 79 | + log.info("[REQUEST] {} {}", methodType, uri); |
| 80 | + } |
| 81 | + |
| 82 | + private void printResponse( |
| 83 | + HttpServletRequest request, |
| 84 | + HttpServletResponse response |
| 85 | + ) { |
| 86 | + Long userId = (Long) request.getAttribute("userId"); |
| 87 | + String uri = buildDecodedRequestUri(request); |
| 88 | + HttpStatus status = HttpStatus.valueOf(response.getStatus()); |
| 89 | + |
| 90 | + log.info("[RESPONSE] {} userId = {}, ({})", uri, userId, status); |
| 91 | + } |
| 92 | + |
| 93 | + private String buildDecodedRequestUri(HttpServletRequest request) { |
| 94 | + String path = request.getRequestURI(); |
| 95 | + String query = request.getQueryString(); |
| 96 | + |
| 97 | + if(query == null || query.isBlank()){ |
| 98 | + return path; |
| 99 | + } |
| 100 | + |
| 101 | + String decodedQuery = decodeQuery(query); |
| 102 | + String maskedQuery = maskSensitiveParams(decodedQuery); |
| 103 | + |
| 104 | + return path + "?" + maskedQuery; |
| 105 | + } |
| 106 | + |
| 107 | + private String decodeQuery(String rawQuery) { |
| 108 | + if(rawQuery == null || rawQuery.isBlank()){ |
| 109 | + return rawQuery; |
| 110 | + } |
| 111 | + |
| 112 | + try { |
| 113 | + return URLDecoder.decode(rawQuery, StandardCharsets.UTF_8); |
| 114 | + } catch (IllegalArgumentException e) { |
| 115 | + log.warn("Query 디코딩 실패 parameter: {}, msg: {}", rawQuery, e.getMessage()); |
| 116 | + return rawQuery; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private String maskSensitiveParams(String decodedQuery) { |
| 121 | + String[] params = decodedQuery.split("&"); |
| 122 | + StringBuilder maskedQuery = new StringBuilder(); |
| 123 | + |
| 124 | + for(int i = 0; i < params.length; i++){ |
| 125 | + String param = params[i]; |
| 126 | + |
| 127 | + if(!param.contains("=")){ |
| 128 | + maskedQuery.append(param); |
| 129 | + }else{ |
| 130 | + int equalIndex = param.indexOf("="); |
| 131 | + String key = param.substring(0, equalIndex); |
| 132 | + |
| 133 | + if(isSensitiveParam(key)){ |
| 134 | + maskedQuery.append(key).append("=").append(MASK_VALUE); |
| 135 | + }else{ |
| 136 | + maskedQuery.append(param); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + if(i < params.length - 1){ |
| 141 | + maskedQuery.append("&"); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + return maskedQuery.toString(); |
| 146 | + } |
| 147 | + |
| 148 | + private boolean isSensitiveParam(String paramKey) { |
| 149 | + for (String sensitiveParam : EXCLUDE_QUERIES){ |
| 150 | + if(sensitiveParam.equalsIgnoreCase(paramKey)){ |
| 151 | + return true; |
| 152 | + } |
| 153 | + } |
| 154 | + return false; |
| 155 | + } |
| 156 | +} |
0 commit comments