File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed
docs/modules/ROOT/pages/servlet/oauth2 Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change @@ -2212,6 +2212,60 @@ fun index(): String {
2212
2212
====
2213
2213
<1> `clientRegistrationId()` is a `static` method in `ServletOAuth2AuthorizedClientExchangeFilterFunction`.
2214
2214
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
+
2215
2269
2216
2270
=== Defaulting the Authorized Client
2217
2271
You can’t perform that action at this time.
0 commit comments