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

    Constructors
    Constructor
    Description
    JwtAuthenticationFilter(JwtService jwtService, UserProfileService userProfileService, org.springframework.web.servlet.HandlerExceptionResolver resolver)
    Constructor for JwtAuthenticationFilter that injects the necessary services.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected 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

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • JwtAuthenticationFilter

      public JwtAuthenticationFilter(JwtService jwtService, @Lazy UserProfileService userProfileService, @Qualifier("handlerExceptionResolver") org.springframework.web.servlet.HandlerExceptionResolver resolver)
      Constructor for JwtAuthenticationFilter that injects the necessary services.
      Parameters:
      jwtService - The JwtService used for JWT operations.
      userProfileService - The UserProfileService for loading user details.
      resolver - The HandlerExceptionResolver 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 class org.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.