|
| 1 | +package ddingdong.ddingdongBE.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.util.List; |
| 9 | +import java.util.UUID; |
| 10 | +import lombok.extern.slf4j.Slf4j; |
| 11 | +import org.slf4j.MDC; |
| 12 | +import org.springframework.stereotype.Component; |
| 13 | +import org.springframework.util.AntPathMatcher; |
| 14 | +import org.springframework.web.filter.OncePerRequestFilter; |
| 15 | + |
| 16 | +@Slf4j |
| 17 | +@Component |
| 18 | +public class HttpLoggingFilter extends OncePerRequestFilter { |
| 19 | + |
| 20 | + private static final List<String> EXCLUDE_URI = List.of( |
| 21 | + "/actuator/**", |
| 22 | + "/swagger-ui/**", |
| 23 | + "/api-docs", |
| 24 | + "/favicon.ico" |
| 25 | + ); |
| 26 | + |
| 27 | + private final AntPathMatcher antPathMatcher = new AntPathMatcher(); |
| 28 | + |
| 29 | + @Override |
| 30 | + protected void doFilterInternal( |
| 31 | + final HttpServletRequest request, |
| 32 | + final HttpServletResponse response, |
| 33 | + final FilterChain filterChain |
| 34 | + ) throws ServletException, IOException { |
| 35 | + |
| 36 | + final String requestURI = request.getRequestURI(); |
| 37 | + if (isExcluded(requestURI)) { |
| 38 | + filterChain.doFilter(request, response); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + final String requestId = createRequestId(); |
| 43 | + final long startTime = System.currentTimeMillis(); |
| 44 | + |
| 45 | + logRequest(request, requestId); |
| 46 | + |
| 47 | + try { |
| 48 | + filterChain.doFilter(request, response); |
| 49 | + } finally { |
| 50 | + final long duration = System.currentTimeMillis() - startTime; |
| 51 | + logResponse(request, response, requestId, duration); |
| 52 | + MDC.remove("request_id"); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private String createRequestId() { |
| 57 | + String requestId = UUID.randomUUID().toString().substring(0, 8); |
| 58 | + MDC.put("request_id", requestId); |
| 59 | + return requestId; |
| 60 | + } |
| 61 | + |
| 62 | + private void logRequest(HttpServletRequest request, String requestId) { |
| 63 | + log.info("[{}] → {} {} | IP: {}", |
| 64 | + requestId, |
| 65 | + request.getMethod(), |
| 66 | + getFullRequestUrl(request), |
| 67 | + getClientIpAddress(request) |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + private void logResponse(HttpServletRequest request, HttpServletResponse response, |
| 72 | + String requestId, long duration) { |
| 73 | + log.info("[{}] ← {} | Status: {} | {}ms", |
| 74 | + requestId, |
| 75 | + request.getMethod(), |
| 76 | + response.getStatus(), |
| 77 | + duration |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + private String getFullRequestUrl(HttpServletRequest request) { |
| 82 | + String queryString = request.getQueryString(); |
| 83 | + return queryString != null ? request.getRequestURI() + "?" + queryString : request.getRequestURI(); |
| 84 | + } |
| 85 | + |
| 86 | + private String getClientIpAddress(HttpServletRequest request) { |
| 87 | + // AWS ALB에서 설정하는 X-Forwarded-For 헤더 확인 |
| 88 | + String xForwardedFor = request.getHeader("X-Forwarded-For"); |
| 89 | + if (xForwardedFor != null && !xForwardedFor.isEmpty()) { |
| 90 | + // 첫 번째 IP가 실제 클라이언트 IP |
| 91 | + return xForwardedFor.split(",")[0].trim(); |
| 92 | + } |
| 93 | + |
| 94 | + // fallback: Remote Address |
| 95 | + return request.getRemoteAddr(); |
| 96 | + } |
| 97 | + |
| 98 | + private boolean isExcluded(String requestURI) { |
| 99 | + return EXCLUDE_URI.stream() |
| 100 | + .anyMatch(pattern -> antPathMatcher.match(pattern, requestURI)); |
| 101 | + } |
| 102 | +} |
0 commit comments