使用 HTTPS 的 HttpGet:SSLPeerUnverifiedException
使用 HttpClient,尝试通过 HTTPS 进行通信时收到以下错误:
线程“main”中出现异常 javax.net.ssl.SSLPeerUnverifiedException:对等点未经过身份验证。
这是我的代码:
URI loginUri = new URI("https://myUrl.asp");
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet( loginUri );
HttpResponse response = httpclient.execute( httpget );
如何抑制或删除此错误?
Using HttpClient, I receive the following error when attempting to communicate over HTTPS:
Exception in thread "main" javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated.
Here is my code:
URI loginUri = new URI("https://myUrl.asp");
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet( loginUri );
HttpResponse response = httpclient.execute( httpget );
How do I suppress or remove this error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
注意:不要在生产代码中执行此操作,而是使用 http 或上面建议的实际自签名公钥。
在 HttpClient 4.xx 上:
Note: Do not do this in production code, use http instead, or the actual self signed public key as suggested above.
On HttpClient 4.xx:
这个答案遵循 owlstead 和 Mat 的回应。它适用于 SE/EE 安装,不适用于 ME/移动/Android SSL。
由于还没有人提到它,我将提及解决此问题的“生产方式”:
按照 HttpClient 中的 AuthSSLProtocolSocketFactory 类 来更新您的信任存储 &关键商店。
keytool -import -alias "my server cert" -file server.crt -keystore my.truststore
< code>keytool -genkey -v -alias "my client key" -validity 365 -keystore my.keystore
keytool -certreq -alias "my client key" -file mycertreq .csr -keystore my.keystore
(自签名或让您的证书签名)
导入受信任的 CA 根证书
keytool -import -alias "my Trusted ca" -file caroot.crt -keystore my.keystore
keytool - import -alias "my client key" -file mycert.p7 -keystore my.keystore
keytool -list -v -keystore my.keystore
如果不这样做拥有服务器证书,生成 JKS 格式的证书,然后将其导出为 CRT 文件。 来源:keytool 文档
This answer follows on to owlstead and Mat's responses. It applies to SE/EE installations, not ME/mobile/Android SSL.
Since no one has yet mentioned it, I'll mention the "production way" to fix this:
Follow the steps from the AuthSSLProtocolSocketFactory class in HttpClient to update your trust store & key stores.
keytool -import -alias "my server cert" -file server.crt -keystore my.truststore
keytool -genkey -v -alias "my client key" -validity 365 -keystore my.keystore
keytool -certreq -alias "my client key" -file mycertreq.csr -keystore my.keystore
(self-sign or get your cert signed)
Import the trusted CA root certificate
keytool -import -alias "my trusted ca" -file caroot.crt -keystore my.keystore
keytool -import -alias "my client key" -file mycert.p7 -keystore my.keystore
keytool -list -v -keystore my.keystore
If you don't have a server certificate, generate one in JKS format, then export it as a CRT file. Source: keytool documentation
使用 HttpClient 3.x,您需要执行以下操作:
EasySSLProtocolSocketFactory 的实现可以找到 此处。
Using HttpClient 3.x, you need to do this:
An implementation of EasySSLProtocolSocketFactory can be found here.
如果您的服务器基于 JDK 7 并且您的客户端基于 JDK 6 并使用 SSL 证书,则会出现此异常。在 JDK 7 中,默认禁用 sslv2hello 消息握手,而在 JDK 6 中,启用 sslv2hello 消息握手。因此,当您的客户端尝试连接服务器时,将向服务器发送 sslv2hello 消息,并且由于 sslv2hello 消息禁用,您将收到此异常。要解决此问题,您必须将客户端移至 JDK 7,或者必须使用 6u91 版本的 JDK。但要获取此版本的 JDK,您必须获取
This exception will come in case your server is based on JDK 7 and your client is on JDK 6 and using SSL certificates. In JDK 7 sslv2hello message handshaking is disabled by default while in JDK 6 sslv2hello message handshaking is enabled. For this reason when your client trying to connect server then a sslv2hello message will be sent towards server and due to sslv2hello message disable you will get this exception. To solve this either you have to move your client to JDK 7 or you have to use 6u91 version of JDK. But to get this version of JDK you have to get the
返回“secureClient”的方法(在 Java 7 环境中 - NetBeans IDE 和 GlassFish Server:端口 https 默认为 3920 ),希望这可以有所帮助:
Method returning a "secureClient" (in a Java 7 environnement - NetBeans IDE and GlassFish Server: port https by default 3920 ), hope this could help :
您的本地 JVM 或远程服务器可能没有所需的密码。转到这里
https://www.oracle.com/java/technologies/ javase-jce8-downloads.html
并下载包含以下内容的 zip 文件:
US_export_policy.jar
和
local_policy.jar
替换现有文件(您需要在 JVM 中找到现有路径)。
在 Mac 上,我的路就在这里。
/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/security
这对我有用。
Your local JVM or remote server may not have the required ciphers. go here
https://www.oracle.com/java/technologies/javase-jce8-downloads.html
and download the zip file that contains:
US_export_policy.jar
and
local_policy.jar
replace the existing files (you need to find the existing path in your JVM).
on a Mac, my path was here.
/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/security
this worked for me.