gpt4 book ai didi

java - 在 Apache HttpAsyncClient 中配置 SSL

转载 作者:太空宇宙 更新时间:2023-11-03 12:51:18 25 4
gpt4 key购买 nike

我需要设置一个支持 SSL 的 Apache HTTPAsyncClient。我使用此代码,但它似乎不起作用(获取“javax.net.ssl.SSLException:收到致命警报:handshake_failure”)

    System.setProperty("javax.net.debug", "ssl,handshake");
System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", "true");

KeyStore ts = KeyStore.getInstance("JKS");
ts.load(loadStream("C:/TrustStore/cacerts"), "trustpass".toCharArray());
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(loadStream("C:/KeyStore/SSL/keystore.SomeKey"), "keypass".toCharArray());

SSLContextBuilder sslBuilder = SSLContexts.custom().loadTrustMaterial(ts).loadKeyMaterial(ks, "somekey".toCharArray()).setSecureRandom(new SecureRandom());
SSLContext ssl = sslBuilder.build();

PoolingNHttpClientConnectionManager cm = new PoolingNHttpClientConnectionManager(new DefaultConnectingIOReactor(IOReactorConfig.DEFAULT));

CloseableHttpAsyncClient clientHttps = HttpAsyncClientBuilder.create()
.setConnectionManager(cm)
.setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
.setSSLContext(ssl)
.build();

RequestConfig.Builder b = RequestConfig.custom();
b.setProxy(new HttpHost("proxyHost", proxyPort));
RequestConfig rc = b.build();

clientHttps.start();

HttpRequestBase req = new HttpPost("https://someurl");
((HttpEntityEnclosingRequestBase)req).setEntity(new StringEntity("somestring"));
req.setConfig(rc);

clientHttps.execute(req, new FutureCallback<HttpResponse>() {

@Override
public void failed(Exception ex) {
System.out.println(ex);
}

@Override
public void completed(HttpResponse result) {
System.out.println(result);
}

@Override
public void cancelled() {
System.out.println("Cancelled");
}
});

当使用 javax.net.ssl.HttpsURLConnection 实现此目的时,它有效(如果需要,我可以附上相关代码)。

编辑

根据@ben75 的回答,我终于用下面的代码让它运行了

System.setProperty("javax.net.debug", "ssl,handshake");
System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", "true");

KeyStore ts = KeyStore.getInstance("JKS");
ts.load(loadStream("C:/TrustStore/cacerts"), "trustpass".toCharArray());
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(loadStream("C:/KeyStore/SSL/keystore.SomeKey"), "keypass".toCharArray());

SSLContextBuilder sslBuilder = SSLContexts.custom().loadTrustMaterial(ts).loadKeyMaterial(ks, "somekey".toCharArray()).setSecureRandom(new SecureRandom());
SSLContext ssl = sslBuilder.build();

SSLIOSessionStrategy s = new SSLIOSessionStrategy(ssl, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
RegistryBuilder<SchemeIOSessionStrategy> rb = RegistryBuilder.create();
rb.register("https", s).register("http", NoopIOSessionStrategy.INSTANCE);
PoolingNHttpClientConnectionManager cm = new PoolingNHttpClientConnectionManager(new DefaultConnectingIOReactor(IOReactorConfig.DEFAULT), rb.build());

CloseableHttpAsyncClient clientHttps = HttpAsyncClientBuilder.create()
.setConnectionManager(cm)
.build();

RequestConfig.Builder b = RequestConfig.custom();
b.setProxy(new HttpHost("proxyHost", proxyPort));
RequestConfig rc = b.build();

clientHttps.start();

HttpRequestBase req = new HttpPost("https://someurl");
((HttpEntityEnclosingRequestBase)req).setEntity(new StringEntity("somestring"));
req.setConfig(rc);

clientHttps.execute(req, new FutureCallback<HttpResponse>() {

@Override
public void failed(Exception ex) {
System.out.println(ex);
}

@Override
public void completed(HttpResponse result) {
System.out.println(result);
}

@Override
public void cancelled() {
System.out.println("Cancelled");
}
});

最佳答案

(我最近(在 Android 上)遇到了非常相似的问题,但我猜你犯了和我一样的错误。)

当您显式设置连接管理器时:builder.setConnectionManager(cm) sslContext 将被忽略。

您可以做的是在 PoolingNHttpClientConnectionManager 中注入(inject)您的 SSLContext。

为此,您可以使用此构造函数: PoolingNHttpClientConnectionManager(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor, Registry iosessionFactoryRegistry)

iosessionFactoryRegistry 包含一个 SSLIOSessionStrategy使用您的 SSLContext 构建

关于java - 在 Apache HttpAsyncClient 中配置 SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28263055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com