Class TokenAuthenticationFilter

java.lang.Object
org.springframework.web.filter.GenericFilterBean
org.springframework.web.filter.OncePerRequestFilter
ai.yda.framework.channel.rest.spring.security.TokenAuthenticationFilter
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

public class TokenAuthenticationFilter extends org.springframework.web.filter.OncePerRequestFilter
Provides a Spring Security filter that processes authentication requests containing a Bearer token in the Authorization header. The filter converts the Bearer token into an Authentication object and attempts to authenticate it using the TokenAuthenticationManager. If the authentication is successful, the authenticated user is stored in the SecurityContextHolder.
Since:
0.1.0
See Also:
  • 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
    Constructs a new TokenAuthenticationFilter instance with the provided token.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected boolean
    authenticationIsRequired(org.springframework.security.core.Authentication authentication)
    Determines whether re-authentication is required based on the current authentication context.
    protected void
    doFilterInternal(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain filterChain)
    Processes the authentication request by extracting the Bearer token from the Authorization header, converting it to an Authentication object, and attempting to authenticate it.

    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

    • TokenAuthenticationFilter

      public TokenAuthenticationFilter(String token)
      Constructs a new TokenAuthenticationFilter instance with the provided token.
      Parameters:
      token - the token used to authenticate incoming requests.
  • Method Details

    • doFilterInternal

      protected void doFilterInternal(@NonNull jakarta.servlet.http.HttpServletRequest request, @NonNull jakarta.servlet.http.HttpServletResponse response, @NonNull jakarta.servlet.FilterChain filterChain) throws jakarta.servlet.ServletException, IOException
      Processes the authentication request by extracting the Bearer token from the Authorization header, converting it to an Authentication object, and attempting to authenticate it. The request and response is then forwarded to the next filter in the chain.
      Specified by:
      doFilterInternal in class org.springframework.web.filter.OncePerRequestFilter
      Parameters:
      request - the current HttpServletRequest.
      response - the current HttpServletResponse.
      filterChain - the FilterChain to proceed with the request processing.
      Throws:
      jakarta.servlet.ServletException - if an error occurs during the filtering process.
      IOException - if an I/O error occurs during the filtering process.
    • authenticationIsRequired

      protected boolean authenticationIsRequired(org.springframework.security.core.Authentication authentication)
      Determines whether re-authentication is required based on the current authentication context.
      Parameters:
      authentication - the new Authentication request to be processed.
      Returns:
      true if re-authentication is required and false otherwise.