content: package br.com.alevhvm.adotai.common.config;
  
  import java.io.IOException;
  import java.util.UUID;
  
  import org.slf4j.MDC;
  import org.springframework.security.authentication.AnonymousAuthenticationToken;
  import org.springframework.security.core.Authentication;
  import org.springframework.security.core.context.SecurityContextHolder;
  import org.springframework.stereotype.Component;
  import org.springframework.web.filter.OncePerRequestFilter;
  
  import jakarta.servlet.FilterChain;
  import jakarta.servlet.ServletException;
  import jakarta.servlet.http.HttpServletRequest;
  import jakarta.servlet.http.HttpServletResponse;
  
  @Component
  public class MDCFilter extends OncePerRequestFilter{
  
      @Override
      protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
              throws ServletException, IOException {
          
          String requestId = UUID.randomUUID().toString();
          MDC.put("requestId", requestId);
  
          Authentication auth = SecurityContextHolder.getContext().getAuthentication();
          String username = "anonymous";
          if(auth != null && auth.isAuthenticated() && !(auth instanceof AnonymousAuthenticationToken)) {
              username = auth.getName();
          }
          MDC.put("username", username);
  
          try {
              filterChain.doFilter(request, response);
          } finally {
              MDC.clear();
          }
      }
      
  }
  