|
| 1 | +package fr.insee.onyxia.api.security; |
| 2 | + |
| 3 | +import fr.insee.onyxia.api.configuration.properties.RegionsConfiguration; |
| 4 | +import fr.insee.onyxia.api.services.UserProvider; |
| 5 | +import jakarta.servlet.FilterChain; |
| 6 | +import jakarta.servlet.ServletException; |
| 7 | +import jakarta.servlet.http.HttpServletRequest; |
| 8 | +import jakarta.servlet.http.HttpServletResponse; |
| 9 | +import java.io.IOException; |
| 10 | +import org.slf4j.MDC; |
| 11 | +import org.springframework.beans.factory.annotation.Autowired; |
| 12 | +import org.springframework.stereotype.Component; |
| 13 | +import org.springframework.web.filter.OncePerRequestFilter; |
| 14 | + |
| 15 | +@Component |
| 16 | +public class LogUserInfoFilter extends OncePerRequestFilter { |
| 17 | + |
| 18 | + private UserProvider userProvider; |
| 19 | + |
| 20 | + private RegionsConfiguration regionsConfiguration; |
| 21 | + |
| 22 | + @Autowired |
| 23 | + public LogUserInfoFilter(UserProvider userProvider, RegionsConfiguration regionsConfiguration) { |
| 24 | + this.userProvider = userProvider; |
| 25 | + this.regionsConfiguration = regionsConfiguration; |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + protected void doFilterInternal( |
| 30 | + HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) |
| 31 | + throws ServletException, IOException { |
| 32 | + try { |
| 33 | + MDC.put( |
| 34 | + "username", |
| 35 | + userProvider.getUser(regionsConfiguration.getDefaultRegion()).getIdep()); |
| 36 | + } catch (Exception ignored) { |
| 37 | + |
| 38 | + } |
| 39 | + filterChain.doFilter(request, response); |
| 40 | + MDC.clear(); |
| 41 | + } |
| 42 | +} |
0 commit comments