mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-05-31 17:22:13 +00:00
Document authentication helper method in WebClient integration
This commit re-applies 49f3c0ce534254ea4b5f5a674c5afb8322c1736c which was lost while splitting pages for Antora. Issue gh-13816 Issue gh-10120
This commit is contained in:
parent
52675c80b3
commit
7335c5745c
@ -198,6 +198,63 @@ fun index(): String {
|
|||||||
======
|
======
|
||||||
<1> `clientRegistrationId()` is a `static` method in `ServletOAuth2AuthorizedClientExchangeFilterFunction`.
|
<1> `clientRegistrationId()` is a `static` method in `ServletOAuth2AuthorizedClientExchangeFilterFunction`.
|
||||||
|
|
||||||
|
The following code shows how to set an `Authentication` as a request attribute:
|
||||||
|
|
||||||
|
[tabs]
|
||||||
|
======
|
||||||
|
Java::
|
||||||
|
+
|
||||||
|
[source,java,role="primary"]
|
||||||
|
----
|
||||||
|
@GetMapping("/")
|
||||||
|
public String index() {
|
||||||
|
String resourceUri = ...
|
||||||
|
|
||||||
|
Authentication anonymousAuthentication = new AnonymousAuthenticationToken(
|
||||||
|
"anonymous", "anonymousUser", AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS"));
|
||||||
|
String body = webClient
|
||||||
|
.get()
|
||||||
|
.uri(resourceUri)
|
||||||
|
.attributes(authentication(anonymousAuthentication)) <1>
|
||||||
|
.retrieve()
|
||||||
|
.bodyToMono(String.class)
|
||||||
|
.block();
|
||||||
|
|
||||||
|
...
|
||||||
|
|
||||||
|
return "index";
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|
||||||
|
Kotlin::
|
||||||
|
+
|
||||||
|
[source,kotlin,role="secondary"]
|
||||||
|
----
|
||||||
|
@GetMapping("/")
|
||||||
|
fun index(): String {
|
||||||
|
val resourceUri: String = ...
|
||||||
|
|
||||||
|
val anonymousAuthentication: Authentication = AnonymousAuthenticationToken(
|
||||||
|
"anonymous", "anonymousUser", AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS"))
|
||||||
|
val body: String = webClient
|
||||||
|
.get()
|
||||||
|
.uri(resourceUri)
|
||||||
|
.attributes(authentication(anonymousAuthentication)) <1>
|
||||||
|
.retrieve()
|
||||||
|
.bodyToMono()
|
||||||
|
.block()
|
||||||
|
|
||||||
|
...
|
||||||
|
|
||||||
|
return "index"
|
||||||
|
}
|
||||||
|
----
|
||||||
|
======
|
||||||
|
<1> `authentication()` is a `static` method in `ServletOAuth2AuthorizedClientExchangeFilterFunction`.
|
||||||
|
|
||||||
|
[WARNING]
|
||||||
|
It is recommended to be cautious with this feature since all HTTP requests will receive an access token bound to the provided principal.
|
||||||
|
|
||||||
|
|
||||||
=== Defaulting the Authorized Client
|
=== Defaulting the Authorized Client
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user