Package api.mpba.rastvdmy.config
Class JwtAuthenticationFilter
java.lang.Object
org.springframework.web.filter.GenericFilterBean
org.springframework.web.filter.OncePerRequestFilter
api.mpba.rastvdmy.config.JwtAuthenticationFilter
- All Implemented Interfaces:
jakarta.servlet.Filter
,org.springframework.beans.factory.Aware
,org.springframework.beans.factory.BeanNameAware
,org.springframework.beans.factory.DisposableBean
,org.springframework.beans.factory.InitializingBean
,org.springframework.context.EnvironmentAware
,org.springframework.core.env.EnvironmentCapable
,org.springframework.web.context.ServletContextAware
@Component
public class JwtAuthenticationFilter
extends org.springframework.web.filter.OncePerRequestFilter
Filter for JWT authentication that extends
OncePerRequestFilter
.
This filter intercepts incoming HTTP requests to validate the JWT provided in the Authorization header.
It extracts the user email from the JWT and sets the authentication in the security context if the token is valid.
The filter is designed to be used in conjunction with Spring Security to manage JWT-based authentication.
-
Field Summary
Fields inherited from class org.springframework.web.filter.OncePerRequestFilter
ALREADY_FILTERED_SUFFIX
Fields inherited from class org.springframework.web.filter.GenericFilterBean
logger
-
Constructor Summary
ConstructorDescriptionJwtAuthenticationFilter
(JwtService jwtService, UserProfileService userProfileService, org.springframework.web.servlet.HandlerExceptionResolver resolver) Constructor forJwtAuthenticationFilter
that injects the necessary services. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
doFilterInternal
(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain filterChain) The main filter method that processes the incoming HTTP request and validates the JWT.Methods inherited from class org.springframework.web.filter.OncePerRequestFilter
doFilter, doFilterNestedErrorDispatch, getAlreadyFilteredAttributeName, isAsyncDispatch, isAsyncStarted, shouldNotFilter, shouldNotFilterAsyncDispatch, shouldNotFilterErrorDispatch
Methods inherited from class org.springframework.web.filter.GenericFilterBean
addRequiredProperty, afterPropertiesSet, createEnvironment, destroy, getEnvironment, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setEnvironment, setServletContext
-
Constructor Details
-
JwtAuthenticationFilter
public JwtAuthenticationFilter(JwtService jwtService, @Lazy UserProfileService userProfileService, @Qualifier("handlerExceptionResolver") org.springframework.web.servlet.HandlerExceptionResolver resolver) Constructor forJwtAuthenticationFilter
that injects the necessary services.- Parameters:
jwtService
- TheJwtService
used for JWT operations.userProfileService
- TheUserProfileService
for loading user details.resolver
- TheHandlerExceptionResolver
to handle exceptions.
-
-
Method Details
-
doFilterInternal
protected void doFilterInternal(@NonNull jakarta.servlet.http.HttpServletRequest request, @NonNull jakarta.servlet.http.HttpServletResponse response, @NonNull jakarta.servlet.FilterChain filterChain) The main filter method that processes the incoming HTTP request and validates the JWT. This method checks for the presence of the Authorization header, extracts the JWT, and validates it against the user's details. If the token is valid, it sets the authentication in the security context.- Specified by:
doFilterInternal
in classorg.springframework.web.filter.OncePerRequestFilter
- Parameters:
request
- The incoming HTTP request.response
- The HTTP response to be sent.filterChain
- The filter chain to pass the request and response to the next filter.
-