From f0287bb5eb1add31876b7075915fd6a020fcd14a Mon Sep 17 00:00:00 2001 From: Matthias Lindinger Date: Thu, 4 Jul 2024 08:45:17 +0200 Subject: [PATCH] Fix clearing of request id --- .../interceptors/MdcLoggingInterceptor.java | 20 +++++++++++++++++++ .../RequestIdLoggingInterceptor.java | 10 ---------- 2 files changed, 20 insertions(+), 10 deletions(-) create mode 100644 metasvc-server/webapp/src/main/java/io/github/dbmdz/metadata/server/interceptors/MdcLoggingInterceptor.java diff --git a/metasvc-server/webapp/src/main/java/io/github/dbmdz/metadata/server/interceptors/MdcLoggingInterceptor.java b/metasvc-server/webapp/src/main/java/io/github/dbmdz/metadata/server/interceptors/MdcLoggingInterceptor.java new file mode 100644 index 000000000..0e49417d3 --- /dev/null +++ b/metasvc-server/webapp/src/main/java/io/github/dbmdz/metadata/server/interceptors/MdcLoggingInterceptor.java @@ -0,0 +1,20 @@ +package io.github.dbmdz.metadata.server.interceptors; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.slf4j.MDC; +import org.springframework.lang.NonNull; +import org.springframework.web.servlet.HandlerInterceptor; + +public class MdcLoggingInterceptor implements HandlerInterceptor { + /** Clear MDC to avoid data leaking between two requests handled by the same thread. */ + @Override + public void afterCompletion( + @NonNull HttpServletRequest request, + @NonNull HttpServletResponse response, + @NonNull Object handler, + Exception ex) + throws Exception { + MDC.clear(); + } +} diff --git a/metasvc-server/webapp/src/main/java/io/github/dbmdz/metadata/server/interceptors/RequestIdLoggingInterceptor.java b/metasvc-server/webapp/src/main/java/io/github/dbmdz/metadata/server/interceptors/RequestIdLoggingInterceptor.java index 972aa7bb7..eec481262 100644 --- a/metasvc-server/webapp/src/main/java/io/github/dbmdz/metadata/server/interceptors/RequestIdLoggingInterceptor.java +++ b/metasvc-server/webapp/src/main/java/io/github/dbmdz/metadata/server/interceptors/RequestIdLoggingInterceptor.java @@ -7,18 +7,8 @@ import org.springframework.lang.NonNull; import org.springframework.util.StringUtils; import org.springframework.web.servlet.HandlerInterceptor; -import org.springframework.web.servlet.ModelAndView; public class RequestIdLoggingInterceptor implements HandlerInterceptor { - /** Clear MDC to avoid data leaking between two requests handled by the same thread. */ - @Override - public void postHandle( - @NonNull HttpServletRequest request, - @NonNull HttpServletResponse response, - @NonNull Object handler, - ModelAndView modelAndView) { - MDC.clear(); - } /** * Register the request identifier (if received from client/frontend server) in the logging