Skip to content

Commit 7335c57

Browse files
author
Steve Riesenberg
committed
Document authentication helper method in WebClient integration
This commit re-applies 49f3c0c which was lost while splitting pages for Antora. Issue gh-13816 Issue gh-10120
1 parent 52675c8 commit 7335c57

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

docs/modules/ROOT/pages/servlet/oauth2/client/authorized-clients.adoc

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,63 @@ fun index(): String {
198198
======
199199
<1> `clientRegistrationId()` is a `static` method in `ServletOAuth2AuthorizedClientExchangeFilterFunction`.
200200

201+
The following code shows how to set an `Authentication` as a request attribute:
202+
203+
[tabs]
204+
======
205+
Java::
206+
+
207+
[source,java,role="primary"]
208+
----
209+
@GetMapping("/")
210+
public String index() {
211+
String resourceUri = ...
212+
213+
Authentication anonymousAuthentication = new AnonymousAuthenticationToken(
214+
"anonymous", "anonymousUser", AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS"));
215+
String body = webClient
216+
.get()
217+
.uri(resourceUri)
218+
.attributes(authentication(anonymousAuthentication)) <1>
219+
.retrieve()
220+
.bodyToMono(String.class)
221+
.block();
222+
223+
...
224+
225+
return "index";
226+
}
227+
----
228+
229+
Kotlin::
230+
+
231+
[source,kotlin,role="secondary"]
232+
----
233+
@GetMapping("/")
234+
fun index(): String {
235+
val resourceUri: String = ...
236+
237+
val anonymousAuthentication: Authentication = AnonymousAuthenticationToken(
238+
"anonymous", "anonymousUser", AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS"))
239+
val body: String = webClient
240+
.get()
241+
.uri(resourceUri)
242+
.attributes(authentication(anonymousAuthentication)) <1>
243+
.retrieve()
244+
.bodyToMono()
245+
.block()
246+
247+
...
248+
249+
return "index"
250+
}
251+
----
252+
======
253+
<1> `authentication()` is a `static` method in `ServletOAuth2AuthorizedClientExchangeFilterFunction`.
254+
255+
[WARNING]
256+
It is recommended to be cautious with this feature since all HTTP requests will receive an access token bound to the provided principal.
257+
201258

202259
=== Defaulting the Authorized Client
203260

0 commit comments

Comments
 (0)