content: package mflix.api.security;
  
  import mflix.api.services.TokenAuthenticationService;
  import org.springframework.security.core.Authentication;
  import org.springframework.security.core.context.SecurityContextHolder;
  import org.springframework.web.filter.OncePerRequestFilter;
  
  import javax.servlet.FilterChain;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import java.io.IOException;
  
  public class JWTAuthenticationFilter extends OncePerRequestFilter {
  
    private TokenAuthenticationService authService;
  
    @Override
    protected void doFilterInternal(
        HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
        throws ServletException, IOException {
      Authentication authentication = authService.getAuthentication(request);
  
      SecurityContextHolder.getContext().setAuthentication(authentication);
      filterChain.doFilter(request, response);
    }
  
    public void setAuthService(TokenAuthenticationService authService) {
      this.authService = authService;
    }
  }
  