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