Class JwtAuthTokenFilter
- All Implemented Interfaces:
jakarta.servlet.Filter,Aware,BeanNameAware,DisposableBean,InitializingBean,EnvironmentAware,EnvironmentCapable,ServletContextAware
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:
-
Field Summary
Fields inherited from class org.springframework.web.filter.OncePerRequestFilter
ALREADY_FILTERED_SUFFIXFields inherited from class org.springframework.web.filter.GenericFilterBean
logger -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voiddoFilterInternal(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain filterChain) Processes an HTTP request to authenticate and authorize users.Methods inherited from class org.springframework.web.filter.OncePerRequestFilter
doFilter, doFilterNestedErrorDispatch, getAlreadyFilteredAttributeName, isAsyncDispatch, isAsyncStarted, shouldNotFilter, shouldNotFilterAsyncDispatch, shouldNotFilterErrorDispatchMethods inherited from class org.springframework.web.filter.GenericFilterBean
addRequiredProperty, afterPropertiesSet, createEnvironment, destroy, getEnvironment, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setEnvironment, setServletContext
-
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:
doFilterInternalin classOncePerRequestFilter- Parameters:
request- the HTTP requestresponse- the HTTP responsefilterChain- the filter chain for executing other filters- Throws:
jakarta.servlet.ServletException- if a servlet error occursIOException- if an I/O error occurs
-