Create webclient object based on changes in BaseURL & scope

23 Views Asked by At

I want to access external API with different baseURLs & scopes but client credentials are same for them using webclient.

Here is how I have done for now

spring:
  security:
    oauth2:
      client:
        registration:
          github:
            clientId: github-client-id
            clientSecret: github-client-secret
            scope: abc
          google:
            clientId: github-client-id
            clientSecret: github-client-secret
            scope: xyz
@Bean
WebClient webClient(ReactiveOAuth2AuthorizedClientManager authorizedClientManager) {
    ServerOAuth2AuthorizedClientExchangeFilterFunction oauth2Client =
            new ServerOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
    oauth2Client.setDefaultClientRegistrationId("google");
    return WebClient.builder()
            .baseUrl("google.com")
            .filter(oauth2Client)
            .build();
}

@Bean
WebClient webClient(ReactiveOAuth2AuthorizedClientManager authorizedClientManager) {
    ServerOAuth2AuthorizedClientExchangeFilterFunction oauth2Client =
            new ServerOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
    oauth2Client.setDefaultClientRegistrationId("github");
    return WebClient.builder()
            .baseUrl("github.com")
            .filter(oauth2Client)
            .build();
}

Here you can see that clientId & Secret is similar just scope is changing and based on which registrationId is changing.

How can I simplify this logic for building webclients based on changes in BaseURL & Scopes/registrations ?

0

There are 0 best solutions below