gpt4 book ai didi

java - 如何修复 "java.security.cert.CertificateException: No subject alternative names present"错误?

转载 作者:IT老高 更新时间:2023-10-28 11:32:06 31 4
gpt4 key购买 nike

我有一个 Java Web 服务客户端,它通过 HTTPS 使用 Web 服务。

import javax.xml.ws.Service;

@WebServiceClient(name = "ISomeService", targetNamespace = "http://tempuri.org/", wsdlLocation = "...")
public class ISomeService
extends Service
{

public ISomeService() {
super(__getWsdlLocation(), ISOMESERVICE_QNAME);
}

当我连接到服务 URL (https://AAA.BBB.CCC.DDD:9443/ISomeService ) 时,我收到异常 java.security.cert.CertificateException:没有主题替代名称存在

要修复它,我首先运行 openssl s_client -showcerts -connect AAA.BBB.CCC.DDD:9443 > certs.txt 并在文件 certs.txt:

CONNECTED(00000003)
---
Certificate chain
0 s:/CN=someSubdomain.someorganisation.com
i:/CN=someSubdomain.someorganisation.com
-----BEGIN CERTIFICATE-----
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
-----END CERTIFICATE-----
---
Server certificate
subject=/CN=someSubdomain.someorganisation.com
issuer=/CN=someSubdomain.someorganisation.com
---
No client certificate CA names sent
---
SSL handshake has read 489 bytes and written 236 bytes
---
New, TLSv1/SSLv3, Cipher is RC4-MD5
Server public key is 512 bit
Compression: NONE
Expansion: NONE
SSL-Session:
Protocol : TLSv1
Cipher : RC4-MD5
Session-ID: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Session-ID-ctx:
Master-Key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Key-Arg : None
Start Time: 1382521838
Timeout : 300 (sec)
Verify return code: 21 (unable to verify the first certificate)
---

AFAIK,现在我需要

  1. 提取 -----BEGIN CERTIFICATE----------END CERTIFICATE-- 之间的 certs.txt 部分---,
  2. 修改它,使证书名称等于AAA.BBB.CCC.DDD
  3. 然后使用 keytool -importcert -file fileWithModifiedCertificate 导入结果(其中 fileWithModifiedCertificate 是操作 1 和 2 的结果)。

这对吗?

如果是这样,我如何才能使步骤 1 中的证书与基于 IP 的地址 (AAA.BBB.CCC.DDD) 一起使用?

更新 1(23.10.2013 15:37 MSK): 回复 similar question ,我阅读了以下内容:

If you're not in control of that server, use its host name (provided that there is at least a CN matching that host name in the existing cert).

“使用”到底是什么意思?

最佳答案

我通过使用 here 提供的方法禁用 HTTPS 检查来解决此问题:

我将以下代码放入 ISomeService 类中:

static {
disableSslVerification();
}

private static void disableSslVerification() {
try
{
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};

// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};

// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
}

由于我使用 https://AAA.BBB.CCC.DDD:9443/ISomeService 仅用于测试目的,这是一个足够好的解决方案,但不要在生产中这样做。

请注意,您也可以为“一次一个连接”禁用 SSL,例如:

 // don't call disableSslVerification but use its internal code:
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if (conn instanceof HttpsURLConnection) {
HttpsURLConnection httpsConn = (HttpsURLConnection) conn;
httpsConn.setHostnameVerifier(allHostsValid);
httpsConn.setSSLSocketFactory(sc.getSocketFactory());
}

关于java - 如何修复 "java.security.cert.CertificateException: No subject alternative names present"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19540289/

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