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>