2016-04-06 11 views
0

HTTPComponentsを使用して特定のWebサービスにカスタムSOAPメッセージを送信しようとしています。私たちは通信に証明書を使用し、Webサービスエンドポイントはhttps暗号化のための証明書を使用します。Java HTTPComponents SOAPリクエスト、SSLHandshake例外(PKIXパス構築に失敗しました)

私はSOAPメッセージを送っしようとしているが、私は、エラーがスローされhttppostを実行する時点で、カスタムJavaアクション経由:

Executing request POST https://webserviceurl/endpoint HTTP/1.1 
Exception While Connecting sun.security.validator.ValidatorException: PKIX path building failed:  sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested  target 
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) 

私はいくつかのこの問題に関する研究と明らかにJavaのニーズをしましたがcacerts内のWebサービスURLに使用される証明書。 InstallCertというJavaツールを使用して、証明書をcacertsに追加しようとしましたが、このエラーが発生します。

PS:keytoolを使ってcacertsに証明書を追加しようとしましたが、幸運もありませんでした。

なぜ接続が失敗したのですか? cacertsファイルにエンドポイント証明書を追加する必要がありますか?この問題を解決する方法のアイデア/信頼できる証明書に証明書を追加する方法

ここに要求し、私の完全なJavaコードです:

// Trust own CA and all self-signed certs 
    SSLContext sslcontext = SSLContexts.custom() 
      .loadTrustMaterial(new File("C:/ourcert.p12"), "MyPass".toCharArray(), 
        new TrustSelfSignedStrategy()) 
      .build();   

    // Allow TLSv1 protocol only 
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
      sslcontext, 
      new String[] { "TLSv1" }, 
      null, 
      SSLConnectionSocketFactory.getDefaultHostnameVerifier()); 

    CloseableHttpClient httpclient = HttpClients.custom() 
      .setSSLSocketFactory(sslsf) 
      .build(); 


    Byte[] result = null; 
    HttpParams httpParameters = new BasicHttpParams(); 
    // Set the timeout in milliseconds until a connection is established. 
    int timeoutConnection = 15000; 
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); 
    // Set the default socket timeout (SO_TIMEOUT) 
    // in milliseconds which is the timeout for waiting for data. 
    int timeoutSocket = 35000; 
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);   

    byte[] result1 = null; 


     HttpPost httppost = new HttpPost(webserviceUrl); 
     httppost.setHeader("soapaction", "aanleveren"); 
     httppost.setHeader("Content-Type", "text/xml; charset=utf-8"); 


     System.out.println("Executing request " + httppost.getRequestLine()); 

     try { 
      HttpEntity entity = new StringEntity(soapBericht,HTTP.UTF_8); 
      httppost.setEntity(entity); 
      HttpResponse response = httpclient.execute(httppost);// calling server 
      HttpEntity r_entity = response.getEntity(); //get response 
      if (r_entity != null) {  
       result1 = new byte[(int) r_entity.getContentLength()]; 
       if (r_entity.isStreaming()) { 
        DataInputStream is = new DataInputStream(
          r_entity.getContent()); 
        is.readFully(result1); 
       } 
      } 

      EntityUtils.consume(entity); 
     } catch (Exception E) { 
      Core.getLogger("SBR").log(LogLevel.WARNING,"ERROR " + E.getMessage() + E.getStackTrace()); 
      System.out.println("Exception While Connecting " +E.getMessage()); 
      E.printStackTrace(); 
     } 

     httpclient.close(); //shut down the connection 

答えて

0

代わりに、システムのルート証明書に追加するのは、そのアプリケーションの信頼を追加してみてください。

keytool -import -alias <ALIAS_NAME> -keystore <your_app_trust>.jks -trustcacerts -file <CERTIFICATE_TO_ADD.der> -v 

私は同じ問題を抱えていました。私はそれを解決しました。

0

Aあなたは、自己署名証明書を使用している非常に一般的なケースを... はこれを試してみてください:

   SSLContext sslContext = null; 
       try 
       { 
        sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy() { 
         @Override 
         public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException 
         { 
          return true; 
         } 

        }).build(); 
       } 
       catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) 
       { 
        //Show some Msg 
       } 
       CloseableHttpClient httpClient = HttpClients.custom() 
                  .setDefaultHeaders(headers) 
                  .setSSLContext(sslContext) 
                  .setSSLHostnameVerifier(new NoopHostnameVerifier()) 
                  .build(); 
関連する問題