'WebClient with ClientRegistrationId as request attribute (using OAuth 2.0)
I'm searching for a working example, where I can set the ClientRegistrationId when I add the request attributes to the WebClient. The provided examples by spring.io don't work for me (docs.spring.io (5.2.12)).
When I try this example (with default ClientRegistrationId):
@Bean
WebClient webClient(ReactiveClientRegistrationRepository clientRegistrations,
ServerOAuth2AuthorizedClientRepository authorizedClients) {
ServerOAuth2AuthorizedClientExchangeFilterFunction oauth =
new ServerOAuth2AuthorizedClientExchangeFilterFunction(clientRegistrations, authorizedClients);
// (optional) explicitly opt into using the oauth2Login to provide an access token implicitly
// oauth.setDefaultOAuth2AuthorizedClient(true);
// (optional) set a default ClientRegistration.registrationId
oauth.setDefaultClientRegistrationId("MyClRegId");
return WebClient.builder()
.filter(oauth)
.build();
}
Calling the endpoint:
ResponseSpec responseSpec = this.webClient.get().uri("/NumberOrders").retrieve();
Application.Properties:
spring.security.oauth2.client.registration.MyClRegId.authorization-grant-type=client_credentials
spring.security.oauth2.client.registration.MyClRegId.client-id=myteam
spring.security.oauth2.client.registration.MyClRegId.client-secret=12349875
spring.security.oauth2.client.provider.MyClRegId.token-uri=https://something/token
spring.security.oauth2.client.registration.MyClRegId.scope=OIDC
I'm getting the following error:
Caused by: java.lang.IllegalArgumentException: serverWebExchange cannot be null
If I try to set the ClientRegestrationId as a request attribute accordingly the spring.io example (docs.spring.io (5.2.12)) there occurs another error (The Method clientRegistrationId(String) is undefined ...):
ResponseSpec responseSpec = this.webClient
.get()
.attributes(clientRegistrationId("client-id"))
.uri("/NumberOrders")
.retrieve();
This error is no surprise, because the method really doesn't exist, but does anybody has an example for such a method? Attributes requires Consumer<Map<String, Object>> attributesConsumer as input to set ClientRegistrationId. Of course it should work without the first error (serverWebExchange cannot be null).
I found a working setup here WebClient OAuth2 (answer by clocken), but ClientRegistrationId is set in a Bean. I would like to set it, while calling the endpoint. So I can use different configurations (with different ClientRegistrationIds) in my application.properties.
Maybe there is a way to set ClientRegistrationId while setting up the WebClient.Builder?
I searched a lot, but I couldn't find any answer. Thanks for help in advance.
I'm using this dependencies:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath />
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
Solution 1:[1]
You can use
webClient.get()
.uri("http://localhost:8084/retrieve-resource")
.attributes(
ServerOAuth2AuthorizedClientExchangeFilterFunction
.clientRegistrationId("bael"))
.retrieve()
// ...
here is the full code: https://www.baeldung.com/spring-webclient-oauth2
Solution 2:[2]
This is a configuration that you need from the example you mentionned:
AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager authorizedClientManager
serverWebExchange cannot be null is thrown when a webClient call is made triggered by something else than a user action, example kafka event trigger a webClient call, so there is not Request Context.
AuthorizedClientService... bean is defined to account for webClient call triggered by events, scheduled tasks threads.
service to service communication
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Eric Aya |
| Solution 2 | user3188964 |
