Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I set the webClient bean used by WebReactiveFeign? #609

Open
rjbgaspar opened this issue Jul 5, 2023 · 0 comments
Open

How can I set the webClient bean used by WebReactiveFeign? #609

rjbgaspar opened this issue Jul 5, 2023 · 0 comments

Comments

@rjbgaspar
Copy link

Hi,

I have two api each one using a different bearer token.

In the first api the bearer token is set using ServerOAuth2AuthorizedClientExchangeFilterFunction, as follow:

@Slf4j
@Configuration
@RequiredArgsConstructor
public class Oauth2ClientConfiguration {
    final ApplicationProperties applicationProperties;

    final ReactiveOAuth2AuthorizedClientService authorizedClientService;

    @Bean
    public ReactiveOAuth2AuthorizedClientManager authorizedClientManager(
            ReactiveClientRegistrationRepository clientRegistrationRepository,
            ReactiveOAuth2AuthorizedClientService authorizedClientService) {

        ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider =
                ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
                        .clientCredentials()
                        .refreshToken()
                        .build();
        AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager authorizedClientManager =
                new AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager(
                        clientRegistrationRepository, authorizedClientService);
        authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);


        return authorizedClientManager;
    }

    @Bean
    public WebClient webClient(
            ReactiveOAuth2AuthorizedClientManager authorizedClientManager,
            WebClient.Builder webClientBuilder
    ) {
        ServerOAuth2AuthorizedClientExchangeFilterFunction
                oauth = new ServerOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);

        oauth.setDefaultClientRegistrationId("oidc");
        oauth.setAuthorizationFailureHandler(authorizationFailureHandler());


        return webClientBuilder.exchangeStrategies(
                ExchangeStrategies.builder()
                        .codecs(configurer -> configurer
                                .defaultCodecs()
                                .maxInMemorySize(16 * 1024 * 1024)
                        )
                        .build()
                )
//                .filter(retryOn401(authorizedClientManager))
                .filter(retryOn401TwentyTimes())
                .baseUrl(applicationProperties.getGateway().getBaseUrl())
                .filter(oauth)
                .filter(logRequest())
                .build();

    }
(...)
}

The second api, which is dagger uses ReactiveHttpRequestInterceptor for setting up the token, configured as:

@Configuration
@EnableReactiveFeignClients(basePackages = "com.gv.bearcat.service.pdm.dagger")
public class ReactiveFeignConfiguration {


    @Bean
    public Contract useFeignAnnotations() {
        return new Contract.Default();
    }

    @Bean
    public WebClient.Builder daggerWebClientBuilder() {
        return WebClient.builder();
    }

    @Bean
    //@Primary <---
    public WebClient daggerWebClient(@Qualifier("daggerWebClientBuilder") WebClient.Builder webClientBuilder) {
        return webClientBuilder.build();
    }

    @Bean
    public DaggerApi daggerApi(@Qualifier("daggerWebClient") WebClient webClient) {
        return WebReactiveFeign
                .<DaggerApi>builder(webClient.mutate())
                .target(DaggerApi.class, "http://localhost:50051");
    }
}

and

@ReactiveFeignClient(name="dagger-client", url="${application.dagger.base-url}", configuration = DaggerClientConfiguration.class)
public interface DaggerApi {
    @RequestLine("POST /api/v1/sorter-parts")
    Mono<ResponseEntity<Mono<String>>> create(SorterArticleDTO sorterArticleDTO);
}

and

@Configuration
public class DaggerClientConfiguration {
    @Bean
    public ReactiveHttpRequestInterceptor apiKeyIntercepter() {
        return ReactiveHttpRequestInterceptors.addHeader("Authorization", "Bearer my-token");
    }

    @Bean
    public ReactiveLoggerListener loggerListener() {
        return new DefaultReactiveLogger(Clock.systemUTC(), LoggerFactory.getLogger(DaggerClientConfiguration.class.getName()));
    }
}

The token is set according to the bean which is marked as primary.

Is there any away to override this behavior, e.g. set the webClient bean to be used in daggerApi
Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant