SSLHandshakeException - PKIX路径构建失败

时间:2016-05-15 03:19:57

标签: java ssl pkix

我正在尝试通过以下代码连接到服务器

    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        public void checkClientTrusted(X509Certificate[] certs,
                String authType) {

        }

        public void checkServerTrusted(X509Certificate[] certs,
                String authType) {

        }

    } };

    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

    // Create all-trusting host name verifier
    HostnameVerifier allHostsValid = new HostnameVerifier() {
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    };

    // Install the all-trusting host verifier
    HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
    /*
     * end of the fix
     */
    // logger.info(input);
    String responseString = "";
    List<Object> providers = new ArrayList<Object>();
    providers.add(new String());

    WebClient client = WebClient
            .create("https://1.2.3.4:8443/api/methodName");

    WebClient.getConfig(client).getHttpConduit().getClient()
            .setConnectionTimeout(5000);

    System.out.println("input : " + requestString);
    client.header("username", new Object[] { headerUsername });
    client.header("password", new Object[] { headerPassword });
    client.header("Authrorization", new Object[] { headerAuth });
    Response response = client.accept(new String[] { "application/json" })
            .type("application/json").post(requestString);
    if (response.getStatus() != 200) {
        throw new Exception("Failed : HTTP error code : "
                + response.getStatus());
    }
    responseString = IOUtils.toString((InputStream) response.getEntity(),
            "UTF-8");
    System.out.println("response : " + responseString);

我得到以下例外:

javax.ws.rs.client.ClientException: javax.ws.rs.client.ClientException: javax.net.ssl.SSLHandshakeException: SSLHandshakeException invoking https://1.2.3.4:8443/api/methodName: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at org.apache.cxf.jaxrs.client.WebClient.doResponse(WebClient.java:1108)
    at org.apache.cxf.jaxrs.client.WebClient.doChainedInvocation(WebClient.java:1059)
    at org.apache.cxf.jaxrs.client.WebClient.doInvoke(WebClient.java:865)
    at org.apache.cxf.jaxrs.client.WebClient.doInvoke(WebClient.java:839)
    at org.apache.cxf.jaxrs.client.WebClient.invoke(WebClient.java:299)
    at org.apache.cxf.jaxrs.client.WebClient.post(WebClient.java:308)
    at com.evampsaanga.phoenix.esb.client.RestClient.callThirdPartyService(RestClient.java:151)
    at com.evampsaanga.phoenix.esb.modules.blacklisting.FileProcessor.callM3(FileProcessor.java:57)
    at com.evampsaanga.phoenix.esb.modules.blacklisting.FileProcessor.processFile(FileProcessor.java:31)
    at com.evampsaanga.phoenix.esb.modules.blacklisting.BlacklistingMain.doProcess(BlacklistingMain.java:29)
    at com.evampsaanga.phoenix.esb.modules.ModuleRunner.doProcess(ModuleRunner.java:18)
    at sun.reflect.GeneratedMethodAccessor41.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.camel.component.bean.MethodInfo.invoke(MethodInfo.java:407)
    at org.apache.camel.component.bean.MethodInfo$1.doProceed(MethodInfo.java:278)
    at org.apache.camel.component.bean.MethodInfo$1.proceed(MethodInfo.java:251)
    at org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:166)
    at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:72)
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:398)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
    at org.apache.camel.component.timer.TimerConsumer.sendTimerExchange(TimerConsumer.java:139)
    at org.apache.camel.component.timer.TimerConsumer$1.run(TimerConsumer.java:64)
    at java.util.TimerThread.mainLoop(Timer.java:555)
    at java.util.TimerThread.run(Timer.java:505)
Caused by: javax.ws.rs.client.ClientException: javax.net.ssl.SSLHandshakeException: SSLHandshakeException invoking https://1.2.3.4:8443/api/methodName: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at org.apache.cxf.jaxrs.client.AbstractClient.checkClientException(AbstractClient.java:575)
    at org.apache.cxf.jaxrs.client.AbstractClient.preProcessResult(AbstractClient.java:557)
    at org.apache.cxf.jaxrs.client.WebClient.doResponse(WebClient.java:1103)
    ... 25 more
Caused by: javax.net.ssl.SSLHandshakeException: SSLHandshakeException invoking https://1.2.3.4:8443/api/methodName: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.reflect.GeneratedConstructorAccessor66.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.mapException(HTTPConduit.java:1339)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1323)
    at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
    at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:628)
    at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:272)
    at org.apache.cxf.jaxrs.client.AbstractClient.doRunInterceptorChain(AbstractClient.java:634)
    at org.apache.cxf.jaxrs.client.WebClient.doChainedInvocation(WebClient.java:1058)
    ... 24 more
Caused by: 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
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1904)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:279)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:273)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1446)
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:209)
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:901)
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:837)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1023)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1332)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1359)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1343)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
    at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1092)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250)
    at org.apache.cxf.transport.http.URLConnectionHTTPConduit$URLConnectionWrappedOutputStream.setupWrappedStream(URLConnectionHTTPConduit.java:174)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleHeadersTrustCaching(HTTPConduit.java:1283)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.onFirstWrite(HTTPConduit.java:1239)
    at org.apache.cxf.transport.http.URLConnectionHTTPConduit$URLConnectionWrappedOutputStream.onFirstWrite(URLConnectionHTTPConduit.java:201)
    at org.apache.cxf.io.AbstractWrappedOutputStream.write(AbstractWrappedOutputStream.java:47)
    at org.apache.cxf.io.AbstractThresholdOutputStream.write(AbstractThresholdOutputStream.java:69)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1296)
    ... 30 more
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(PKIXValidator.java:385)
    at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
    at sun.security.validator.Validator.validate(Validator.java:260)
    at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:326)
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:231)
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:126)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1428)
    ... 48 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:196)
    at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:268)
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:380)
    ... 54 more

我尝试通过以下方式在JDK中安装其他服务器的证书:Following this video on youtube

  

openssl s_client -connect 1.2.3.4:8443

然后复制CERTIFICATE并粘贴名为client.pem的文件。 之后,我转到Java_home / bin并运行以下命令:

  

./ keystore -keystore clientcert - importcert -alias demo -file   /home/test/client.pem

输入密钥库密码并收到“证书已添加到密钥库”

的消息

最后,当我尝试运行以下内容时:

  

java -Djavax.net.ssl.trustStore = clientcert   -Djavax.net.ssl.trustStorePassword = 123456客户端

然后我得到的消息是:

  

错误:无法找到或加载主类客户端

你能否告诉我,我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

您是否正在从java_home / bin运行命令“java -Djavax.net.ssl.trustStore = clientcert -Djavax.net.ssl.trustStorePassword = 123456 Client”?

如果是这样,它找不到您的“客户”类。请从您的工作文件夹(Client类)再次尝试该命令,或者在命令中将类路径提供给Client。