【问题标题】:Spring Boot AsyncRestTemplate SSLSocketFactorySpring Boot AsyncRestTemplate SSLSocketFactory
【发布时间】:2016-05-18 04:17:04
【问题描述】:

在 WebSphere 中部署 Spring Boot 应用程序。由于它们的 SSL 配置,我们需要明确指定 SSLSocketFactory 以确保应用程序使用 WebSphere 证书而不是默认的 Java 密钥/信任存储。

通过 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);
}

但是使用 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);
}

本质上,我需要调用:

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

这会触发 WebSphere 覆盖 SSL 连接处理。

非常感谢任何关于我所缺少的想法 - 谢谢。

【问题讨论】:

    标签: java spring ssl apache-httpclient-4.x resttemplate


    【解决方案1】:

    你不能。 javax.net.SocketFactory 与基于 NIO 的 i/o 模型不兼容。没有办法将SSLContext 用于非阻塞 SSL。

    【讨论】:

      猜你喜欢
      • 2016-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-17
      • 2015-12-28
      • 2012-04-12
      相关资源
      最近更新 更多