Apache Camel ProducerTemplate忽略SSL证书检查

问题描述:

我正在使用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

不知道我在骆驼方式做了什么,但这对我有效。刚刚写了一个方法用于使用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