spring-projects / spring-projects/spring-security

Consider using a configured `WebClient` in the application context instead of creating a new one on each component

Open
#16,549 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

in: oauth2 type: enhancement
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.