File tree Expand file tree Collapse file tree 1 file changed +57
-0
lines changed
docs/modules/ROOT/pages/servlet/oauth2/client Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments