@ -9,13 +9,24 @@ For Servlet environments, refer to <<oauth2Client-webclient-servlet, WebClient f
@@ -9,13 +9,24 @@ For Servlet environments, refer to <<oauth2Client-webclient-servlet, WebClient f
Spring Framework has built in support for setting a Bearer token.
[source,java]
====
.Java
[source,java,role="primary"]
----
webClient.get()
.headers(h -> h.setBearerAuth(token))
...
----
.Kotlin
[source,kotlin,role="secondary"]
----
webClient.get()
.headers { it.setBearerAuth(token) }
...
----
====
Spring Security builds on this support to provide additional benefits:
* Spring Security will automatically refresh expired tokens (if a refresh token is present)
@ -30,7 +41,9 @@ Spring Security builds on this support to provide additional benefits:
@@ -30,7 +41,9 @@ Spring Security builds on this support to provide additional benefits:
The first step is ensuring to setup the `WebClient` correctly.
An example of setting up `WebClient` in a fully reactive environment can be found below:
@ -54,7 +85,9 @@ If we set `defaultOAuth2AuthorizedClient` to `true` in our setup and the user au
@@ -54,7 +85,9 @@ If we set `defaultOAuth2AuthorizedClient` to `true` in our setup and the user au
Alternatively, if we set `defaultClientRegistrationId` to a valid `ClientRegistration` id, that registration is used to provide the access token.
This is convenient, but in environments where not all endpoints should get the access token, it is dangerous (you might provide the wrong access token to an endpoint).
[source,java]
====
.Java
[source,java,role="primary"]
----
Mono<String> body = this.webClient
.get()
@ -63,6 +96,17 @@ Mono<String> body = this.webClient
@@ -63,6 +96,17 @@ Mono<String> body = this.webClient
.bodyToMono(String.class);
----
.Kotlin
[source,kotlin,role="secondary"]
----
val body: Mono<String> = webClient
.get()
.uri(this.uri)
.retrieve()
.bodyToMono()
----
====
[[webclient-explicit]]
== Explicit OAuth2AuthorizedClient
@ -70,7 +114,9 @@ The `OAuth2AuthorizedClient` can be explicitly provided by setting it on the req
@@ -70,7 +114,9 @@ The `OAuth2AuthorizedClient` can be explicitly provided by setting it on the req
In the example below we resolve the `OAuth2AuthorizedClient` using Spring WebFlux or Spring MVC argument resolver support.
However, it does not matter how the `OAuth2AuthorizedClient` is resolved.
Alternatively, it is possible to specify the `clientRegistrationId` on the request attributes and the `WebClient` will attempt to lookup the `OAuth2AuthorizedClient`.
If it is not found, one will automatically be acquired.
[source,java]
====
.Java
[source,java,role="primary"]
----
Mono<String> body = this.webClient
.get()
@ -98,3 +161,15 @@ Mono<String> body = this.webClient
@@ -98,3 +161,15 @@ Mono<String> body = this.webClient