avax.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
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Unknown Source)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(Unknown Source)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.writeRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.AppOutputStream.write(Unknown Source)
at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
at java.io.BufferedOutputStream.flush(Unknown Source)
at org.apache.commons.httpclient.methods.EntityEnclosingMethod.writeRequestBody(EntityEnclosingMethod.java:506)
at org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase.java:2114)
at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:1096)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:398)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
at com.leg3s.comm.utils.AppHttpClient.execute(AppHttpClient.java:82)
at com.leg3s.comm.utils.AppHttpClient.executeHttpMethod(AppHttpClient.java:63)
at com.leg3s.pay.ring.utils.RingPayThread.run(RingPayThread.java:43)
at java.lang.Thread.run(Unknown Source)
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.validator.PKIXValidator.doBuild(Unknown Source)
at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)
at sun.security.validator.Validator.validate(Unknown Source)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.validate(Unknown Source)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
... 21 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)
at java.security.cert.CertPathBuilder.build(Unknown Source)
... 27 more在用httpClient访问网站的时候出现以上错误,在网上搜了一下,
处理的方法大概是先获取证书生成相应文件,然后再加载。
我想问一下httpClient的自动处理的方法,或者有没有相关的组件可以自动处理?

解决方案 »

  1.   

    楼主,你这是缺证书,证书可以通过IE来获得:
      1. 用IE打开需要连接的https网址,会弹出如下对话框:
      2. 单击"View Certificate",在弹出的对话框中选择"Details",然后再单击"Copy to File",根据提供的向导生成待访问网页的证书文件
      3. 向导第一步,欢迎界面,直接单击"Next",
      4. 向导第二步,选择导出的文件格式,默认,单击"Next",
      5. 向导第三步,输入导出的文件名,输入后,单击"Next",
      6. 向导第四步,单击"Finish",完成向导
      7. 最后弹出一个对话框,显示导出成功
      用keytool工具把刚才导出的证书倒入本地keystore。Keytool命令在<java-home>\bin\下,打开命令行窗口,并到<java-home>\lib\security\目录下,运行下面的命令:
      keytool -import -noprompt -keystore cacerts -storepass changeit -alias yourEntry1 -file your.cer
      其中参数alias后跟的值是当前证书在keystore中的唯一标识符,但是大小写不区分;参数file后跟的是刚才通过IE导出的证书所在的路径和文件名;如果你想删除刚才导入到keystore的证书,可以用命令:
      keytool -delete -keystore cacerts -storepass changeit -alias yourEntry1
     
    添加证书后就能解决问题。
      

  2.   

    我想问一下httpClient的自动处理的方法,或者有没有相关的组件可以自动处理?一楼正解,没有自动处理。
    你运行httpclient的时候指定-Djavax.net.ssl.trustStore=$keyfile_path就行了
    注意keyfile_path按照一楼说的 把证书导进去
      

  3.   

    问题已经解决,感谢楼上两位的帮忙,你们说的我都知道,
    我的意思HttpClient里有没有相关的类,可以获取请求时弹出的SLL证书的信息。
    这样我就可以获取SLL证书信息后,把相关的信息加入该次请求中。
    具体的方法我已经实现,主要是通过实现HttpClient的SecureProtocolSocketFactory接口,
    然后把相关的信息加入HttpClient的协议中。
      

  4.   

    目前的HttpClient没有自动获取的SSL证书的方法,需要自己写,我自己也封装好写了一个,以后要用的时候只要一两行代码就搞定了。
      

  5.   

    能分享下吗,小菜鸟不会,导入jdk证书库我倒是会,但是应用部署到几台pc上了,jkd版本可能不一样,最好的解决方法当然是代码加载证书.