3

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>
TimeBandit
  • 61
  • 1
  • 9

2 Answers2

3

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

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
1

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

user3188964
  • 41
  • 1
  • 4
  • For actual example of this `AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager` see: https://stackoverflow.com/a/67045987/5562284 – nouveu Oct 05 '22 at 09:21