From 0c203e07be80e6954fd27ee86f669a0e6594eaac Mon Sep 17 00:00:00 2001 From: Eunhee Baek <78892355+ehBeak@users.noreply.github.com> Date: Thu, 22 Aug 2024 13:19:34 +0900 Subject: [PATCH 01/10] =?UTF-8?q?refactor:=20IP=20=EC=A3=BC=EC=86=8C=20?= =?UTF-8?q?=EC=A0=95=EB=B3=B4=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/main/java/kr/momo/config/filter/LogGenerator.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/backend/src/main/java/kr/momo/config/filter/LogGenerator.java b/backend/src/main/java/kr/momo/config/filter/LogGenerator.java index fca91f4fe..fd318899b 100644 --- a/backend/src/main/java/kr/momo/config/filter/LogGenerator.java +++ b/backend/src/main/java/kr/momo/config/filter/LogGenerator.java @@ -12,9 +12,8 @@ public class LogGenerator { public void logRequest(String traceId, HttpServletRequest request) { String httpMethod = request.getMethod(); String requestURI = request.getRequestURI(); - String remoteAddr = request.getRemoteAddr(); - log.info("REQUEST [{}][{} {}][{}]", traceId, httpMethod, requestURI, remoteAddr); + log.info("REQUEST [{}][{} {}]", traceId, httpMethod, requestURI); } public void logResponse(String traceId, long duration, HttpServletRequest request, HttpServletResponse response) { From 4942a3ba69aa7821f89767e485975e3acc1e501b Mon Sep 17 00:00:00 2001 From: Eunhee Baek <78892355+ehBeak@users.noreply.github.com> Date: Thu, 22 Aug 2024 15:19:49 +0900 Subject: [PATCH 02/10] =?UTF-8?q?feat:=20=EC=9A=94=EC=B2=AD=EC=97=90=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=EC=9E=90=20=EC=A0=95=EB=B3=B4=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kr/momo/config/filter/LogGenerator.java | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/backend/src/main/java/kr/momo/config/filter/LogGenerator.java b/backend/src/main/java/kr/momo/config/filter/LogGenerator.java index fd318899b..fd1e75f04 100644 --- a/backend/src/main/java/kr/momo/config/filter/LogGenerator.java +++ b/backend/src/main/java/kr/momo/config/filter/LogGenerator.java @@ -1,19 +1,36 @@ package kr.momo.config.filter; +import jakarta.servlet.http.Cookie; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import java.util.Arrays; +import java.util.Optional; +import kr.momo.service.auth.JwtManager; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @Slf4j @Component +@RequiredArgsConstructor public class LogGenerator { + private final JwtManager jwtManager; + + public void logRequest(String traceId, HttpServletRequest request) { String httpMethod = request.getMethod(); String requestURI = request.getRequestURI(); + Cookie[] cookies = request.getCookies(); + String token = getCookieValue(cookies).orElse("all"); + + if (token.equals("all")) { + log.info("REQUEST [{}][{} {}][{}]", traceId, httpMethod, requestURI, token); + return; + } + long userId = jwtManager.extract(token); - log.info("REQUEST [{}][{} {}]", traceId, httpMethod, requestURI); + log.info("REQUEST [{}] [USERID:{}] [{} {}]", traceId, userId, httpMethod, requestURI); } public void logResponse(String traceId, long duration, HttpServletRequest request, HttpServletResponse response) { @@ -23,4 +40,15 @@ public void logResponse(String traceId, long duration, HttpServletRequest reques log.info("RESPONSE [{}][{} {}][{} ms][Status: {}]", traceId, httpMethod, requestURI, duration, status); } + + private Optional getCookieValue(Cookie[] cookies) { + if (cookies == null) { + return Optional.empty(); + } + + return Arrays.stream(cookies) + .filter(cookie -> "ACCESS_TOKEN".equals(cookie.getName())) + .map(Cookie::getValue) + .findFirst(); + } } From 900eb58e4df1931b6c33612e678eb66bd05e3e78 Mon Sep 17 00:00:00 2001 From: Eunhee Baek <78892355+ehBeak@users.noreply.github.com> Date: Thu, 22 Aug 2024 16:58:22 +0900 Subject: [PATCH 03/10] =?UTF-8?q?fix:=20=EC=98=88=EC=99=B8=20=EB=B0=9C?= =?UTF-8?q?=EC=83=9D=EC=8B=9C=20500=EC=9D=91=EB=8B=B5=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/kr/momo/config/WebConfig.java | 8 ++++ .../kr/momo/config/filter/FilterConfig.java | 29 ------------- .../java/kr/momo/config/filter/LogFilter.java | 42 ------------------- .../kr/momo/config/filter/LogGenerator.java | 36 +++++++++------- .../kr/momo/config/filter/LogInterceptor.java | 40 ++++++++++++++++++ .../exception/GlobalExceptionHandler.java | 8 ++-- 6 files changed, 72 insertions(+), 91 deletions(-) delete mode 100644 backend/src/main/java/kr/momo/config/filter/FilterConfig.java delete mode 100644 backend/src/main/java/kr/momo/config/filter/LogFilter.java create mode 100644 backend/src/main/java/kr/momo/config/filter/LogInterceptor.java diff --git a/backend/src/main/java/kr/momo/config/WebConfig.java b/backend/src/main/java/kr/momo/config/WebConfig.java index 35d0d078c..f048a8502 100644 --- a/backend/src/main/java/kr/momo/config/WebConfig.java +++ b/backend/src/main/java/kr/momo/config/WebConfig.java @@ -1,10 +1,12 @@ package kr.momo.config; import java.util.List; +import kr.momo.config.filter.LogInterceptor; import kr.momo.controller.auth.AuthArgumentResolver; import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Configuration; import org.springframework.web.method.support.HandlerMethodArgumentResolver; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration @@ -12,9 +14,15 @@ public class WebConfig implements WebMvcConfigurer { private final AuthArgumentResolver authArgumentResolver; + private final LogInterceptor logInterceptor; @Override public void addArgumentResolvers(List resolvers) { resolvers.add(authArgumentResolver); } + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(logInterceptor).addPathPatterns("/api/v1/**"); + } } diff --git a/backend/src/main/java/kr/momo/config/filter/FilterConfig.java b/backend/src/main/java/kr/momo/config/filter/FilterConfig.java deleted file mode 100644 index fbdec8729..000000000 --- a/backend/src/main/java/kr/momo/config/filter/FilterConfig.java +++ /dev/null @@ -1,29 +0,0 @@ -package kr.momo.config.filter; - -import jakarta.servlet.Filter; -import lombok.RequiredArgsConstructor; -import org.springframework.boot.web.servlet.FilterRegistrationBean; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -@RequiredArgsConstructor -public class FilterConfig { - - private static final String BASE_URL = "/api/v1/*"; - private static final int FIRST_ORDER = 0; - - private final TraceIdGenerator traceIdGenerator; - private final LogGenerator logGenerator; - - @Bean - public FilterRegistrationBean logFilter() { - FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean<>(); - - filterRegistrationBean.setFilter(new LogFilter(traceIdGenerator, logGenerator)); - filterRegistrationBean.addUrlPatterns(BASE_URL); - filterRegistrationBean.setOrder(FIRST_ORDER); - - return filterRegistrationBean; - } -} diff --git a/backend/src/main/java/kr/momo/config/filter/LogFilter.java b/backend/src/main/java/kr/momo/config/filter/LogFilter.java deleted file mode 100644 index e66464b48..000000000 --- a/backend/src/main/java/kr/momo/config/filter/LogFilter.java +++ /dev/null @@ -1,42 +0,0 @@ -package kr.momo.config.filter; - -import jakarta.servlet.Filter; -import jakarta.servlet.FilterChain; -import jakarta.servlet.ServletException; -import jakarta.servlet.ServletRequest; -import jakarta.servlet.ServletResponse; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import java.io.IOException; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.slf4j.MDC; -import org.slf4j.MDC.MDCCloseable; - -@Slf4j -@RequiredArgsConstructor -public class LogFilter implements Filter { - - public static final String TRACE_ID = "traceId"; - - private final TraceIdGenerator traceIdGenerator; - private final LogGenerator logGenerator; - - @Override - public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) - throws IOException, ServletException { - - HttpServletRequest httpRequest = (HttpServletRequest) servletRequest; - HttpServletResponse httpResponse = (HttpServletResponse) servletResponse; - - String traceId = traceIdGenerator.generateShortUuid(); - - try (MDCCloseable ignored = MDC.putCloseable(TRACE_ID, traceId)) { - logGenerator.logRequest(traceId, httpRequest); - long startTime = System.currentTimeMillis(); - filterChain.doFilter(servletRequest, servletResponse); - long duration = System.currentTimeMillis() - startTime; - logGenerator.logResponse(traceId, duration, httpRequest, httpResponse); - } - } -} diff --git a/backend/src/main/java/kr/momo/config/filter/LogGenerator.java b/backend/src/main/java/kr/momo/config/filter/LogGenerator.java index fd1e75f04..039df46bf 100644 --- a/backend/src/main/java/kr/momo/config/filter/LogGenerator.java +++ b/backend/src/main/java/kr/momo/config/filter/LogGenerator.java @@ -15,30 +15,22 @@ @RequiredArgsConstructor public class LogGenerator { - private final JwtManager jwtManager; + private static final String ANONYMOUS = "ANONYMOUS"; + private static final String ACCESS_TOKEN = "ACCESS_TOKEN"; + private final JwtManager jwtManager; public void logRequest(String traceId, HttpServletRequest request) { String httpMethod = request.getMethod(); String requestURI = request.getRequestURI(); Cookie[] cookies = request.getCookies(); - String token = getCookieValue(cookies).orElse("all"); + String userInfo = getCookieValue(cookies).orElse(ANONYMOUS); - if (token.equals("all")) { - log.info("REQUEST [{}][{} {}][{}]", traceId, httpMethod, requestURI, token); - return; + if (isLoginUser(userInfo)) { + userInfo = String.valueOf(jwtManager.extract(userInfo)); } - long userId = jwtManager.extract(token); - - log.info("REQUEST [{}] [USERID:{}] [{} {}]", traceId, userId, httpMethod, requestURI); - } - - public void logResponse(String traceId, long duration, HttpServletRequest request, HttpServletResponse response) { - String httpMethod = request.getMethod(); - String requestURI = request.getRequestURI(); - int status = response.getStatus(); - log.info("RESPONSE [{}][{} {}][{} ms][Status: {}]", traceId, httpMethod, requestURI, duration, status); + log.info("REQUEST [{}][USERID:{}][{} {}]", traceId, userInfo, httpMethod, requestURI); } private Optional getCookieValue(Cookie[] cookies) { @@ -47,8 +39,20 @@ private Optional getCookieValue(Cookie[] cookies) { } return Arrays.stream(cookies) - .filter(cookie -> "ACCESS_TOKEN".equals(cookie.getName())) + .filter(cookie -> ACCESS_TOKEN.equals(cookie.getName())) .map(Cookie::getValue) .findFirst(); } + + private boolean isLoginUser(String token) { + return !ANONYMOUS.equals(token); + } + + public void logResponse(String traceId, long duration, HttpServletRequest request, HttpServletResponse response) { + String httpMethod = request.getMethod(); + String requestURI = request.getRequestURI(); + int status = response.getStatus(); + + log.info("RESPONSE [{}][{} {}][{} ms][Status: {}]", traceId, httpMethod, requestURI, duration, status); + } } diff --git a/backend/src/main/java/kr/momo/config/filter/LogInterceptor.java b/backend/src/main/java/kr/momo/config/filter/LogInterceptor.java new file mode 100644 index 000000000..b2206b8a7 --- /dev/null +++ b/backend/src/main/java/kr/momo/config/filter/LogInterceptor.java @@ -0,0 +1,40 @@ +package kr.momo.config.filter; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.slf4j.MDC; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.HandlerInterceptor; + +@Slf4j +@RequiredArgsConstructor +@Component +public class LogInterceptor implements HandlerInterceptor { + + public static final String TRACE_ID = "traceId"; + + private final TraceIdGenerator traceIdGenerator; + private final LogGenerator logGenerator; + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { + String traceId = traceIdGenerator.generateShortUuid(); + MDC.put(TRACE_ID, traceId); + logGenerator.logRequest(traceId, request); + request.setAttribute("startTime", System.currentTimeMillis()); + return true; + } + + @Override + public void afterCompletion( + HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex + ) { + long startTime = (Long) request.getAttribute("startTime"); + long duration = System.currentTimeMillis() - startTime; + String traceId = MDC.get(TRACE_ID); + logGenerator.logResponse(traceId, duration, request, response); + MDC.remove(TRACE_ID); + } +} diff --git a/backend/src/main/java/kr/momo/exception/GlobalExceptionHandler.java b/backend/src/main/java/kr/momo/exception/GlobalExceptionHandler.java index 88a7d5780..559cea062 100644 --- a/backend/src/main/java/kr/momo/exception/GlobalExceptionHandler.java +++ b/backend/src/main/java/kr/momo/exception/GlobalExceptionHandler.java @@ -1,6 +1,6 @@ package kr.momo.exception; -import kr.momo.config.filter.LogFilter; +import kr.momo.config.filter.LogInterceptor; import lombok.extern.slf4j.Slf4j; import org.slf4j.MDC; import org.springframework.http.HttpStatus; @@ -20,7 +20,7 @@ public class GlobalExceptionHandler { @ExceptionHandler public ProblemDetail handleMomoException(MomoException ex) { - String traceId = MDC.get(LogFilter.TRACE_ID); + String traceId = MDC.get(LogInterceptor.TRACE_ID); log.warn(EXCEPTION_LOG_FORMAT, traceId, ex); ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(ex.httpStatus(), ex.message()); @@ -29,7 +29,7 @@ public ProblemDetail handleMomoException(MomoException ex) { @ExceptionHandler public ProblemDetail handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) { - String traceId = MDC.get(LogFilter.TRACE_ID); + String traceId = MDC.get(LogInterceptor.TRACE_ID); log.warn(EXCEPTION_LOG_FORMAT, traceId, ex); ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail( @@ -40,7 +40,7 @@ public ProblemDetail handleMethodArgumentNotValidException(MethodArgumentNotVali @ExceptionHandler public ProblemDetail handleInternalException(Exception ex) { - String traceId = MDC.get(LogFilter.TRACE_ID); + String traceId = MDC.get(LogInterceptor.TRACE_ID); log.error(EXCEPTION_LOG_FORMAT, traceId, ex); return ProblemDetail.forStatusAndDetail(HttpStatus.INTERNAL_SERVER_ERROR, INTERNAL_SERVER_ERROR_MESSAGE); From 5c2df3996f4a9a13ac2582b36c8bd42d76537cdd Mon Sep 17 00:00:00 2001 From: Eunhee Baek <78892355+ehBeak@users.noreply.github.com> Date: Fri, 23 Aug 2024 01:43:15 +0900 Subject: [PATCH 04/10] =?UTF-8?q?refactor:=20=ED=8C=A8=ED=82=A4=EC=A7=80?= =?UTF-8?q?=20=EC=9D=B4=EB=A6=84=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/main/java/kr/momo/config/WebConfig.java | 2 +- .../kr/momo/config/{filter => interceptor}/LogGenerator.java | 2 +- .../kr/momo/config/{filter => interceptor}/LogInterceptor.java | 2 +- .../momo/config/{filter => interceptor}/TraceIdGenerator.java | 2 +- .../src/main/java/kr/momo/exception/GlobalExceptionHandler.java | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) rename backend/src/main/java/kr/momo/config/{filter => interceptor}/LogGenerator.java (98%) rename backend/src/main/java/kr/momo/config/{filter => interceptor}/LogInterceptor.java (97%) rename backend/src/main/java/kr/momo/config/{filter => interceptor}/TraceIdGenerator.java (86%) diff --git a/backend/src/main/java/kr/momo/config/WebConfig.java b/backend/src/main/java/kr/momo/config/WebConfig.java index f048a8502..f7b6328f8 100644 --- a/backend/src/main/java/kr/momo/config/WebConfig.java +++ b/backend/src/main/java/kr/momo/config/WebConfig.java @@ -1,7 +1,7 @@ package kr.momo.config; import java.util.List; -import kr.momo.config.filter.LogInterceptor; +import kr.momo.config.interceptor.LogInterceptor; import kr.momo.controller.auth.AuthArgumentResolver; import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Configuration; diff --git a/backend/src/main/java/kr/momo/config/filter/LogGenerator.java b/backend/src/main/java/kr/momo/config/interceptor/LogGenerator.java similarity index 98% rename from backend/src/main/java/kr/momo/config/filter/LogGenerator.java rename to backend/src/main/java/kr/momo/config/interceptor/LogGenerator.java index 039df46bf..76244ce54 100644 --- a/backend/src/main/java/kr/momo/config/filter/LogGenerator.java +++ b/backend/src/main/java/kr/momo/config/interceptor/LogGenerator.java @@ -1,4 +1,4 @@ -package kr.momo.config.filter; +package kr.momo.config.interceptor; import jakarta.servlet.http.Cookie; import jakarta.servlet.http.HttpServletRequest; diff --git a/backend/src/main/java/kr/momo/config/filter/LogInterceptor.java b/backend/src/main/java/kr/momo/config/interceptor/LogInterceptor.java similarity index 97% rename from backend/src/main/java/kr/momo/config/filter/LogInterceptor.java rename to backend/src/main/java/kr/momo/config/interceptor/LogInterceptor.java index b2206b8a7..85016caff 100644 --- a/backend/src/main/java/kr/momo/config/filter/LogInterceptor.java +++ b/backend/src/main/java/kr/momo/config/interceptor/LogInterceptor.java @@ -1,4 +1,4 @@ -package kr.momo.config.filter; +package kr.momo.config.interceptor; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; diff --git a/backend/src/main/java/kr/momo/config/filter/TraceIdGenerator.java b/backend/src/main/java/kr/momo/config/interceptor/TraceIdGenerator.java similarity index 86% rename from backend/src/main/java/kr/momo/config/filter/TraceIdGenerator.java rename to backend/src/main/java/kr/momo/config/interceptor/TraceIdGenerator.java index 48cf5494d..9426b67a3 100644 --- a/backend/src/main/java/kr/momo/config/filter/TraceIdGenerator.java +++ b/backend/src/main/java/kr/momo/config/interceptor/TraceIdGenerator.java @@ -1,4 +1,4 @@ -package kr.momo.config.filter; +package kr.momo.config.interceptor; import java.util.UUID; import org.springframework.stereotype.Component; diff --git a/backend/src/main/java/kr/momo/exception/GlobalExceptionHandler.java b/backend/src/main/java/kr/momo/exception/GlobalExceptionHandler.java index 559cea062..c0c519713 100644 --- a/backend/src/main/java/kr/momo/exception/GlobalExceptionHandler.java +++ b/backend/src/main/java/kr/momo/exception/GlobalExceptionHandler.java @@ -1,6 +1,6 @@ package kr.momo.exception; -import kr.momo.config.filter.LogInterceptor; +import kr.momo.config.interceptor.LogInterceptor; import lombok.extern.slf4j.Slf4j; import org.slf4j.MDC; import org.springframework.http.HttpStatus; From a6702fdc76fa35f87cb6a122ed9f562c9353eda0 Mon Sep 17 00:00:00 2001 From: Eunhee Baek <78892355+ehBeak@users.noreply.github.com> Date: Fri, 23 Aug 2024 03:17:18 +0900 Subject: [PATCH 05/10] =?UTF-8?q?refactor:=20=EC=97=AD=ED=95=A0=EC=97=90?= =?UTF-8?q?=20=EB=94=B0=EB=9D=BC=20=EC=9D=B8=ED=84=B0=EC=85=89=ED=84=B0=20?= =?UTF-8?q?=EB=B6=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/kr/momo/config/WebConfig.java | 11 +++-- .../momo/config/interceptor/LogGenerator.java | 31 +----------- ...terceptor.java => LoggingInterceptor.java} | 7 +-- .../interceptor/UserInfoInterceptor.java | 48 +++++++++++++++++++ .../exception/GlobalExceptionHandler.java | 8 ++-- 5 files changed, 65 insertions(+), 40 deletions(-) rename backend/src/main/java/kr/momo/config/interceptor/{LogInterceptor.java => LoggingInterceptor.java} (82%) create mode 100644 backend/src/main/java/kr/momo/config/interceptor/UserInfoInterceptor.java diff --git a/backend/src/main/java/kr/momo/config/WebConfig.java b/backend/src/main/java/kr/momo/config/WebConfig.java index f7b6328f8..8ad168b1a 100644 --- a/backend/src/main/java/kr/momo/config/WebConfig.java +++ b/backend/src/main/java/kr/momo/config/WebConfig.java @@ -1,7 +1,8 @@ package kr.momo.config; import java.util.List; -import kr.momo.config.interceptor.LogInterceptor; +import kr.momo.config.interceptor.UserInfoInterceptor; +import kr.momo.config.interceptor.LoggingInterceptor; import kr.momo.controller.auth.AuthArgumentResolver; import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Configuration; @@ -13,8 +14,11 @@ @RequiredArgsConstructor public class WebConfig implements WebMvcConfigurer { + private static final String BASE_URL = "/api/v1/**"; + private final AuthArgumentResolver authArgumentResolver; - private final LogInterceptor logInterceptor; + private final UserInfoInterceptor userInfoInterceptor; + private final LoggingInterceptor loggingInterceptor; @Override public void addArgumentResolvers(List resolvers) { @@ -23,6 +27,7 @@ public void addArgumentResolvers(List resolvers) @Override public void addInterceptors(InterceptorRegistry registry) { - registry.addInterceptor(logInterceptor).addPathPatterns("/api/v1/**"); + registry.addInterceptor(userInfoInterceptor).addPathPatterns(BASE_URL); + registry.addInterceptor(loggingInterceptor).addPathPatterns(BASE_URL); } } diff --git a/backend/src/main/java/kr/momo/config/interceptor/LogGenerator.java b/backend/src/main/java/kr/momo/config/interceptor/LogGenerator.java index 76244ce54..5e3bc5378 100644 --- a/backend/src/main/java/kr/momo/config/interceptor/LogGenerator.java +++ b/backend/src/main/java/kr/momo/config/interceptor/LogGenerator.java @@ -1,11 +1,7 @@ package kr.momo.config.interceptor; -import jakarta.servlet.http.Cookie; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import java.util.Arrays; -import java.util.Optional; -import kr.momo.service.auth.JwtManager; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @@ -15,39 +11,14 @@ @RequiredArgsConstructor public class LogGenerator { - private static final String ANONYMOUS = "ANONYMOUS"; - private static final String ACCESS_TOKEN = "ACCESS_TOKEN"; - - private final JwtManager jwtManager; - public void logRequest(String traceId, HttpServletRequest request) { String httpMethod = request.getMethod(); String requestURI = request.getRequestURI(); - Cookie[] cookies = request.getCookies(); - String userInfo = getCookieValue(cookies).orElse(ANONYMOUS); - - if (isLoginUser(userInfo)) { - userInfo = String.valueOf(jwtManager.extract(userInfo)); - } + String userInfo = request.getAttribute("userInfo").toString(); log.info("REQUEST [{}][USERID:{}][{} {}]", traceId, userInfo, httpMethod, requestURI); } - private Optional getCookieValue(Cookie[] cookies) { - if (cookies == null) { - return Optional.empty(); - } - - return Arrays.stream(cookies) - .filter(cookie -> ACCESS_TOKEN.equals(cookie.getName())) - .map(Cookie::getValue) - .findFirst(); - } - - private boolean isLoginUser(String token) { - return !ANONYMOUS.equals(token); - } - public void logResponse(String traceId, long duration, HttpServletRequest request, HttpServletResponse response) { String httpMethod = request.getMethod(); String requestURI = request.getRequestURI(); diff --git a/backend/src/main/java/kr/momo/config/interceptor/LogInterceptor.java b/backend/src/main/java/kr/momo/config/interceptor/LoggingInterceptor.java similarity index 82% rename from backend/src/main/java/kr/momo/config/interceptor/LogInterceptor.java rename to backend/src/main/java/kr/momo/config/interceptor/LoggingInterceptor.java index 85016caff..17f9e73c7 100644 --- a/backend/src/main/java/kr/momo/config/interceptor/LogInterceptor.java +++ b/backend/src/main/java/kr/momo/config/interceptor/LoggingInterceptor.java @@ -11,9 +11,10 @@ @Slf4j @RequiredArgsConstructor @Component -public class LogInterceptor implements HandlerInterceptor { +public class LoggingInterceptor implements HandlerInterceptor { public static final String TRACE_ID = "traceId"; + private static final String START_TIME = "startTime"; private final TraceIdGenerator traceIdGenerator; private final LogGenerator logGenerator; @@ -23,7 +24,7 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons String traceId = traceIdGenerator.generateShortUuid(); MDC.put(TRACE_ID, traceId); logGenerator.logRequest(traceId, request); - request.setAttribute("startTime", System.currentTimeMillis()); + request.setAttribute(START_TIME, System.currentTimeMillis()); return true; } @@ -31,7 +32,7 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons public void afterCompletion( HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex ) { - long startTime = (Long) request.getAttribute("startTime"); + long startTime = (Long) request.getAttribute(START_TIME); long duration = System.currentTimeMillis() - startTime; String traceId = MDC.get(TRACE_ID); logGenerator.logResponse(traceId, duration, request, response); diff --git a/backend/src/main/java/kr/momo/config/interceptor/UserInfoInterceptor.java b/backend/src/main/java/kr/momo/config/interceptor/UserInfoInterceptor.java new file mode 100644 index 000000000..44b1511af --- /dev/null +++ b/backend/src/main/java/kr/momo/config/interceptor/UserInfoInterceptor.java @@ -0,0 +1,48 @@ +package kr.momo.config.interceptor; + +import jakarta.servlet.http.Cookie; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.util.Arrays; +import java.util.Optional; +import kr.momo.service.auth.JwtManager; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.HandlerInterceptor; + +@Component +@RequiredArgsConstructor +public class UserInfoInterceptor implements HandlerInterceptor { + + private static final String ANONYMOUS = "ANONYMOUS"; + private static final String ACCESS_TOKEN = "ACCESS_TOKEN"; + + private final JwtManager jwtManager; + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { + Cookie[] cookies = request.getCookies(); + String userInfo = getCookieValue(cookies).orElse(ANONYMOUS); + if (isLoginUser(userInfo)) { + userInfo = String.valueOf(jwtManager.extract(userInfo)); + } + + request.setAttribute("userInfo", userInfo); + return true; + } + + private Optional getCookieValue(Cookie[] cookies) { + if (cookies == null) { + return Optional.empty(); + } + + return Arrays.stream(cookies) + .filter(cookie -> ACCESS_TOKEN.equals(cookie.getName())) + .map(Cookie::getValue) + .findFirst(); + } + + private boolean isLoginUser(String token) { + return !ANONYMOUS.equals(token); + } +} diff --git a/backend/src/main/java/kr/momo/exception/GlobalExceptionHandler.java b/backend/src/main/java/kr/momo/exception/GlobalExceptionHandler.java index c0c519713..2ee59d6d5 100644 --- a/backend/src/main/java/kr/momo/exception/GlobalExceptionHandler.java +++ b/backend/src/main/java/kr/momo/exception/GlobalExceptionHandler.java @@ -1,6 +1,6 @@ package kr.momo.exception; -import kr.momo.config.interceptor.LogInterceptor; +import kr.momo.config.interceptor.LoggingInterceptor; import lombok.extern.slf4j.Slf4j; import org.slf4j.MDC; import org.springframework.http.HttpStatus; @@ -20,7 +20,7 @@ public class GlobalExceptionHandler { @ExceptionHandler public ProblemDetail handleMomoException(MomoException ex) { - String traceId = MDC.get(LogInterceptor.TRACE_ID); + String traceId = MDC.get(LoggingInterceptor.TRACE_ID); log.warn(EXCEPTION_LOG_FORMAT, traceId, ex); ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(ex.httpStatus(), ex.message()); @@ -29,7 +29,7 @@ public ProblemDetail handleMomoException(MomoException ex) { @ExceptionHandler public ProblemDetail handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) { - String traceId = MDC.get(LogInterceptor.TRACE_ID); + String traceId = MDC.get(LoggingInterceptor.TRACE_ID); log.warn(EXCEPTION_LOG_FORMAT, traceId, ex); ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail( @@ -40,7 +40,7 @@ public ProblemDetail handleMethodArgumentNotValidException(MethodArgumentNotVali @ExceptionHandler public ProblemDetail handleInternalException(Exception ex) { - String traceId = MDC.get(LogInterceptor.TRACE_ID); + String traceId = MDC.get(LoggingInterceptor.TRACE_ID); log.error(EXCEPTION_LOG_FORMAT, traceId, ex); return ProblemDetail.forStatusAndDetail(HttpStatus.INTERNAL_SERVER_ERROR, INTERNAL_SERVER_ERROR_MESSAGE); From 98dea92a0ec0eb089b727c6d40c118e039454e3a Mon Sep 17 00:00:00 2001 From: Eunhee Baek <78892355+ehBeak@users.noreply.github.com> Date: Fri, 23 Aug 2024 03:29:39 +0900 Subject: [PATCH 06/10] =?UTF-8?q?refactor:=20userInfo=20=EC=83=81=EC=88=98?= =?UTF-8?q?=EB=A1=9C=20=EC=B6=94=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/kr/momo/config/interceptor/LogGenerator.java | 2 +- .../java/kr/momo/config/interceptor/UserInfoInterceptor.java | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/backend/src/main/java/kr/momo/config/interceptor/LogGenerator.java b/backend/src/main/java/kr/momo/config/interceptor/LogGenerator.java index 5e3bc5378..f68beffa6 100644 --- a/backend/src/main/java/kr/momo/config/interceptor/LogGenerator.java +++ b/backend/src/main/java/kr/momo/config/interceptor/LogGenerator.java @@ -14,7 +14,7 @@ public class LogGenerator { public void logRequest(String traceId, HttpServletRequest request) { String httpMethod = request.getMethod(); String requestURI = request.getRequestURI(); - String userInfo = request.getAttribute("userInfo").toString(); + String userInfo = request.getAttribute(UserInfoInterceptor.USER_INFO).toString(); log.info("REQUEST [{}][USERID:{}][{} {}]", traceId, userInfo, httpMethod, requestURI); } diff --git a/backend/src/main/java/kr/momo/config/interceptor/UserInfoInterceptor.java b/backend/src/main/java/kr/momo/config/interceptor/UserInfoInterceptor.java index 44b1511af..46a7b61b7 100644 --- a/backend/src/main/java/kr/momo/config/interceptor/UserInfoInterceptor.java +++ b/backend/src/main/java/kr/momo/config/interceptor/UserInfoInterceptor.java @@ -14,6 +14,8 @@ @RequiredArgsConstructor public class UserInfoInterceptor implements HandlerInterceptor { + public static final String USER_INFO = "userInfo"; + private static final String ANONYMOUS = "ANONYMOUS"; private static final String ACCESS_TOKEN = "ACCESS_TOKEN"; @@ -27,7 +29,7 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons userInfo = String.valueOf(jwtManager.extract(userInfo)); } - request.setAttribute("userInfo", userInfo); + request.setAttribute(USER_INFO, userInfo); return true; } From b6beff9440db42573223d97af97a200456ed7847 Mon Sep 17 00:00:00 2001 From: Eunhee Baek <78892355+ehBeak@users.noreply.github.com> Date: Fri, 23 Aug 2024 07:11:28 +0900 Subject: [PATCH 07/10] =?UTF-8?q?refactor:=20=EC=9D=B8=ED=84=B0=EC=85=89?= =?UTF-8?q?=ED=84=B0=20=EB=93=B1=EB=A1=9D=20=EC=88=9C=EC=84=9C=20=EB=AA=85?= =?UTF-8?q?=EC=8B=9C=EC=A0=81=EC=9C=BC=EB=A1=9C=20=EC=A7=80=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/main/java/kr/momo/config/WebConfig.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/src/main/java/kr/momo/config/WebConfig.java b/backend/src/main/java/kr/momo/config/WebConfig.java index 8ad168b1a..f3ff33257 100644 --- a/backend/src/main/java/kr/momo/config/WebConfig.java +++ b/backend/src/main/java/kr/momo/config/WebConfig.java @@ -27,7 +27,7 @@ public void addArgumentResolvers(List resolvers) @Override public void addInterceptors(InterceptorRegistry registry) { - registry.addInterceptor(userInfoInterceptor).addPathPatterns(BASE_URL); - registry.addInterceptor(loggingInterceptor).addPathPatterns(BASE_URL); + registry.addInterceptor(userInfoInterceptor).addPathPatterns(BASE_URL).order(1); + registry.addInterceptor(loggingInterceptor).addPathPatterns(BASE_URL).order(2); } } From ccb548920d184184746ae3a79b51a7b2c3c91124 Mon Sep 17 00:00:00 2001 From: Eunhee Baek <78892355+ehBeak@users.noreply.github.com> Date: Fri, 23 Aug 2024 07:12:57 +0900 Subject: [PATCH 08/10] =?UTF-8?q?refactor:=20=EC=9D=B8=ED=84=B0=EC=85=89?= =?UTF-8?q?=ED=84=B0=20=EC=9D=B4=EB=A6=84=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/main/java/kr/momo/config/WebConfig.java | 6 +++--- .../{UserInfoInterceptor.java => JwtInterceptor.java} | 2 +- .../main/java/kr/momo/config/interceptor/LogGenerator.java | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) rename backend/src/main/java/kr/momo/config/interceptor/{UserInfoInterceptor.java => JwtInterceptor.java} (95%) diff --git a/backend/src/main/java/kr/momo/config/WebConfig.java b/backend/src/main/java/kr/momo/config/WebConfig.java index f3ff33257..df00fa4b1 100644 --- a/backend/src/main/java/kr/momo/config/WebConfig.java +++ b/backend/src/main/java/kr/momo/config/WebConfig.java @@ -1,7 +1,7 @@ package kr.momo.config; import java.util.List; -import kr.momo.config.interceptor.UserInfoInterceptor; +import kr.momo.config.interceptor.JwtInterceptor; import kr.momo.config.interceptor.LoggingInterceptor; import kr.momo.controller.auth.AuthArgumentResolver; import lombok.RequiredArgsConstructor; @@ -17,7 +17,7 @@ public class WebConfig implements WebMvcConfigurer { private static final String BASE_URL = "/api/v1/**"; private final AuthArgumentResolver authArgumentResolver; - private final UserInfoInterceptor userInfoInterceptor; + private final JwtInterceptor jwtInterceptor; private final LoggingInterceptor loggingInterceptor; @Override @@ -27,7 +27,7 @@ public void addArgumentResolvers(List resolvers) @Override public void addInterceptors(InterceptorRegistry registry) { - registry.addInterceptor(userInfoInterceptor).addPathPatterns(BASE_URL).order(1); + registry.addInterceptor(jwtInterceptor).addPathPatterns(BASE_URL).order(1); registry.addInterceptor(loggingInterceptor).addPathPatterns(BASE_URL).order(2); } } diff --git a/backend/src/main/java/kr/momo/config/interceptor/UserInfoInterceptor.java b/backend/src/main/java/kr/momo/config/interceptor/JwtInterceptor.java similarity index 95% rename from backend/src/main/java/kr/momo/config/interceptor/UserInfoInterceptor.java rename to backend/src/main/java/kr/momo/config/interceptor/JwtInterceptor.java index 46a7b61b7..4c86609fb 100644 --- a/backend/src/main/java/kr/momo/config/interceptor/UserInfoInterceptor.java +++ b/backend/src/main/java/kr/momo/config/interceptor/JwtInterceptor.java @@ -12,7 +12,7 @@ @Component @RequiredArgsConstructor -public class UserInfoInterceptor implements HandlerInterceptor { +public class JwtInterceptor implements HandlerInterceptor { public static final String USER_INFO = "userInfo"; diff --git a/backend/src/main/java/kr/momo/config/interceptor/LogGenerator.java b/backend/src/main/java/kr/momo/config/interceptor/LogGenerator.java index f68beffa6..e387bfee8 100644 --- a/backend/src/main/java/kr/momo/config/interceptor/LogGenerator.java +++ b/backend/src/main/java/kr/momo/config/interceptor/LogGenerator.java @@ -14,7 +14,7 @@ public class LogGenerator { public void logRequest(String traceId, HttpServletRequest request) { String httpMethod = request.getMethod(); String requestURI = request.getRequestURI(); - String userInfo = request.getAttribute(UserInfoInterceptor.USER_INFO).toString(); + String userInfo = request.getAttribute(JwtInterceptor.USER_INFO).toString(); log.info("REQUEST [{}][USERID:{}][{} {}]", traceId, userInfo, httpMethod, requestURI); } From 539d9c3ae77afa21b2378c781efcbea37fc70bed Mon Sep 17 00:00:00 2001 From: Eunhee Baek <78892355+ehBeak@users.noreply.github.com> Date: Fri, 23 Aug 2024 07:16:40 +0900 Subject: [PATCH 09/10] =?UTF-8?q?refactor:=20=EC=9D=91=EB=8B=B5=20?= =?UTF-8?q?=EB=A1=9C=EA=B7=B8=EC=97=90=EB=8F=84=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=EC=9E=90=20=EC=A0=95=EB=B3=B4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/kr/momo/config/interceptor/LogGenerator.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/backend/src/main/java/kr/momo/config/interceptor/LogGenerator.java b/backend/src/main/java/kr/momo/config/interceptor/LogGenerator.java index e387bfee8..f38371829 100644 --- a/backend/src/main/java/kr/momo/config/interceptor/LogGenerator.java +++ b/backend/src/main/java/kr/momo/config/interceptor/LogGenerator.java @@ -22,8 +22,17 @@ public void logRequest(String traceId, HttpServletRequest request) { public void logResponse(String traceId, long duration, HttpServletRequest request, HttpServletResponse response) { String httpMethod = request.getMethod(); String requestURI = request.getRequestURI(); + String userInfo = request.getAttribute(JwtInterceptor.USER_INFO).toString(); int status = response.getStatus(); - log.info("RESPONSE [{}][{} {}][{} ms][Status: {}]", traceId, httpMethod, requestURI, duration, status); + log.info( + "RESPONSE [{}][USERID:{}][{} {}][{} ms][Status: {}]", + traceId, + userInfo, + httpMethod, + requestURI, + duration, + status + ); } } From 3f23c4864bc47aeda5acd0960f2ea62f910ced08 Mon Sep 17 00:00:00 2001 From: Eunhee Baek <78892355+ehBeak@users.noreply.github.com> Date: Fri, 23 Aug 2024 07:34:54 +0900 Subject: [PATCH 10/10] =?UTF-8?q?refactor:=20MDC=EB=A5=BC=20=ED=86=B5?= =?UTF-8?q?=ED=95=B4=EC=84=9C=20=EC=9C=A0=EC=A0=80=20=EC=A0=95=EB=B3=B4?= =?UTF-8?q?=EB=A5=BC=20=EA=B0=80=EC=A0=B8=EC=98=A4=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/kr/momo/config/interceptor/JwtInterceptor.java | 3 ++- .../main/java/kr/momo/config/interceptor/LogGenerator.java | 5 +++-- .../java/kr/momo/config/interceptor/LoggingInterceptor.java | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/backend/src/main/java/kr/momo/config/interceptor/JwtInterceptor.java b/backend/src/main/java/kr/momo/config/interceptor/JwtInterceptor.java index 4c86609fb..034a3e040 100644 --- a/backend/src/main/java/kr/momo/config/interceptor/JwtInterceptor.java +++ b/backend/src/main/java/kr/momo/config/interceptor/JwtInterceptor.java @@ -7,6 +7,7 @@ import java.util.Optional; import kr.momo.service.auth.JwtManager; import lombok.RequiredArgsConstructor; +import org.slf4j.MDC; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; @@ -28,8 +29,8 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons if (isLoginUser(userInfo)) { userInfo = String.valueOf(jwtManager.extract(userInfo)); } + MDC.put(USER_INFO, userInfo); - request.setAttribute(USER_INFO, userInfo); return true; } diff --git a/backend/src/main/java/kr/momo/config/interceptor/LogGenerator.java b/backend/src/main/java/kr/momo/config/interceptor/LogGenerator.java index f38371829..e3bfbad6f 100644 --- a/backend/src/main/java/kr/momo/config/interceptor/LogGenerator.java +++ b/backend/src/main/java/kr/momo/config/interceptor/LogGenerator.java @@ -4,6 +4,7 @@ import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.slf4j.MDC; import org.springframework.stereotype.Component; @Slf4j @@ -14,7 +15,7 @@ public class LogGenerator { public void logRequest(String traceId, HttpServletRequest request) { String httpMethod = request.getMethod(); String requestURI = request.getRequestURI(); - String userInfo = request.getAttribute(JwtInterceptor.USER_INFO).toString(); + String userInfo = MDC.get(JwtInterceptor.USER_INFO); log.info("REQUEST [{}][USERID:{}][{} {}]", traceId, userInfo, httpMethod, requestURI); } @@ -22,7 +23,7 @@ public void logRequest(String traceId, HttpServletRequest request) { public void logResponse(String traceId, long duration, HttpServletRequest request, HttpServletResponse response) { String httpMethod = request.getMethod(); String requestURI = request.getRequestURI(); - String userInfo = request.getAttribute(JwtInterceptor.USER_INFO).toString(); + String userInfo = MDC.get(JwtInterceptor.USER_INFO); int status = response.getStatus(); log.info( diff --git a/backend/src/main/java/kr/momo/config/interceptor/LoggingInterceptor.java b/backend/src/main/java/kr/momo/config/interceptor/LoggingInterceptor.java index 17f9e73c7..63283c378 100644 --- a/backend/src/main/java/kr/momo/config/interceptor/LoggingInterceptor.java +++ b/backend/src/main/java/kr/momo/config/interceptor/LoggingInterceptor.java @@ -36,6 +36,6 @@ public void afterCompletion( long duration = System.currentTimeMillis() - startTime; String traceId = MDC.get(TRACE_ID); logGenerator.logResponse(traceId, duration, request, response); - MDC.remove(TRACE_ID); + MDC.clear(); } }