Exception in thread "main" javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No name matching www.baidu.com found at sun.security.ssl.Alerts.getSSLException(Alerts.java:192) at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949) at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302) at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296) at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1509) at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
时间: 2023-12-16 18:04:55 浏览: 63
这是一个 SSL 握手异常错误,可能是由于 SSL 握手过程中出现了证书不匹配的问题,导致连接无法建立。具体来说,错误信息中显示证书中的主机名与要连接的主机名不匹配,这可能是由于证书过期、证书颁发机构不受信任或证书中的主机名与要连接的主机名不匹配等原因导致的。解决这个问题的方法是检查证书是否合法,并确保证书中的主机名与要连接的主机名一致。如果证书存在问题,可以联系证书颁发机构进行处理,如果证书正确无误,可以考虑在代码中添加信任证书的代码来解决问题。
相关问题
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException:
javax.net.ssl.SSLHandshakeException是一个常见的SSL握手异常,通常是由于SSL证书问题引起的。sun.security.validator.ValidatorException是一个证书验证异常,通常是由于证书无效或未被信任引起的。PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException是一个证书路径构建异常,通常是由于证书链中的某个证书无效或未被信任引起的。如果您遇到了这些异常,您可以尝试以下解决方案:
1.检查证书是否过期或被吊销。
2.检查证书是否由受信任的CA颁发。
3.检查证书链中的所有证书是否都有效且被信任。
4.尝试更新证书或使用新的证书。
5.尝试禁用SSL证书验证,但这不是一个安全的解决方案。
以下是一个Java代码示例,演示如何处理javax.net.ssl.SSLHandshakeException异常:
```java
try {
// your SSL/TLS connection code here
} catch (javax.net.ssl.SSLHandshakeException e) {
if (e.getCause() instanceof java.security.cert.CertificateException) {
// handle certificate exception
} else if (e.getCause() instanceof java.security.cert.CertPathValidatorException) {
// handle certificate path validation exception
} else {
// handle other SSL handshake exception
}
}
```
https javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException:
引用:HTTP Status 500 - javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find ...
引用:Java Spring应用发送数据报如下问题。 javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 原因分析:
引用:用httpclient访问https资源时,会出现异常,与环境也有关系,有些机器请求正常。 解决方案:
在HTTPS通信中,当Java程序尝试与服务端建立安全连接时,会进行SSL握手过程。如果在握手过程中出现异常javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException,可能是由于证书验证失败导致的。这种异常通常有两种原因:一是服务端证书不被信任,二是客户端无法找到合适的证书链。
解决这个问题的方法有以下几种:
1. 信任自签名证书:可以通过自定义TrustManager来信任自签名的证书。但这种方法存在安全风险,因为所有的自签名证书都会被信任。
2. 导入服务端证书:可以将服务端的证书导入到Java的信任证书库中,以确保它被信任。可以使用keytool工具将证书导入到Java信任库中。
3. 禁用证书验证:在开发环境中,可以禁用证书验证来避免这个问题。但在生产环境中不建议这样做,因为会降低通信的安全性。