gpt4 book ai didi

java - JDK 11 HttpClient 抛出 "No subject alternative DNS name"错误

转载 作者:行者123 更新时间:2023-12-04 17:44:19 36 4
gpt4 key购买 nike

我正在编写一个基于 JDK11 HttpClient 的简单 REST api 客户端,简单代码如下:

public class MyClass {

private static final X509TrustManager TRUST_MANAGER = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs, String string) {
}

public void checkServerTrusted(X509Certificate[] xcs, String string) {
}

public X509Certificate[] getAcceptedIssuers() {
return null;
}
};

private static HttpClient getNewHttpClient() {
int timeout = 600;
try {

HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true);

SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(null, new TrustManager[]{TRUST_MANAGER}, new SecureRandom());

// Install the all-trusting host verifier
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());

//Set SSL parameters
SSLParameters parameters = new SSLParameters();
parameters.setEndpointIdentificationAlgorithm("HTTPS");
HttpClient httpClient = HttpClient.newBuilder()
.connectTimeout(Duration.ofMillis(timeout * 1000))
.sslContext(sslContext)
.sslParameters(parameters)
.build();
return httpClient;
} catch (Exception e) {
logger.warn("Unable to create HttpClient with disabled SSL Certificate verifying, default client will be used", e);
return HttpClient.newHttpClient();
}
}

public static void main(String[] args) {
HttpRequest requestBuilder = HttpRequest.newBuilder()
.uri(URI.create("https://somehostname.xx.xxx.net"))
.GET()
.build();

getNewHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
}
}

问题是,当我尝试打开某个 SSL 域时,出现错误:

Caused by: java.security.cert.CertificateException: No subject alternative DNS name matching somehostname.xx.xxx.net found.



我怎么解决这个问题?

最佳答案

虽然完全关闭主机名验证(即对于整个 VM)可能是一种选择,但它可能很危险。
如果您从 javax.net.ssl.X509ExtendedTrustManager 派生您的 TRUST_MANAGER,您还可以禁用主机名验证。

    private static final TrustManager DUMMY_TRUST_MANAGER = new X509ExtendedTrustManager() {        
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[0];
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException { }
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException { }
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine) throws CertificateException { }
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket) throws CertificateException { }
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngine engine) throws CertificateException { }
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType, Socket socket) throws CertificateException { }
};
当然,这也将完全禁用对提供给您的客户的证书的任何检查。
https://bugs.openjdk.java.net/browse/JDK-8213309 上还有一个 Unresolved 问题向JDK添加API以专门关闭主机名验证。如果您也可以访问 openjdk 错误数据库,则可以为该问题投票。

关于java - JDK 11 HttpClient 抛出 "No subject alternative DNS name"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52856027/

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