gpt4 book ai didi

java-8 - 如何使用 javax.net.ssl.SSLContext 设置密码套件

转载 作者:行者123 更新时间:2023-12-03 23:53:18 27 4
gpt4 key购买 nike

在使用 java8 的 Spring Boot 应用程序中,我将 httpClient 连接的底层 SSLConext 设置如下:

 import javax.net.ssl.SSLContext;

SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(null, null, null);

CloseableHttpClient httpClient = HttpClientBuilder
.create()
.setConnectionManager(myConnectionManager)
.setDefaultRequestConfig(rqConfig)
.setSSLContext(sslContext)
.build();

我需要将底层 TLS1.2 安全连接的密码套件设置为我选择的更强大的东西。我看不到用我在代码中创建 sslContext 的方式来做到这一点。

有人可以帮我用我的 sslContext 设置密码套件吗?

================更新=================
 This is how I have now created my HttpClient

CloseableHttpClient httpClient = HttpClientBuilder
.create()
.setConnectionManager(myConnectionManager)
.setDefaultRequestConfig(rqConfig)
.setSSLSocketFactory(new SSLConnectionSocketFactory(
SSLContexts.createSystemDefault(),
new String[]{"TLSv1.2"},
new String[] {"some-gibberish-cipher-suite"},
SSLConnectionSocketFactory.getDefaultHostnameVerifier()))
.build();

最佳答案

创建自定义 SSLConnectionSocketFactory 时可以指定首选 TLS 协议(protocol)版本和自定义密码实例

CloseableHttpClient client = HttpClients.custom()
.setSSLSocketFactory(new SSLConnectionSocketFactory(
SSLContexts.createSystemDefault(),
new String[]{"TLSv1.2"},
new String[] {"TLS_RSA_WITH_AES_256_CBC_SHA256"},
SSLConnectionSocketFactory.getDefaultHostnameVerifier()))
.build();
try (CloseableHttpResponse response = client.execute(new HttpGet("https://httpbin.org/"))) {
System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
}

或者,可以创建自定义 PoolingHttpClientConnectionManager具有所需 SSL 配置的实例。
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", new SSLConnectionSocketFactory(
SSLContexts.createSystemDefault(),
new String[]{"TLSv1.2"},
new String[]{"TLS_RSA_WITH_AES_256_CBC_SHA256"},
SSLConnectionSocketFactory.getDefaultHostnameVerifier()))
.build());

CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(cm)
.build();
try (CloseableHttpResponse response = client.execute(new HttpGet("https://httpbin.org/"))) {
System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
}

关于java-8 - 如何使用 javax.net.ssl.SSLContext 设置密码套件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53953100/

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