'peer not authenticated'SSL证书错误使用DefaultHttpClient

时间:2014-05-20 07:58:11

标签: java keytool

我有一个应用程序jar,我使用DefaultHTTPClient类对象调用 HTTPS url但是它给 peer not authenticate exception ,所以我想使用keystore对jar进行签名

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

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

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

更新

根据@Duncan,我可以通过引用This Link.在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

在此之后我输入'y'来信任证书

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

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

Stack如下:

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)

我的代码是:

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();
}

1 个答案:

答案 0 :(得分:3)

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

根据@Duncan(评论)我可以通过引用This Link来导入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

在此之后我进入了' y'信任证书

  

相信这个证书? [不]:你   证书已添加到密钥库