为什么我收到异常 javax.net.ssl.SSLPeerUnverifiedException:对等点未经过身份验证?

2024-01-03

我正在使用 Apache HttpComponents HttpClient(4.0.1) 进行 HTTPS 调用,但我将此异常作为响应:

 javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
        at com.sun.net.ssl.internal.ssl.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:345)
        at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:128)
        at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:390)
        at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:148)
        at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:149)
        at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:121)
        at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:561)
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:415)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)

我提供了所有必需的参数。目标系统不需要任何用户名/密码或代理,但它包含安装在服务器中的 JKS csrtificates。用户名和密码为空值。

这是与org.apache.commons.httpclient.methods.PostMethod- 版本 3.0 - commons-httpclient-3.0.jar 现在我们已经实现了org.apache.http.client.methods.HttpPost- 版本 4.0.1 - commons-httpclient.jar

这是不起作用的示例代码片段:

HttpParams param = new BasicHttpParams();
HttpProtocolParams.setVersion(param, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(param, "UTF-8");
HttpProtocolParams.setUseExpectContinue(param, true);
DefaultHttpClient httpClient = new DefaultHttpClient(param);
httpClient.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT,10000)));
httpClient.getParams().setParameter(HttpConnectionParams.SO_TIMEOUT,10000)));
httpClient.getCredentialsProvider().setCredentials(new AuthScope(<HOST IP,PORT)),
    AuthScope.ANY_REALM),
    new UsernamePasswordCredentials("", ""));

try {
HttpPost httpPost = new HttpPost(END POINT URL);
StringEntity requestEntity = new StringEntity(inputString, "text/xml", "UTF-8");
httpPost.setEntity(requestEntity);
response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
if (null != responseEntity) 
{
    responseBody = EntityUtils.toString(responseEntity);
}
if (null != httpPost.getURI()) {
    url = httpPost.getURI().toString();
}
} catch (IOException e) {
    e.printStackTrace();
} finally {
   httpClient.getConnectionManager().shutdown();
}

异常消息

javax.net.ssl.SSLPeerUnverifiedException:对等点未经过身份验证

并不总是表明问题的根本原因。 您可能需要通过添加 Java VM 参数来启用 SSL 握手调试-Djavax.net.debug=ssl:handshake。 添加后,您将收到更多有用的错误消息。 如果远程服务器使用不受信任的证书,您将看到以下错误消息:

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

如果是这种情况,那么@Abhishek 的回答将解决该问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么我收到异常 javax.net.ssl.SSLPeerUnverifiedException:对等点未经过身份验证? 的相关文章

随机推荐