Skip to content

Commit 324d679

Browse files
committed
Add WebClient samples to docs
Issue gh-8172
1 parent 496fcbb commit 324d679

File tree

1 file changed

+80
-5
lines changed

1 file changed

+80
-5
lines changed

docs/manual/src/docs/asciidoc/_includes/reactive/webclient.adoc

+80-5
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,24 @@ For Servlet environments, refer to <<oauth2Client-webclient-servlet, WebClient f
99

1010
Spring Framework has built in support for setting a Bearer token.
1111

12-
[source,java]
12+
====
13+
.Java
14+
[source,java,role="primary"]
1315
----
1416
webClient.get()
1517
.headers(h -> h.setBearerAuth(token))
1618
...
1719
----
1820
21+
.Kotlin
22+
[source,kotlin,role="secondary"]
23+
----
24+
webClient.get()
25+
.headers { it.setBearerAuth(token) }
26+
...
27+
----
28+
====
29+
1930
Spring Security builds on this support to provide additional benefits:
2031

2132
* 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:
3041
The first step is ensuring to setup the `WebClient` correctly.
3142
An example of setting up `WebClient` in a fully reactive environment can be found below:
3243

33-
[source,java]
44+
====
45+
.Java
46+
[source,java,role="primary"]
3447
----
3548
@Bean
3649
WebClient webClient(ReactiveClientRegistrationRepository clientRegistrations,
@@ -47,14 +60,34 @@ WebClient webClient(ReactiveClientRegistrationRepository clientRegistrations,
4760
}
4861
----
4962
63+
.Kotlin
64+
[source,kotlin,role="secondary"]
65+
----
66+
@Bean
67+
fun webClient(clientRegistrations: ReactiveClientRegistrationRepository,
68+
authorizedClients: ServerOAuth2AuthorizedClientRepository): WebClient {
69+
val oauth = ServerOAuth2AuthorizedClientExchangeFilterFunction(clientRegistrations, authorizedClients)
70+
// (optional) explicitly opt into using the oauth2Login to provide an access token implicitly
71+
// oauth.setDefaultOAuth2AuthorizedClient(true)
72+
// (optional) set a default ClientRegistration.registrationId
73+
// oauth.setDefaultClientRegistrationId("client-registration-id")
74+
return WebClient.builder()
75+
.filter(oauth)
76+
.build()
77+
}
78+
----
79+
====
80+
5081
[[webclient-implicit]]
5182
== Implicit OAuth2AuthorizedClient
5283

5384
If we set `defaultOAuth2AuthorizedClient` to `true` in our setup and the user authenticated with oauth2Login (i.e. OIDC), then the current authentication is used to automatically provide the access token.
5485
Alternatively, if we set `defaultClientRegistrationId` to a valid `ClientRegistration` id, that registration is used to provide the access token.
5586
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).
5687

57-
[source,java]
88+
====
89+
.Java
90+
[source,java,role="primary"]
5891
----
5992
Mono<String> body = this.webClient
6093
.get()
@@ -63,14 +96,27 @@ Mono<String> body = this.webClient
6396
.bodyToMono(String.class);
6497
----
6598
99+
.Kotlin
100+
[source,kotlin,role="secondary"]
101+
----
102+
val body: Mono<String> = webClient
103+
.get()
104+
.uri(this.uri)
105+
.retrieve()
106+
.bodyToMono()
107+
----
108+
====
109+
66110
[[webclient-explicit]]
67111
== Explicit OAuth2AuthorizedClient
68112

69113
The `OAuth2AuthorizedClient` can be explicitly provided by setting it on the requests attributes.
70114
In the example below we resolve the `OAuth2AuthorizedClient` using Spring WebFlux or Spring MVC argument resolver support.
71115
However, it does not matter how the `OAuth2AuthorizedClient` is resolved.
72116

73-
[source,java]
117+
====
118+
.Java
119+
[source,java,role="primary"]
74120
----
75121
@GetMapping("/explicit")
76122
Mono<String> explicit(@RegisteredOAuth2AuthorizedClient("client-id") OAuth2AuthorizedClient authorizedClient) {
@@ -83,13 +129,30 @@ Mono<String> explicit(@RegisteredOAuth2AuthorizedClient("client-id") OAuth2Autho
83129
}
84130
----
85131
132+
.Kotlin
133+
[source,kotlin,role="secondary"]
134+
----
135+
@GetMapping("/explicit")
136+
fun explicit(@RegisteredOAuth2AuthorizedClient("client-id") authorizedClient: OAuth2AuthorizedClient?): Mono<String> {
137+
return this.webClient
138+
.get()
139+
.uri(uri)
140+
.attributes(oauth2AuthorizedClient(authorizedClient))
141+
.retrieve()
142+
.bodyToMono()
143+
}
144+
----
145+
====
146+
86147
[[webclient-clientregistrationid]]
87148
== clientRegistrationId
88149

89150
Alternatively, it is possible to specify the `clientRegistrationId` on the request attributes and the `WebClient` will attempt to lookup the `OAuth2AuthorizedClient`.
90151
If it is not found, one will automatically be acquired.
91152

92-
[source,java]
153+
====
154+
.Java
155+
[source,java,role="primary"]
93156
----
94157
Mono<String> body = this.webClient
95158
.get()
@@ -98,3 +161,15 @@ Mono<String> body = this.webClient
98161
.retrieve()
99162
.bodyToMono(String.class);
100163
----
164+
165+
.Kotlin
166+
[source,kotlin,role="secondary"]
167+
----
168+
val body: Mono<String> = this.webClient
169+
.get()
170+
.uri(uri)
171+
.attributes(clientRegistrationId("client-id"))
172+
.retrieve()
173+
.bodyToMono()
174+
----
175+
====

0 commit comments

Comments
 (0)