2016-08-23 191 views
1

我正在使用Apache Camel使用SOAP服務,並且該服務託管在自簽名證書上用於開發目的。Apache Camel ProducerTemplate忽略SSL證書檢查

我試着將證書導入密鑰庫,但失敗了,因爲證書沒有有效的CN。

我想要忽略證書錯誤或信任所有證書。我如何使用producerTemplate來做到這一點。

 Exchange exchangeRequest = producerTemplate.request(endpoint, 
      new Processor() { 
       public void process(Exchange exchange) throws Exception { 
        exchange.getIn().setBody(payload); 
        if (headermap != null && headermap.size() > 0) { 
         exchange.getIn().setHeaders(headermap); 
        } 
        if (soapHeader != null && !soapHeader.equals("")) { 
         exchange.getIn() 
           .setHeader(
             SpringWebserviceConstants.SPRING_WS_SOAP_HEADER, 
             soapHeader); 
        } 
       } 
      }); 
+0

你有沒有看:https://mail-archives.apache.org/mod_mbox/camel-users/201108.mbox/%[email protected] .com%3E –

+0

但httpClientConfigurer選項不適用於Spring-ws組件。只有sslContextParameters可用,它需要一個密鑰庫。 – Samy

回答

0

不知道我在駱駝方式做了什麼,但這對我有效。剛剛寫了一個方法用於使用JAVA信任所有證書,並在使用Camel ProducerTemplate發出請求之前調用它。

public void trustall() throws NoSuchAlgorithmException, KeyManagementException, IOException { 
    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) { 
     } 
    } }; 

    // Install the all-trusting trust manager 
    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() { 
     @Override 
     public boolean verify(String hostname, SSLSession session) { 
      return true; 
     } 

    }; 

    // Install the all-trusting host verifier 
    HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); 

    URL url = new URL(endpoint); 
    URLConnection con = url.openConnection(); 
    Reader reader = new InputStreamReader(con.getInputStream()); 
    while (true) { 
     int ch = reader.read(); 
     if (ch == -1) { 
      break; 
     } 
     System.out.print((char) ch); 
    } 
} 
+0

你能接受這個答案,以便該問題不再顯示爲未答覆?謝謝! –

+0

@MilošMilivojević非常感謝! – Samy