spring-projects / spring-projects/spring-security
Consider using a configured `WebClient` in the application context instead of creating a new one on each component
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 9.6k
- Forks
- 6.3k
- Avg merge
- 2d 11h
- Merged PRs (30d)
- 52
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the auto-configuration and the named components, including ReactiveOAuth2AccessTokenResponseClient, ReactiveOidcIdTokenDecoderFactory, and NimbusReactiveJwtDecoder. Determine how a WebClient from the application context could be used consistently, and define completion through tests showing configured clients are reused without requiring replacement factories.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- authentication, security
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100