Class TokenAuthenticationFilter

java.lang.Object
ai.yda.framework.channel.rest.spring.streaming.security.TokenAuthenticationFilter
All Implemented Interfaces:
org.springframework.web.server.WebFilter

public class TokenAuthenticationFilter extends Object implements org.springframework.web.server.WebFilter
Provides a reactive 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 ServerSecurityContextRepository.
Since:
0.1.0
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    TokenAuthenticationFilter(String token, org.springframework.security.web.server.context.ServerSecurityContextRepository securityContextRepository)
    Constructs a new TokenAuthenticationFilter instance with the specified token and security context repository.
  • Method Summary

    Modifier and Type
    Method
    Description
    reactor.core.publisher.Mono<Void>
    filter(org.springframework.web.server.ServerWebExchange exchange, org.springframework.web.server.WebFilterChain chain)
    Filters the request to authenticate the user based on the token.
    protected reactor.core.publisher.Mono<Void>
    onAuthenticationSuccess(org.springframework.security.core.Authentication authentication, org.springframework.security.web.server.WebFilterExchange webFilterExchange)
    Handles the success of authentication by saving the security context and invoking the success handler.

    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, org.springframework.security.web.server.context.ServerSecurityContextRepository securityContextRepository)
      Constructs a new TokenAuthenticationFilter instance with the specified token and security context repository.
      Parameters:
      token - the token used for authentication.
      securityContextRepository - the ServerSecurityContextRepository to manage the security context.
  • Method Details

    • filter

      @NonNull public reactor.core.publisher.Mono<Void> filter(@NonNull org.springframework.web.server.ServerWebExchange exchange, @NonNull org.springframework.web.server.WebFilterChain chain)
      Filters the request to authenticate the user based on the token.

      This method attempts to convert the token from the request using TokenAuthenticationConverter. If successful, it proceeds with authentication using TokenAuthenticationManager. Upon successful authentication, it saves the security context and triggers the success handler. In case of authentication failure, it invokes the failure handler.

      Specified by:
      filter in interface org.springframework.web.server.WebFilter
      Parameters:
      exchange - the ServerWebExchange that contains the request and response objects.
      chain - the WebFilterChain that allows further processing of the request.
      Returns:
      a reactor.core.publisher.Mono<Void> object.
    • onAuthenticationSuccess

      protected reactor.core.publisher.Mono<Void> onAuthenticationSuccess(org.springframework.security.core.Authentication authentication, org.springframework.security.web.server.WebFilterExchange webFilterExchange)
      Handles the success of authentication by saving the security context and invoking the success handler.
      Parameters:
      authentication - the successful Authentication result.
      webFilterExchange - the WebFilterExchange containing the current request and response.
      Returns:
      a reactor.core.publisher.Mono<Void> object.