Class ServletOAuth2AuthorizedClientExchangeFilterFunction

java.lang.Object
org.springframework.security.oauth2.client.web.reactive.function.client.ServletOAuth2AuthorizedClientExchangeFilterFunction
All Implemented Interfaces:
org.springframework.web.reactive.function.client.ExchangeFilterFunction

public final class ServletOAuth2AuthorizedClientExchangeFilterFunction extends Object implements org.springframework.web.reactive.function.client.ExchangeFilterFunction
Provides an easy mechanism for using an OAuth2AuthorizedClient to make OAuth 2.0 requests by including the access token as a bearer token.

NOTE:This class is intended to be used in a Servlet environment.

Example usage:

 ServletOAuth2AuthorizedClientExchangeFilterFunction oauth2 = new ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
 WebClient webClient = WebClient.builder()
    .apply(oauth2.oauth2Configuration())
    .build();
 Mono<String> response = webClient
    .get()
    .uri(uri)
    .attributes(oauth2AuthorizedClient(authorizedClient))
    // ...
    .retrieve()
    .bodyToMono(String.class);
 

Authentication and Authorization Failures

Since 5.3, this filter function has the ability to forward authentication (HTTP 401 Unauthorized) and authorization (HTTP 403 Forbidden) failures from an OAuth 2.0 Resource Server to a OAuth2AuthorizationFailureHandler. A RemoveAuthorizedClientOAuth2AuthorizationFailureHandler can be used to remove the cached OAuth2AuthorizedClient, so that future requests will result in a new token being retrieved from an Authorization Server, and sent to the Resource Server.

If the ServletOAuth2AuthorizedClientExchangeFilterFunction(ClientRegistrationRepository, OAuth2AuthorizedClientRepository) constructor is used, a RemoveAuthorizedClientOAuth2AuthorizationFailureHandler will be configured automatically.

If the ServletOAuth2AuthorizedClientExchangeFilterFunction(OAuth2AuthorizedClientManager) constructor is used, a RemoveAuthorizedClientOAuth2AuthorizationFailureHandler will NOT be configured automatically. It is recommended that you configure one via setAuthorizationFailureHandler(OAuth2AuthorizationFailureHandler).

Since:
5.1
See Also:
  • Constructor Details

    • ServletOAuth2AuthorizedClientExchangeFilterFunction

      public ServletOAuth2AuthorizedClientExchangeFilterFunction()
    • ServletOAuth2AuthorizedClientExchangeFilterFunction

      public ServletOAuth2AuthorizedClientExchangeFilterFunction(OAuth2AuthorizedClientManager authorizedClientManager)
      Constructs a ServletOAuth2AuthorizedClientExchangeFilterFunction using the provided parameters.

      When this constructor is used, authentication (HTTP 401) and authorization (HTTP 403) failures returned from an OAuth 2.0 Resource Server will NOT be forwarded to an OAuth2AuthorizationFailureHandler. Therefore, future requests to the Resource Server will most likely use the same (likely invalid) token, resulting in the same errors returned from the Resource Server. It is recommended to configure a RemoveAuthorizedClientOAuth2AuthorizationFailureHandler via setAuthorizationFailureHandler(OAuth2AuthorizationFailureHandler) so that authentication and authorization failures returned from a Resource Server will result in removing the authorized client, so that a new token is retrieved for future requests.

      Parameters:
      authorizedClientManager - the OAuth2AuthorizedClientManager which manages the authorized client(s)
      Since:
      5.2
    • ServletOAuth2AuthorizedClientExchangeFilterFunction

      public ServletOAuth2AuthorizedClientExchangeFilterFunction(ClientRegistrationRepository clientRegistrationRepository, OAuth2AuthorizedClientRepository authorizedClientRepository)
      Constructs a ServletOAuth2AuthorizedClientExchangeFilterFunction using the provided parameters.

      Since 5.3, when this constructor is used, authentication (HTTP 401) and authorization (HTTP 403) failures returned from an OAuth 2.0 Resource Server will be forwarded to a RemoveAuthorizedClientOAuth2AuthorizationFailureHandler, which will potentially remove the OAuth2AuthorizedClient from the given OAuth2AuthorizedClientRepository, depending on the OAuth 2.0 error code returned. Authentication failures returned from an OAuth 2.0 Resource Server typically indicate that the token is invalid, and should not be used in future requests. Removing the authorized client from the repository will ensure that the existing token will not be sent for future requests to the Resource Server, and a new token is retrieved from the Authorization Server and used for future requests to the Resource Server.

      Parameters:
      clientRegistrationRepository - the repository of client registrations
      authorizedClientRepository - the repository of authorized clients
  • Method Details