Java 'peer not authentication' SSL 证书错误使用 DefaultHttpClient

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23754081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:02:01  来源:igfitidea点击:

'peer not authenticated' SSL certificate error usng DefaultHttpClient

javakeytool

提问by Amogh

I have an application jar from which I m calling an HTTPS url using DefaultHTTPClient classobject but its giving peer not authenticate exception, so I want to sign jar using keystore.

我有一个应用程序 jar,我从中使用 DefaultHTTPClient 类对象调用HTTPS url,但它给对等方未验证异常,所以我想使用密钥库对 jar 进行签名。

I have .cer file which has public key and I can able to import into keystore but when I use jarsigner tool it says certificate chain not found.you must have private key and associate public key.

我有一个带有公钥的 .cer 文件,我可以导入到密钥库中,但是当我使用 jarsigner 工具时,它说没有找到证书链。你必须有私钥和关联公钥。

I have .pfx file also which is suppose to an private key but I don't know how to import it.can any one able to tell me the steps how to import .pfx file and use in jarsigner.

我也有 .pfx 文件,它假设是一个私钥,但我不知道如何导入它。谁能告诉我如何导入 .pfx 文件并在 jarsigner 中使用的步骤。

Correct me if I m wrong somewhere..

如果我在某处错了,请纠正我..

UPDATE

更新

As per @Duncan I am able to import .cer file in JVM by referring This Link.. I used bellowed command to import .cer into cacerts

根据@Duncan,我可以通过引用此链接在 JVM 中导入 .cer 文件. 我使用 bellowed 命令将 .cer 导入 cacerts

c:\Program Files\Java\jre7\bin>keytool -importcert -alias esbcert -file "e:\Desktop\esbcert\esb.cer" -keystore "c:\Program Files\Java\jre7\lib\security\cacerts" -storepass changeit

c:\Program Files\Java\jre7\bin>keytool -importcert -alias esbcert -file "e:\Desktop\esbcert\esb.cer" -keystore "c:\Program Files\Java\jre7\lib\security\cacerts" -storepass changeit

After this I entered 'y' to trust the certificate

在此之后,我输入“y”以信任证书

Trust this certificate? [no]: y Certificate was added to keystore

Trust this certificate? [no]: y Certificate was added to keystore

After that I Run my application but it still gives me javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated

之后我运行我的应用程序,但它仍然给我 javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated

Stack is as follows :

堆栈如下:

javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
        at sun.security.ssl.SSLSessionImpl.getPeerCertificates(Unknown Source)
        at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.jav
a:126)
        at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFact
ory.java:572)
        at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnect
ion(DefaultClientConnectionOperator.java:180)
        at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedCli
entConnectionImpl.java:294)
        at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(Default
RequestDirector.java:645)
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultReq
uestDirector.java:480)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpCl
ient.java:906)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpCl
ient.java:805)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpCl
ient.java:784)
        at testhttps.TestHTTPS.testWithMKCLHTTPClient(TestHTTPS.java:95)
        at testhttps.TestHTTPS.main(TestHTTPS.java:49)

My Code is :

我的代码是:

String url = "https://domain.org/webapp/transformer/doTransformer/doReg";
try {


    HttpPost postRequest = new HttpPost(url);       
    HttpResponse httpResponse = null;

    DefaultHttpClient httpClient = new DefaultHttpClient();     
    httpResponse = httpClient.execute(postRequest);             
} catch (Exception e) {         
    e.printStackTrace();
}

采纳答案by Amogh

This exceptions tell that connection made to server URL is not from authenticated client. To resolve this issue we have to import server's public certificate in jreon which java application is runnering to import certificate follow these steps:

此异常表明对服务器 URL 的连接不是来自经过身份验证的客户端。要解决此问题,我们必须导入服务器的公共证书,jre其中 java 应用程序正在运行以导入证书,请执行以下步骤:

As per @Duncan (comment) I am able to import .cer file in JVM by referring This Link.. I used bellowed command to import .cer into cacerts

根据@Duncan(评论),我可以通过引用此链接在 JVM 中导入 .cer 文件。我使用 bellowed 命令将 .cer 导入到 cacerts

c:\Program Files\Java\jre7\bin>keytool -importcert -alias esbcert -file "e:\Desktop\esbcert\esb.cer" -keystore "c:\Program Files\Java\jre7\lib\security\cacerts" -storepass changeit

c:\Program Files\Java\jre7\bin>keytool -importcert -alias esbcert -file "e:\Desktop\esbcert\esb.cer" -keystore "c:\Program Files\Java\jre7\lib\security\cacerts " -storepass changeit

After this I entered 'y' to trust the certificate

在此之后,我输入“y”以信任证书

Trust this certificate? [no]: y Certificate was added to keystore

信任这个证书?[no]:y 证书已添加到密钥库