From ead54980df36fa8bf0a176c66a20ab844a8c9c71 Mon Sep 17 00:00:00 2001 From: Olivier Levitt Date: Fri, 22 Nov 2024 11:10:37 +0100 Subject: [PATCH] Add username to logs --- .../api/security/LogUserInfoFilter.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 onyxia-api/src/main/java/fr/insee/onyxia/api/security/LogUserInfoFilter.java diff --git a/onyxia-api/src/main/java/fr/insee/onyxia/api/security/LogUserInfoFilter.java b/onyxia-api/src/main/java/fr/insee/onyxia/api/security/LogUserInfoFilter.java new file mode 100644 index 00000000..381cefb3 --- /dev/null +++ b/onyxia-api/src/main/java/fr/insee/onyxia/api/security/LogUserInfoFilter.java @@ -0,0 +1,32 @@ +package fr.insee.onyxia.api.security; + +import fr.insee.onyxia.api.configuration.properties.RegionsConfiguration; +import fr.insee.onyxia.api.services.UserProvider; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.slf4j.MDC; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.filter.OncePerRequestFilter; + +import java.io.IOException; + +@Component +public class LogUserInfoFilter extends OncePerRequestFilter { + + @Autowired + private UserProvider userProvider; + + @Autowired + private RegionsConfiguration regionsConfiguration; + + @Override + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, + FilterChain filterChain) throws ServletException, IOException { + MDC.put("username", userProvider.getUser(regionsConfiguration.getDefaultRegion()).getIdep()); + filterChain.doFilter(request, response); + MDC.clear(); + } +} \ No newline at end of file