Class OAuth2ClientHttpRequestInterceptor

java.lang.Object
org.springframework.security.oauth2.client.web.client.OAuth2ClientHttpRequestInterceptor
All Implemented Interfaces:
org.springframework.http.client.ClientHttpRequestInterceptor

public final class OAuth2ClientHttpRequestInterceptor extends Object implements org.springframework.http.client.ClientHttpRequestInterceptor
Provides an easy mechanism for using an OAuth2AuthorizedClient to make OAuth 2.0 requests by including the access token as a bearer token.

Example usage:

OAuth2ClientHttpRequestInterceptor requestInterceptor =
    new OAuth2ClientHttpRequestInterceptor(authorizedClientManager);
RestClient restClient = RestClient.builder()
    .requestInterceptor(requestInterceptor)
    .build();
String response = restClient.get()
    .uri(uri)
    .retrieve()
    .body(String.class);

Authentication and Authorization Failures

This interceptor has the ability to forward authentication (HTTP 401 Unauthorized) and authorization (HTTP 403 Forbidden) failures from an OAuth 2.0 Resource Server to an 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.

Use either authorizationFailureHandler(OAuth2AuthorizedClientRepository) or authorizationFailureHandler(OAuth2AuthorizedClientService) to create a RemoveAuthorizedClientOAuth2AuthorizationFailureHandler which can be provided to setAuthorizationFailureHandler(OAuth2AuthorizationFailureHandler).

For example:

OAuth2AuthorizationFailureHandler authorizationFailureHandler =
    OAuth2ClientHttpRequestInterceptor.authorizationFailureHandler(authorizedClientRepository);
requestInterceptor.setAuthorizationFailureHandler(authorizationFailureHandler);
Since:
6.4
See Also: