Skip to content

Commit 49f3c0c

Browse files
Steve Riesenbergsjohnr
authored andcommitted
Document authentication helper method in WebClient integration
Closes gh-10120
1 parent 869e379 commit 49f3c0c

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2212,6 +2212,60 @@ fun index(): String {
22122212
====
22132213
<1> `clientRegistrationId()` is a `static` method in `ServletOAuth2AuthorizedClientExchangeFilterFunction`.
22142214

2215+
The following code shows how to set an `Authentication` as a request attribute:
2216+
2217+
====
2218+
.Java
2219+
[source,java,role="primary"]
2220+
----
2221+
@GetMapping("/")
2222+
public String index() {
2223+
String resourceUri = ...
2224+
2225+
Authentication anonymousAuthentication = new AnonymousAuthenticationToken(
2226+
"anonymous", "anonymousUser", AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS"));
2227+
String body = webClient
2228+
.get()
2229+
.uri(resourceUri)
2230+
.attributes(authentication(anonymousAuthentication)) <1>
2231+
.retrieve()
2232+
.bodyToMono(String.class)
2233+
.block();
2234+
2235+
...
2236+
2237+
return "index";
2238+
}
2239+
----
2240+
2241+
.Kotlin
2242+
[source,kotlin,role="secondary"]
2243+
----
2244+
@GetMapping("/")
2245+
fun index(): String {
2246+
val resourceUri: String = ...
2247+
2248+
val anonymousAuthentication: Authentication = AnonymousAuthenticationToken(
2249+
"anonymous", "anonymousUser", AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS"))
2250+
val body: String = webClient
2251+
.get()
2252+
.uri(resourceUri)
2253+
.attributes(authentication(anonymousAuthentication)) <1>
2254+
.retrieve()
2255+
.bodyToMono()
2256+
.block()
2257+
2258+
...
2259+
2260+
return "index"
2261+
}
2262+
----
2263+
====
2264+
<1> `authentication()` is a `static` method in `ServletOAuth2AuthorizedClientExchangeFilterFunction`.
2265+
2266+
[WARNING]
2267+
It is recommended to be cautious with this feature since all HTTP requests will receive an access token bound to the provided principal.
2268+
22152269

22162270
=== Defaulting the Authorized Client
22172271

0 commit comments

Comments
 (0)