服务器名称指示(SNI)上的Java(Server Name Indication (SNI) on

2019-06-28 03:39发布

谁能帮我开始在开展与Java服务器名称指示HTTP连接?

我想请求一个网站我adminstering内容。 我一直在使用Apache的HttpClient库,但因为网站只使用SNI为HTTPS和SNI未在DefaultHttpClient使我的安全内容请求失败。 我找了指令如何Apache的HttpClient库内接近这一点,但我看到这个文件结束: http://hc.apache.org/httpclient-3.x/sslguide.html ,这是过时(指代码发回时的HttpClient和的HttpCore是Apache的公共包的一部分)。

所以...任何帮助吗?

Answer 1:

你可能想跟踪https://issues.apache.org/jira/browse/HTTPCLIENT-1119

Java 7中的潜在客户端实现能够支持它,并公开通过SSLSocketImpl#setHost功能(通过sun.net.www.protocol.https.HttpsClient称为

在Java 7中使用

    new URL("https://cmbntr.sni.velox.ch/").openStream()

直到HttpClient的-1119是固定



Answer 2:

这是我如何org.apache.httpcomponents的HttpClient的V4.3做到了+

private HttpClientConnectionManager createConnectionManager(final SSLContext ctx) {
    LOG.info("Creating sslConnectionSocketFactory");
    final SSLConnectionSocketFactory sslSF = new SSLConnectionSocketFactory(ctx) {

        @Override
        protected void prepareSocket(SSLSocket socket) throws IOException {
            try {
                System.out.println("************ setting socket HOST property *************");
                PropertyUtils.setProperty(socket, HOST, Constants.SNI_HOST);
            } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException ex) {
                LOG.error(ex.getMessage());
            }
            super.prepareSocket(socket); 
        }

    };

    LOG.info("Creating connectionRegistry");
    final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("https", sslSF)
            .build();

    LOG.info("Creating poolingConnectionManager");
    final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
    connectionManager.setDefaultMaxPerRoute(MAX_CONNECTIONS_PER_ROUTE);
    connectionManager.setMaxTotal(MAX_CONNECTIONS);

    return connectionManager;
}

这是我如何创建HttpClient

final KeyManager[] keyManagers = createKeyManagers();
final TrustManager[] trustManagers = createTrustManagers();
final SSLContext ctx = createSslContext(keyManagers, trustManagers);

final HttpClientConnectionManager connectionManager = createConnectionManager(ctx);

LOG.info("Creating httpClient");
HttpClient httpClient = HttpClients
        .custom()
        .setConnectionManager(connectionManager)
        .build();


Answer 3:

与短期修正为下描述: TLS与SNI在Java客户端 ,可以SNI服务器支持添加到JDK 7,并用X509ExtendedKeyManager使用它的一起。



Answer 4:

什么工作对我来说是配置ServerName在Apache配置正确:

/etc/apache2/sites-avaible/default

<VirtualHost *:443>
  ServerName foo.domain.com
  ...
</VirtualHost>

就像在说https://stackoverflow.com/a/8058839/2088282 。



Answer 5:

看来,这个问题是在Java 7中 。



文章来源: Server Name Indication (SNI) on Java
标签: java http ssl sni