Skip to content

Commit

Permalink
Add username to logs
Browse files Browse the repository at this point in the history
  • Loading branch information
olevitt committed Nov 22, 2024
1 parent 60621f7 commit ead5498
Showing 1 changed file with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit ead5498

Please sign in to comment.