Class JwtAuthTokenFilter

All Implemented Interfaces:
jakarta.servlet.Filter, Aware, BeanNameAware, DisposableBean, InitializingBean, EnvironmentAware, EnvironmentCapable, ServletContextAware

public class JwtAuthTokenFilter extends OncePerRequestFilter
JWT Authentication Filter for validating tokens in requests.

This filter intercepts incoming HTTP requests to validate JWT tokens present in the request headers. If a valid token is found, it sets up the Spring Security context with an authenticated user. This enables stateless authentication for RESTful API endpoints.

The filter performs the following operations:

  • Extracts the JWT token from the Authorization header
  • Validates the token using JwtUtils
  • Loads user details based on the username from the token
  • Creates an authentication object and sets it in the security context

This class extends Spring's OncePerRequestFilter to ensure the filter is executed only once per request. It is part of the security configuration and is added to the filter chain to process requests before they reach the controllers.

Since:
2025-05-15
See Also:
  • Constructor Details

    • JwtAuthTokenFilter

      public JwtAuthTokenFilter()
      Constructs a new JWT authentication filter.

      This default constructor initializes the filter without specific configuration. The required dependencies (jwtUtils and userDetailsService) are autowired by Spring.

  • Method Details

    • doFilterInternal

      protected void doFilterInternal(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain filterChain) throws jakarta.servlet.ServletException, IOException
      Processes an HTTP request to authenticate and authorize users.

      This method is called for each HTTP request passing through the filter chain. It examines the Authorization header for a JWT token, validates it, and if valid, sets up the security context with an authenticated user.

      Specified by:
      doFilterInternal in class OncePerRequestFilter
      Parameters:
      request - the HTTP request
      response - the HTTP response
      filterChain - the filter chain for executing other filters
      Throws:
      jakarta.servlet.ServletException - if a servlet error occurs
      IOException - if an I/O error occurs