Skip to content

Commit

Permalink
Add username to logs (#526)
Browse files Browse the repository at this point in the history
  • Loading branch information
olevitt authored Dec 12, 2024
1 parent 715cda2 commit d3bd2c8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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 java.io.IOException;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

@Component
public class LogUserInfoFilter extends OncePerRequestFilter {

private UserProvider userProvider;

private RegionsConfiguration regionsConfiguration;

@Autowired
public LogUserInfoFilter(UserProvider userProvider, RegionsConfiguration regionsConfiguration) {
this.userProvider = userProvider;
this.regionsConfiguration = regionsConfiguration;
}

@Override
protected void doFilterInternal(
HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
try {
MDC.put(
"username",
userProvider.getUser(regionsConfiguration.getDefaultRegion()).getIdep());
} catch (Exception ignored) {

}
filterChain.doFilter(request, response);
MDC.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import fr.insee.onyxia.api.configuration.SecurityConfig;
import fr.insee.onyxia.api.configuration.properties.RegionsConfiguration;
import fr.insee.onyxia.api.controller.api.user.UserController;
import fr.insee.onyxia.api.services.UserProvider;
import fr.insee.onyxia.api.services.utils.HttpRequestUtils;
import fr.insee.onyxia.api.user.OnyxiaUserProvider;
import fr.insee.onyxia.model.OnyxiaUser;
Expand Down Expand Up @@ -41,6 +42,8 @@ public class UserControllerTest extends BaseTest {

@MockBean private OnyxiaUserProvider onyxiaUserProvider;

@MockBean private UserProvider userProvider;

@BeforeEach
public void setUp() {
User user = new User();
Expand Down

0 comments on commit d3bd2c8

Please sign in to comment.