Question
I have a webflux app to be used as a Eureka client (with eureka.client.webclient.enabled). The Eureka server is secured behind OAuth2. I want to be able to connect to it securely with a token retrieved with my own ClientRegistration. What is the preferred way to provide my own WebClient to Eureka's WebClientEurekaHttpClient?
What I've tried
I have tried overriding the EurekaHttpClient bean with my own WebClientEurekaHttpClient that's setup with a secure version of WebClient:
@Bean
// @Primary still didn't work
EurekaHttpClient oAuth2WebClientEurekaHttpClient() {
//...
return new WebClientEurekaHttpClient(myWebClient);
}
I have gotten a @Primary WebClient to work, but that also overrides the WebClient for all other usages of the WebClient bean. I would prefer to give a specific WebClient that is only used by WebClientEurekaHttpClient.
Dependencies
Spring Cloud 2020.0.1 Spring Boot 2.4 Spring Cloud Starter Netflix Eureka Client 3.0.1
Comment From: semagnum
Resolved issue myself, figured this would be helpful for others as I've seen no one show examples of this. But I had looked over this piece of documentation, so forgive me for being somewhat redundant: cloud.spring.io/spring-cloud-netflix/multi/multi__service_discovery_eureka_clients.html#_authenticating_with_the_eureka_server
For my case, I just had to provide a WebClientDiscoveryClientOptionalArgs bean, which has a WebClient builder as an argument:
@Bean
public WebClientDiscoveryClientOptionalArgs myOptionalArgs() {
return new WebClientDiscoveryClientOptionalArgs(
() -> WebClient.builder().filter(myOAuthFilterFunctionHere()).build()
);
}
Eureka used the bean and authenticated successfully. Cheers.