gpt4 book ai didi

Java - 标准 SSL 证书全信任代码失败

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

我认为到目前为止,几乎每个有过 SSL 证书信任错误经验的 Java 编码人员都使用过或至少遇到过以下代码:

    // Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts;
trustAllCerts = new TrustManager[] {
new X509TrustManager() {
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
@Override
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() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
};

// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);

到目前为止,我已经成功地使用此代码在通过其 IP 地址访问 HTTPs 站点时忽略证书不匹配(不要问我为什么这样做,这是一个完全不同的故事)。然而,我尝试对另一个 HTTPs 站点做同样的事情,发现这段代码失败并出现以下异常:

javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: Certificates does not conform to algorithm constraints

那么,问题是出了什么问题,我能做些什么呢?

最佳答案

我已经解决了这个问题。看起来服务器在其证书中使用了 MD2。到目前为止,我知道的唯一解决这个问题的方法是在 JDK 路径中找到 jre/lib/security/java.security 文件并将 jdk.certpath.disabledAlgorithms=MD2 更改为 jdk.certpath.disabledAlgorithms= 以便 MD2 算法'禁用。这看起来真的很糟糕,但遗憾的是,我找不到以编程方式启用 MD2 的方法。

回想起来,我现在记得我可以访问它,但只能在我更新 Java 之前访问它,这会终止 java.security 文件编辑。

关于Java - 标准 SSL 证书全信任代码失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23973937/

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