|
@@ -19,8 +19,8 @@ Kotlin::
|
|
|
[source,kotlin,role="secondary",subs="+attributes"]
|
|
|
----
|
|
|
@Bean
|
|
|
-fun myAccessTokenResponseClient(): OAuth2AccessTokenResponseClient<{grant-type}> {
|
|
|
- return new {class-name}()
|
|
|
+fun accessTokenResponseClient(): OAuth2AccessTokenResponseClient<{grant-type}> {
|
|
|
+ return {class-name}()
|
|
|
}
|
|
|
----
|
|
|
======
|
|
@@ -35,22 +35,22 @@ Choose from the following use cases to learn more:
|
|
|
|
|
|
* I want to <<oauth2-client-{section-id}-access-token-request-headers,customize headers of the Access Token request>>
|
|
|
* I want to <<oauth2-client-{section-id}-access-token-request-parameters,customize parameters of the Access Token request>>
|
|
|
-* I want to <<oauth2-client-{section-id}-access-token-response,customize the instance of `RestClient` that is used>>
|
|
|
+* I want to <<oauth2-client-{section-id}-access-token-response-rest-client,customize the instance of `RestClient` that is used>>
|
|
|
* I want to <<oauth2-client-{section-id}-access-token-response-parameters,customize parameters of the Access Token response>>
|
|
|
* I want to <<oauth2-client-{section-id}-access-token-response-errors,customize error handling of the Access Token response>>
|
|
|
|
|
|
[#oauth2-client-{section-id}-access-token-request]
|
|
|
== Customizing the Access Token Request
|
|
|
|
|
|
-`{class-name}` provides hooks for customizing HTTP headers and request parameters of the Token Request.
|
|
|
+`{class-name}` provides hooks for customizing HTTP headers and request parameters of the OAuth 2.0 Access Token Request.
|
|
|
|
|
|
[#oauth2-client-{section-id}-access-token-request-headers]
|
|
|
=== Customizing Request Headers
|
|
|
|
|
|
-There are two options for customizing HTTP headers by providing a `Converter<{grant-request}, HttpHeaders>`:
|
|
|
+There are two options for customizing HTTP headers:
|
|
|
|
|
|
-* Add additional headers by calling `addHeadersConverter(...)`
|
|
|
-* Fully customize headers by calling `setHeadersConverter(...)`
|
|
|
+* Add additional headers by calling `addHeadersConverter()`
|
|
|
+* Fully customize headers by calling `setHeadersConverter()`
|
|
|
|
|
|
You can include additional headers without affecting the default headers added to every request using `addHeadersConverter()`.
|
|
|
The following example adds a `User-Agent` header to the request when the `registrationId` is `spring`:
|
|
@@ -64,11 +64,11 @@ Java::
|
|
|
----
|
|
|
{class-name} accessTokenResponseClient =
|
|
|
new {class-name}();
|
|
|
-accessTokenResponseClient.addHeadersConverter((grantRequest) -> {
|
|
|
+accessTokenResponseClient.addHeadersConverter(grantRequest -> {
|
|
|
ClientRegistration clientRegistration = grantRequest.getClientRegistration();
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
if (clientRegistration.getRegistrationId().equals("spring")) {
|
|
|
- headers.set(HttpHeaders.USER_AGENT, "...");
|
|
|
+ headers.set(HttpHeaders.USER_AGENT, "my-user-agent");
|
|
|
}
|
|
|
return headers;
|
|
|
});
|
|
@@ -83,7 +83,7 @@ accessTokenResponseClient.addHeadersConverter { grantRequest ->
|
|
|
val clientRegistration = grantRequest.getClientRegistration()
|
|
|
val headers = HttpHeaders()
|
|
|
if (clientRegistration.getRegistrationId() == "spring") {
|
|
|
- headers[HttpHeaders.USER_AGENT] = "..."
|
|
|
+ headers[HttpHeaders.USER_AGENT] = "my-user-agent"
|
|
|
}
|
|
|
headers
|
|
|
}
|
|
@@ -124,15 +124,16 @@ accessTokenResponseClient.setHeadersConverter(headersConverter)
|
|
|
[#oauth2-client-{section-id}-access-token-request-parameters]
|
|
|
=== Customizing Request Parameters
|
|
|
|
|
|
-There are two options for customizing request parameters by providing a `Converter<{grant-request}, MultiValueMap<String, String>>`:
|
|
|
+There are three options for customizing request parameters:
|
|
|
|
|
|
-* Add additional parameters by calling `addParametersConverter(...)`
|
|
|
-* Override parameters by calling `setParametersConverter(...)`
|
|
|
+* Add additional parameters by calling `addParametersConverter()`
|
|
|
+* Override parameters by calling `setParametersConverter()`
|
|
|
+* Fully customize parameters by calling `setParametersCustomizer()`
|
|
|
|
|
|
[NOTE]
|
|
|
====
|
|
|
Using `setParametersConverter()` does not fully customize parameters because it would require the user to provide all default parameters themselves.
|
|
|
-Default parameters are always provided, but can be fully customized or omitted by providing a `Consumer<MultiValueMap<String, String>>` to `setParametersCustomizer(...)`.
|
|
|
+Default parameters are always provided, but can be fully customized or omitted by calling `setParametersCustomizer()`.
|
|
|
====
|
|
|
|
|
|
You can include additional parameters without affecting the default parameters added to every request using `addParametersConverter()`.
|
|
@@ -147,7 +148,7 @@ Java::
|
|
|
----
|
|
|
{class-name} accessTokenResponseClient =
|
|
|
new {class-name}();
|
|
|
-accessTokenResponseClient.addParametersConverter((grantRequest) -> {
|
|
|
+accessTokenResponseClient.addParametersConverter(grantRequest -> {
|
|
|
ClientRegistration clientRegistration = grantRequest.getClientRegistration();
|
|
|
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<String, String>();
|
|
|
if (clientRegistration.getRegistrationId().equals("keycloak")) {
|
|
@@ -185,7 +186,7 @@ Java::
|
|
|
----
|
|
|
{class-name} accessTokenResponseClient =
|
|
|
new {class-name}();
|
|
|
-accessTokenResponseClient.setParametersConverter((grantRequest) -> {
|
|
|
+accessTokenResponseClient.setParametersConverter(grantRequest -> {
|
|
|
ClientRegistration clientRegistration = grantRequest.getClientRegistration();
|
|
|
LinkedMultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
|
|
|
if (clientRegistration.getRegistrationId().equals("okta")) {
|
|
@@ -230,7 +231,7 @@ Java::
|
|
|
----
|
|
|
{class-name} accessTokenResponseClient =
|
|
|
new {class-name}();
|
|
|
-accessTokenResponseClient.setParametersCustomizer((parameters) -> {
|
|
|
+accessTokenResponseClient.setParametersCustomizer(parameters -> {
|
|
|
if (parameters.containsKey(OAuth2ParameterNames.CLIENT_ASSERTION)) {
|
|
|
parameters.remove(OAuth2ParameterNames.CLIENT_ID);
|
|
|
}
|
|
@@ -253,7 +254,12 @@ accessTokenResponseClient.setParametersCustomizer { parameters ->
|
|
|
[#oauth2-client-{section-id}-access-token-response]
|
|
|
== Customizing the Access Token Response
|
|
|
|
|
|
-You can customize the Token Response by providing a pre-configured `RestClient` to `setRestClient(...)`.
|
|
|
+`{class-name}` provides hooks for customizing response parameters and error handling of the OAuth 2.0 Access Token Response.
|
|
|
+
|
|
|
+[#oauth2-client-{section-id}-access-token-response-rest-client]
|
|
|
+=== Customizing the `WebClient`
|
|
|
+
|
|
|
+You can customize the Token Response by providing a pre-configured `RestClient` to `setRestClient()`.
|
|
|
The default `RestClient` is configured as follows:
|
|
|
|
|
|
.Default `RestClient` Configuration
|
|
@@ -264,7 +270,7 @@ Java::
|
|
|
[source,java,role="primary",subs="+attributes"]
|
|
|
----
|
|
|
RestClient restClient = RestClient.builder()
|
|
|
- .messageConverters((messageConverters) -> {
|
|
|
+ .messageConverters(messageConverters -> {
|
|
|
messageConverters.clear();
|
|
|
messageConverters.add(new FormHttpMessageConverter());
|
|
|
messageConverters.add(new OAuth2AccessTokenResponseHttpMessageConverter());
|
|
@@ -296,10 +302,12 @@ accessTokenResponseClient.setRestClient(restClient)
|
|
|
======
|
|
|
|
|
|
`OAuth2AccessTokenResponseHttpMessageConverter` is an `HttpMessageConverter` for an OAuth 2.0 Access Token Response.
|
|
|
-You can provide `setAccessTokenResponseConverter()` with a custom `Converter<Map<String, Object>, OAuth2AccessTokenResponse>` that is used for converting the OAuth 2.0 Access Token Response parameters to an `OAuth2AccessTokenResponse`.
|
|
|
+You can customize the conversion of Token Response parameters to an `OAuth2AccessTokenResponse` by calling `setAccessTokenResponseConverter()`.
|
|
|
+The default implementation is `DefaultMapOAuth2AccessTokenResponseConverter`.
|
|
|
|
|
|
`OAuth2ErrorResponseErrorHandler` is a `ResponseErrorHandler` that can handle an OAuth 2.0 Error, such as `400 Bad Request`.
|
|
|
It uses an `OAuth2ErrorHttpMessageConverter` for converting the OAuth 2.0 Error parameters to an `OAuth2Error`.
|
|
|
+You can customize the conversion of Token Response parameters to an `OAuth2Error` by calling `setErrorConverter()`.
|
|
|
|
|
|
[TIP]
|
|
|
====
|
|
@@ -307,6 +315,8 @@ Spring MVC `FormHttpMessageConverter` is required, as it is used when sending th
|
|
|
====
|
|
|
|
|
|
[#oauth2-client-{section-id}-access-token-response-parameters]
|
|
|
+=== Customizing Response Parameters
|
|
|
+
|
|
|
The following example provides a starting point for customizing the conversion of Token Response parameters to an `OAuth2AccessTokenResponse`:
|
|
|
|
|
|
.Customize Access Token Response Converter
|
|
@@ -318,7 +328,7 @@ Java::
|
|
|
----
|
|
|
OAuth2AccessTokenResponseHttpMessageConverter accessTokenResponseMessageConverter =
|
|
|
new OAuth2AccessTokenResponseHttpMessageConverter();
|
|
|
-accessTokenResponseMessageConverter.setAccessTokenResponseConverter((parameters) -> {
|
|
|
+accessTokenResponseMessageConverter.setAccessTokenResponseConverter(parameters -> {
|
|
|
// ...
|
|
|
return OAuth2AccessTokenResponse.withToken("custom-token")
|
|
|
// ...
|
|
@@ -341,6 +351,8 @@ accessTokenResponseMessageConverter.setAccessTokenResponseConverter { parameters
|
|
|
======
|
|
|
|
|
|
[#oauth2-client-{section-id}-access-token-response-errors]
|
|
|
+=== Customizing Error Handling
|
|
|
+
|
|
|
The following example provides a starting point for customizing the conversion of Error parameters to an `OAuth2Error`:
|
|
|
|
|
|
.Customize Access Token Error Handler
|
|
@@ -352,7 +364,7 @@ Java::
|
|
|
----
|
|
|
OAuth2ErrorHttpMessageConverter errorConverter =
|
|
|
new OAuth2ErrorHttpMessageConverter();
|
|
|
-errorConverter.setErrorConverter((parameters) -> {
|
|
|
+errorConverter.setErrorConverter(parameters -> {
|
|
|
// ...
|
|
|
return new OAuth2Error("custom-error", "custom description", "custom-uri");
|
|
|
});
|