Skip to content

Commit e8d98a5

Browse files
committed
Add ref doc for refresh_token grant
Fixes gh-7398
1 parent 034b5e9 commit e8d98a5

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

docs/manual/src/docs/asciidoc/_includes/servlet/preface/oauth2-client.adoc

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ The following sections will go into more detail on the core components used by O
8181
** <<oauth2Client-authorized-manager-provider, OAuth2AuthorizedClientManager / OAuth2AuthorizedClientProvider>>
8282
* <<oauth2Client-auth-grant-support>>
8383
** <<oauth2Client-auth-code-grant, Authorization Code>>
84+
** <<oauth2Client-refresh-token-grant, Refresh Token>>
8485
** <<oauth2Client-client-creds-grant, Client Credentials>>
8586
* <<oauth2Client-additional-features>>
8687
** <<oauth2Client-registered-authorized-client, Resolving an Authorized Client>>
@@ -552,6 +553,80 @@ public class OAuth2ClientSecurityConfig extends WebSecurityConfigurerAdapter {
552553
----
553554

554555

556+
[[oauth2Client-refresh-token-grant]]
557+
==== Refresh Token
558+
559+
[NOTE]
560+
Please refer to the OAuth 2.0 Authorization Framework for further details on the https://tools.ietf.org/html/rfc6749#section-1.5[Refresh Token].
561+
562+
563+
===== Refreshing an Access Token
564+
565+
[NOTE]
566+
Please refer to the https://tools.ietf.org/html/rfc6749#section-6[Access Token Request/Response] protocol flow for the Refresh Token grant.
567+
568+
The default implementation of `OAuth2AccessTokenResponseClient` for the Refresh Token grant is `DefaultRefreshTokenTokenResponseClient`, which uses a `RestOperations` when refreshing an access token at the Authorization Server’s Token Endpoint.
569+
570+
The `DefaultRefreshTokenTokenResponseClient` is quite flexible as it allows you to customize the pre-processing of the Token Request and/or post-handling of the Token Response.
571+
572+
573+
===== Customizing the Access Token Request
574+
575+
If you need to customize the pre-processing of the Token Request, you can provide `DefaultRefreshTokenTokenResponseClient.setRequestEntityConverter()` with a custom `Converter<OAuth2RefreshTokenGrantRequest, RequestEntity<?>>`.
576+
The default implementation `OAuth2RefreshTokenGrantRequestEntityConverter` builds a `RequestEntity` representation of a standard https://tools.ietf.org/html/rfc6749#section-6[OAuth 2.0 Access Token Request].
577+
However, providing a custom `Converter`, would allow you to extend the standard Token Request and add custom parameter(s).
578+
579+
IMPORTANT: The custom `Converter` must return a valid `RequestEntity` representation of an OAuth 2.0 Access Token Request that is understood by the intended OAuth 2.0 Provider.
580+
581+
582+
===== Customizing the Access Token Response
583+
584+
On the other end, if you need to customize the post-handling of the Token Response, you will need to provide `DefaultRefreshTokenTokenResponseClient.setRestOperations()` with a custom configured `RestOperations`.
585+
The default `RestOperations` is configured as follows:
586+
587+
[source,java]
588+
----
589+
RestTemplate restTemplate = new RestTemplate(Arrays.asList(
590+
new FormHttpMessageConverter(),
591+
new OAuth2AccessTokenResponseHttpMessageConverter()));
592+
593+
restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler());
594+
----
595+
596+
TIP: Spring MVC `FormHttpMessageConverter` is required as it's used when sending the OAuth 2.0 Access Token Request.
597+
598+
`OAuth2AccessTokenResponseHttpMessageConverter` is a `HttpMessageConverter` for an OAuth 2.0 Access Token Response.
599+
You can provide `OAuth2AccessTokenResponseHttpMessageConverter.setTokenResponseConverter()` with a custom `Converter<Map<String, String>, OAuth2AccessTokenResponse>` that is used for converting the OAuth 2.0 Access Token Response parameters to an `OAuth2AccessTokenResponse`.
600+
601+
`OAuth2ErrorResponseErrorHandler` is a `ResponseErrorHandler` that can handle an OAuth 2.0 Error, eg. 400 Bad Request.
602+
It uses an `OAuth2ErrorHttpMessageConverter` for converting the OAuth 2.0 Error parameters to an `OAuth2Error`.
603+
604+
Whether you customize `DefaultRefreshTokenTokenResponseClient` or provide your own implementation of `OAuth2AccessTokenResponseClient`, you'll need to configure it as shown in the following example:
605+
606+
[source,java]
607+
----
608+
// Customize
609+
OAuth2AccessTokenResponseClient<OAuth2RefreshTokenGrantRequest> refreshTokenTokenResponseClient = ...
610+
611+
OAuth2AuthorizedClientProvider authorizedClientProvider =
612+
OAuth2AuthorizedClientProviderBuilder.builder()
613+
.authorizationCode()
614+
.refreshToken(configurer -> configurer.accessTokenResponseClient(refreshTokenTokenResponseClient))
615+
.build();
616+
617+
...
618+
619+
authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
620+
----
621+
622+
[NOTE]
623+
`OAuth2AuthorizedClientProviderBuilder.builder().refreshToken()` configures a `RefreshTokenOAuth2AuthorizedClientProvider`,
624+
which is an implementation of an `OAuth2AuthorizedClientProvider` for the Refresh Token grant.
625+
626+
The `OAuth2RefreshToken` may optionally be returned in the Access Token Response for the `authorization_code` and `password` grant types.
627+
If the `OAuth2AuthorizedClient.getRefreshToken()` is available and the `OAuth2AuthorizedClient.getAccessToken()` is expired, it will automatically be refreshed by the `RefreshTokenOAuth2AuthorizedClientProvider`.
628+
629+
555630
[[oauth2Client-client-creds-grant]]
556631
==== Client Credentials
557632

0 commit comments

Comments
 (0)