Consider using a configured `WebClient` in the application context instead of creating a new one on each component
Context
When using Spring Gateway with Spring Security on GCP, I have to connect to an external Identity Provider. The problem is that the connections are closed by the NAT. One solution is to configure the connection pool. For this, I am using this configuration:
spring:
cloud:
gateway:
httpclient:
pool:
eviction-interval: 1m
max-idle-time: 1m
max-life-time: 5m
The configuration creates a HttpClient that can be used to configure a WebClient:
@Bean
WebClient webClient(HttpClient httpClient) {
return WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(httpClient))
.build();
}
Current Behavior
Unfortunately, the WebClient is never used "automatically" by the autoconfiguration and in some cases, you are even forced to reimplement some interfaces just to make it possible to share the WebClient:
@Bean
ReactiveOAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> accessTokenResponseClient(WebClient webClient) {
var authorizationCodeTokenResponseClient = new WebClientReactiveAuthorizationCodeTokenResponseClient();
// the WebClient in the application context is not set automatically
authorizationCodeTokenResponseClient.setWebClient(webClient);
return authorizationCodeTokenResponseClient;
}
@Bean
ReactiveJwtDecoderFactory<ClientRegistration> jwtDecoderFactory(WebClient webClient) {
// I had to replace the ReactiveOidcIdTokenDecoderFactory to be able to set a configured WebClient
return new JwtDecoderFactory(webClient);
}
...
Expected Behavior
When a WebClient is in the application context, it should be used by the auto configuration.
Additionally, the factories should be able to fully configure the created beans. This is not the case of the ReactiveOidcIdTokenDecoderFactory which cannot be configured with a WebClient and force NimbusReactiveJwtDecoder to create a new one.
Philosophically related to the issue described in https://github.com/spring-projects/spring-security/issues/14946, i.e. Spring Security not using the builder defined in the application context to instantiate HTTP clients.