简体   繁体   English

Spring Boot AsyncRestTemplate SSLSocketFactory

[英]Spring Boot AsyncRestTemplate SSLSocketFactory

Deploying a Spring Boot application in WebSphere. 在WebSphere中部署Spring Boot应用程序。 Due to their SSL configuration, we need to explicitly specify the SSLSocketFactory to ensure the application uses the WebSphere certificates and not the default Java key/trust stores. 由于SSL配置,我们需要显式指定SSLSocketFactory以确保应用程序使用WebSphere证书而不是默认的Java密钥/信任存储。

Doing this via the RestTemplate is straightforward: 通过RestTemplate这样做很简单:

protected RestTemplate getRestTemplate() {

    SSLSocketFactory sslSocketFactory = new SSLSocketFactory(
        (javax.net.ssl.SSLSocketFactory) javax.net.ssl.SSLSocketFactory.getDefault(),
        SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER
    );

    SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme(HTTP_SCHEME, HTTP_PORT, PlainSocketFactory.getSocketFactory()));
    registry.register(new Scheme(HTTPS_SCHEME, HTTPS_PORT, sslSocketFactory));

    BasicClientConnectionManager connectionManager = new BasicClientConnectionManager(registry);
    DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager);
    ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);

    return new RestTemplate(requestFactory);
}

However with the AsyncRestTemplate, I am unable to find how to set the SSLSocketFactory: 但是使用AsyncRestTemplate,我无法找到如何设置SSLSocketFactory:

protected AsyncRestTemplate getAsyncRestTemplate() throws IOReactorException, NoSuchAlgorithmException {

    SSLIOSessionStrategy strategy = new SSLIOSessionStrategy(
        SSLContext.getDefault(),
        SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER
    );

    Registry<SchemeIOSessionStrategy> registry = RegistryBuilder.<SchemeIOSessionStrategy>create()
        .register(HTTP_SCHEME, NoopIOSessionStrategy.INSTANCE)
        .register(HTTPS_SCHEME, strategy)
        .build();

    PoolingNHttpClientConnectionManager connManager = new PoolingNHttpClientConnectionManager(
        new DefaultConnectingIOReactor(),
        registry
    );

    CloseableHttpAsyncClient httpClient = HttpAsyncClientBuilder
        .create()
        .setConnectionManager(connManager)
        .build();

    AsyncClientHttpRequestFactory requestFactory = new HttpComponentsAsyncClientHttpRequestFactory(httpClient);

    return new AsyncRestTemplate(requestFactory);
}

In essence, I need to call: 从本质上讲,我需要打电话:

(javax.net.ssl.SSLSocketFactory) javax.net.ssl.SSLSocketFactory.getDefault()

Which triggers WebSphere to override SSL connection handling. 这会触发WebSphere覆盖SSL连接处理。

Any ideas on what I'm missing are greatly appreciated - thanks. 对我所缺少的任何想法都非常感谢 - 谢谢。

You cant. 你不能。 javax.net.SocketFactory is incompatible with NIO based i/o models. javax.net.SocketFactory与基于NIO的i / o模型不兼容。 There is no way around using SSLContext for non-blocking SSL. 无法使用SSLContext进行非阻塞SSL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM