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
ConstructorsConstructorDescriptionTokenAuthenticationFilter(String token, org.springframework.security.web.server.context.ServerSecurityContextRepository securityContextRepository) Constructs a newTokenAuthenticationFilterinstance with the specified token and security context repository. -
Method Summary
Modifier and TypeMethodDescriptionreactor.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.
-
Constructor Details
-
TokenAuthenticationFilter
public TokenAuthenticationFilter(String token, org.springframework.security.web.server.context.ServerSecurityContextRepository securityContextRepository) Constructs a newTokenAuthenticationFilterinstance with the specified token and security context repository.- Parameters:
token- the token used for authentication.securityContextRepository- theServerSecurityContextRepositoryto 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 usingTokenAuthenticationManager. 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:
filterin interfaceorg.springframework.web.server.WebFilter- Parameters:
exchange- theServerWebExchangethat contains the request and response objects.chain- theWebFilterChainthat 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 successfulAuthenticationresult.webFilterExchange- theWebFilterExchangecontaining the current request and response.- Returns:
- a
reactor.core.publisher.Mono<Void>object.
-