Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: configure ssl context for webclient #3811

Merged
merged 3 commits into from
Oct 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -176,26 +176,19 @@ public HttpsFactory factory() {
* @param httpClient default http client
* @param headersFiltersProvider header filter for spring gateway router
* @param properties client HTTP properties
* @return instance of NettyRoutingFilterApiml
* @return instance of NettyRoutingFilterApiml
*/
@Bean
public NettyRoutingFilterApiml createNettyRoutingFilterApiml(HttpClient httpClient, ObjectProvider<List<HttpHeadersFilter>> headersFiltersProvider, HttpClientProperties properties) {
// obtain SSL contexts (one with keystore to support client cert sign and truststore, second just with truststore)
SslContext justTruststore = sslContext(false);
SslContext withKeystore = sslContext(true);
return new NettyRoutingFilterApiml(getHttpClient(httpClient, false), getHttpClient(httpClient, true), headersFiltersProvider, properties);
}

var builderJustTruststore = SslProvider.builder().sslContext(justTruststore);
var builderWithKeystore = SslProvider.builder().sslContext(withKeystore);
public HttpClient getHttpClient(HttpClient httpClient, boolean useClientCert) {
var sslContextBuilder = SslProvider.builder().sslContext(getSslContext(useClientCert));
if (!nonStrictVerifySslCertificatesOfServices) {
builderJustTruststore.handlerConfigurator(HttpClientSecurityUtils.HOSTNAME_VERIFICATION_CONFIGURER);
builderWithKeystore.handlerConfigurator(HttpClientSecurityUtils.HOSTNAME_VERIFICATION_CONFIGURER);
sslContextBuilder.handlerConfigurator(HttpClientSecurityUtils.HOSTNAME_VERIFICATION_CONFIGURER);
}

// construct http clients with different SSL configuration - with / without client certs
var httpClientNoCert = httpClient.secure(builderJustTruststore.build());
var httpClientClientCert = httpClient.secure(builderWithKeystore.build());

return new NettyRoutingFilterApiml(httpClientNoCert, httpClientClientCert, headersFiltersProvider, properties);
return httpClient.secure(sslContextBuilder.build());
}

/**
Expand Down Expand Up @@ -225,7 +218,7 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) thro
/**
* @return io.netty.handler.ssl.SslContext for http client.
*/
SslContext sslContext(boolean setKeystore) {
SslContext getSslContext(boolean setKeystore) {
try {
SslContextBuilder builder = SslContextBuilder.forClient();

Expand Down Expand Up @@ -348,7 +341,7 @@ public HttpClientFactory gatewayHttpClientFactory(
ServerProperties serverProperties, List<HttpClientCustomizer> customizers,
HttpClientSslConfigurer sslConfigurer
) {
SslContext sslContext = sslContext(false);
SslContext sslContext = getSslContext(false);
return new HttpClientFactory(properties, serverProperties, sslConfigurer, customizers) {
@Override
protected HttpClient createInstance() {
Expand All @@ -360,13 +353,12 @@ protected HttpClient createInstance() {
@Bean
@Primary
public WebClient webClient(HttpClient httpClient) {
return WebClient.builder().clientConnector(new ReactorClientHttpConnector(httpClient)).build();
return WebClient.builder().clientConnector(new ReactorClientHttpConnector(getHttpClient(httpClient, false))).build();
}

@Bean
public WebClient webClientClientCert(HttpClient httpClient) {
httpClient = httpClient.secure(sslContextSpec -> sslContextSpec.sslContext(sslContext(true)));
return WebClient.builder().clientConnector(new ReactorClientHttpConnector(httpClient)).build();
return WebClient.builder().clientConnector(new ReactorClientHttpConnector(getHttpClient(httpClient, true))).build();
}

@Bean
Expand Down
Loading