2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								[[oauth2Client-additional-features]]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								= Authorized Client Features
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-12-13 16:57:36 -06:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								This section covers additional features provided by Spring Security for the OAuth2 client.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								[[oauth2Client-registered-authorized-client]]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								== Resolving an Authorized Client
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-12-13 16:57:36 -06:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								The `@RegisteredOAuth2AuthorizedClient` annotation provides the ability to resolve a method parameter to an argument value of type `OAuth2AuthorizedClient`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								This is a convenient alternative compared to accessing the `OAuth2AuthorizedClient` by using the `OAuth2AuthorizedClientManager` or `OAuth2AuthorizedClientService`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								The following example shows how to use `@RegisteredOAuth2AuthorizedClient`:
							 
						 
					
						
							
								
									
										
										
										
											2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								@Controller
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								public class OAuth2ClientController {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									@GetMapping("/")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									public String index(@RegisteredOAuth2AuthorizedClient("okta") OAuth2AuthorizedClient authorizedClient) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										OAuth2AccessToken accessToken = authorizedClient.getAccessToken();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										return "index";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								@Controller
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								class OAuth2ClientController {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    @GetMapping("/")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    fun index(@RegisteredOAuth2AuthorizedClient("okta") authorizedClient: OAuth2AuthorizedClient): String {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        val accessToken = authorizedClient.accessToken
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        return "index"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-12-13 16:57:36 -06:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								The `@RegisteredOAuth2AuthorizedClient` annotation is handled by `OAuth2AuthorizedClientArgumentResolver`, which directly uses an xref:servlet/oauth2/client/core.adoc#oauth2Client-authorized-manager-provider[`OAuth2AuthorizedClientManager`] and, therefore, inherits its capabilities.
							 
						 
					
						
							
								
									
										
										
										
											2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								[[oauth2Client-webclient-servlet]]
							 
						 
					
						
							
								
									
										
										
										
											2021-12-13 16:57:36 -06:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								== WebClient Integration for Servlet Environments
							 
						 
					
						
							
								
									
										
										
										
											2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-12-13 16:57:36 -06:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								The OAuth 2.0 Client support integrates with `WebClient` by using an `ExchangeFilterFunction`.
							 
						 
					
						
							
								
									
										
										
										
											2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-12-13 16:57:36 -06:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								The `ServletOAuth2AuthorizedClientExchangeFilterFunction` provides a mechanism for requesting protected resources by using an `OAuth2AuthorizedClient` and including the associated `OAuth2AccessToken` as a Bearer Token.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								It directly uses an xref:servlet/oauth2/client/core.adoc#oauth2Client-authorized-manager-provider[`OAuth2AuthorizedClientManager`] and, therefore, inherits the following capabilities:
							 
						 
					
						
							
								
									
										
										
										
											2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-12-13 16:57:36 -06:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								* An `OAuth2AccessToken` is requested if the client has not yet been authorized.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								** `authorization_code`: Triggers the Authorization Request redirect to initiate the flow.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								** `client_credentials`: The access token is obtained directly from the Token Endpoint.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								** `password`: The access token is obtained directly from the Token Endpoint.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								* If the `OAuth2AccessToken` is expired, it is refreshed (or renewed) if an `OAuth2AuthorizedClientProvider` is available to perform the authorization
							 
						 
					
						
							
								
									
										
										
										
											2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								The following code shows an example of how to configure `WebClient` with OAuth 2.0 Client support:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								@Bean
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								WebClient webClient(OAuth2AuthorizedClientManager authorizedClientManager) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									ServletOAuth2AuthorizedClientExchangeFilterFunction oauth2Client =
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											new ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									return WebClient.builder()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											.apply(oauth2Client.oauth2Configuration())
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											.build();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								@Bean
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								fun webClient(authorizedClientManager: OAuth2AuthorizedClientManager?): WebClient {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    val oauth2Client = ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    return WebClient.builder()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            .apply(oauth2Client.oauth2Configuration())
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            .build()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								=== Providing the Authorized Client
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								The `ServletOAuth2AuthorizedClientExchangeFilterFunction` determines the client to use (for a request) by resolving the `OAuth2AuthorizedClient` from the `ClientRequest.attributes()` (request attributes).
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								The following code shows how to set an `OAuth2AuthorizedClient` as a request attribute:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								@GetMapping("/")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								public String index(@RegisteredOAuth2AuthorizedClient("okta") OAuth2AuthorizedClient authorizedClient) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									String resourceUri = ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									String body = webClient
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											.get()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											.uri(resourceUri)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											.attributes(oauth2AuthorizedClient(authorizedClient))   <1>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											.retrieve()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											.bodyToMono(String.class)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											.block();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									return "index";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								@GetMapping("/")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								fun index(@RegisteredOAuth2AuthorizedClient("okta") authorizedClient: OAuth2AuthorizedClient): String {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    val resourceUri: String = ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    val body: String = webClient
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            .get()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            .uri(resourceUri)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            .attributes(oauth2AuthorizedClient(authorizedClient)) <1>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            .retrieve()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            .bodyToMono()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            .block()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    return "index"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								<1> `oauth2AuthorizedClient()` is a `static` method in `ServletOAuth2AuthorizedClientExchangeFilterFunction`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								The following code shows how to set the `ClientRegistration.getRegistrationId()` as a request attribute:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								@GetMapping("/")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								public String index() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									String resourceUri = ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									String body = webClient
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											.get()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											.uri(resourceUri)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											.attributes(clientRegistrationId("okta"))   <1>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											.retrieve()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											.bodyToMono(String.class)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											.block();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									return "index";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								@GetMapping("/")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								fun index(): String {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    val resourceUri: String = ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    val body: String = webClient
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            .get()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            .uri(resourceUri)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            .attributes(clientRegistrationId("okta"))  <1>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            .retrieve()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            .bodyToMono()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            .block()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    return "index"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								<1> `clientRegistrationId()` is a `static` method in `ServletOAuth2AuthorizedClientExchangeFilterFunction`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-11-09 10:46:32 -06:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								The following code shows how to set an `Authentication` as a request attribute:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								@GetMapping("/")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								public String index() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									String resourceUri = ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									Authentication anonymousAuthentication = new AnonymousAuthenticationToken(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											"anonymous", "anonymousUser", AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS"));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									String body = webClient
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											.get()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											.uri(resourceUri)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											.attributes(authentication(anonymousAuthentication))   <1>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											.retrieve()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											.bodyToMono(String.class)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											.block();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									return "index";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								@GetMapping("/")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								fun index(): String {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    val resourceUri: String = ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    val anonymousAuthentication: Authentication = AnonymousAuthenticationToken(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            "anonymous", "anonymousUser", AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS"))
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    val body: String = webClient
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            .get()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            .uri(resourceUri)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            .attributes(authentication(anonymousAuthentication))  <1>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            .retrieve()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            .bodyToMono()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            .block()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    return "index"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								<1> `authentication()` is a `static` method in `ServletOAuth2AuthorizedClientExchangeFilterFunction`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								[WARNING]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								It is recommended to be cautious with this feature since all HTTP requests will receive an access token bound to the provided principal.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								=== Defaulting the Authorized Client
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-12-13 16:57:36 -06:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								If neither `OAuth2AuthorizedClient` or `ClientRegistration.getRegistrationId()` is provided as a request attribute, the `ServletOAuth2AuthorizedClientExchangeFilterFunction` can determine the _default_ client to use, depending on its configuration.
							 
						 
					
						
							
								
									
										
										
										
											2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-12-13 16:57:36 -06:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								If `setDefaultOAuth2AuthorizedClient(true)` is configured and the user has authenticated by using `HttpSecurity.oauth2Login()`, the `OAuth2AccessToken` associated with the current `OAuth2AuthenticationToken` is used.
							 
						 
					
						
							
								
									
										
										
										
											2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								The following code shows the specific configuration:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								@Bean
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								WebClient webClient(OAuth2AuthorizedClientManager authorizedClientManager) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									ServletOAuth2AuthorizedClientExchangeFilterFunction oauth2Client =
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											new ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									oauth2Client.setDefaultOAuth2AuthorizedClient(true);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									return WebClient.builder()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											.apply(oauth2Client.oauth2Configuration())
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											.build();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								@Bean
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								fun webClient(authorizedClientManager: OAuth2AuthorizedClientManager?): WebClient {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    val oauth2Client = ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    oauth2Client.setDefaultOAuth2AuthorizedClient(true)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    return WebClient.builder()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            .apply(oauth2Client.oauth2Configuration())
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            .build()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								[WARNING]
							 
						 
					
						
							
								
									
										
										
										
											2021-12-13 16:57:36 -06:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								====
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Be cautious with this feature, since all HTTP requests receive the access token.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								====
							 
						 
					
						
							
								
									
										
										
										
											2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Alternatively, if `setDefaultClientRegistrationId("okta")` is configured with a valid `ClientRegistration`, the `OAuth2AccessToken` associated with the `OAuth2AuthorizedClient` is used.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								The following code shows the specific configuration:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								@Bean
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								WebClient webClient(OAuth2AuthorizedClientManager authorizedClientManager) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									ServletOAuth2AuthorizedClientExchangeFilterFunction oauth2Client =
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											new ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									oauth2Client.setDefaultClientRegistrationId("okta");
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									return WebClient.builder()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											.apply(oauth2Client.oauth2Configuration())
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											.build();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								@Bean
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								fun webClient(authorizedClientManager: OAuth2AuthorizedClientManager?): WebClient {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    val oauth2Client = ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    oauth2Client.setDefaultClientRegistrationId("okta")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    return WebClient.builder()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            .apply(oauth2Client.oauth2Configuration())
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            .build()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-11-04 11:31:27 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								[WARNING]
							 
						 
					
						
							
								
									
										
										
										
											2021-12-13 16:57:36 -06:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								====
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Be cautious with this feature, since all HTTP requests receive the access token.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								====