Class JwtAuthenticationFilter

java.lang.Object
org.springframework.web.filter.GenericFilterBean
org.springframework.web.filter.OncePerRequestFilter
app.YUP.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
A Spring Security filter that authenticates requests using JWT tokens. This filter extracts the JWT token from the request headers or cookies, and then uses it to authenticate the user. If the token is valid, the user is authenticated using Spring Security's authentication manager. If the user is successfully authenticated, the filter adds the user details to the security context, and sets the authentication object in the security context. This allows the user to be accessed by other Spring Security components, such as authorization managers.
  • 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
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    doFilterInternal(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain filterChain)
    This method is called by Spring when a request is received by the application.
    protected String
    extractTokenFromCookie(jakarta.servlet.http.HttpServletRequest request)
    Extracts the JWT token from the request cookies.

    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()
  • 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
      This method is called by Spring when a request is received by the application. It checks if the request contains a valid JWT token, and if so, authenticates the user using the token. If the user is successfully authenticated, the filter adds the user details to the security context, and sets the authentication object in the security context. This allows the user to be accessed by other Spring Security components, such as authorization managers.
      Specified by:
      doFilterInternal in class org.springframework.web.filter.OncePerRequestFilter
      Throws:
      jakarta.servlet.ServletException
      IOException
    • extractTokenFromCookie

      protected String extractTokenFromCookie(jakarta.servlet.http.HttpServletRequest request)
      Extracts the JWT token from the request cookies.
      Parameters:
      request - the HTTP request
      Returns:
      the JWT token, or null if the token could not be found