spring-webfluxproject-reactorspring-webclientreactor-nettysslcontext

Multiple SSLContext with Webclient from WebFlux


I need to call multiple different API's from my application with WebClient. Let's say, there are two different API's. For these API's I have to use different SSLContexts that built from different certificates. I create different SSLContexts for them (using different certificates).

As a backend for WebClient I use standard approach - ReactorClientHttpConnector with Reactor Netty Http client

SSL can be configured there the following way:

new ReactorClientHttpConnector(HttpClient.create()
        .secure(sslContextSpec -> sslContextSpec.sslContext(sslContext))
);

But I'm not sure how to configure multiple different SSLContext (for each API).

Now I see three ways for doing that:


Option 1:

Create two different ReactorClientHttpConnector with different HttpClient (HttpClient.create) and put there my SSLContexts.

Eventually I'll have two WebClients each based on it's own ReactorClientHttpConnector. I don't know if this is the valid approach since I suspect I'll have then two different event loop groups in my application

Option 2:

Maybe I can chain .secure() method and add different SSLContexts like this:

new ReactorClientHttpConnector(HttpClient.create()
        .secure(sslContextSpec -> sslContextSpec.sslContext(sslContext))
        .secure(sslContextSpec -> sslContextSpec.sslContext(sslContext2))
);

However, I'm not sure if they will be both applied and not overrided, and if it will work at all.

Option 3:

Somehow create one SSLContext that contains all the required certificates


So, how to handle with this?


Solution

  • Create two different WebClients - this is approach 1

    Create two different ReactorClientHttpConnector with different HttpClient (HttpClient.create) and put there my SSLContexts. Eventually I'll have two WebClients each based on it's own ReactorClientHttpConnector. I don't know if this is the valid approach since I suspect I'll have then two different event loop groups in my application

    The two WebClients will share the event loop group. They will use different event loop groups ONLY if you configure them via runOn or you use ReactorResourceFactory. See more here