2014-01-23 76 views
2

我搜索了任何可能的解決方案來信任使用Resteasy客戶端的所有證書,但我無法找到單個工作解決方案。我開始認爲使用Resteasy 2.2.1無法做到這一點。現在HTTP Resteasy客戶端SSL信任所有證書

,這是我所使用RestEasy的客戶端設置一個代理到目前爲止做了一個正常的HTTP連接的示例:

org.apache.commons.httpclient.HttpClient hc = new HttpClient(); 
ApacheHttpClientExecutor ace; 
String proxyhost = getProperty("proxyHost"); 
Integer proxyport = getProperty("proxyPort", Integer.class); 
boolean useProxy = (proxyhost != null); 
if(useProxy){ 
    hc.getHostConfiguration().setProxy(proxyhost, proxyport); 
    ace = new ApacheHttpClientExecutor(hc); 
} else { 
    ace = new ApacheHttpClientExecutor(); 
} 
ClientRequestFactory crf = new ClientRequestFactory(ace,uri); 

現在,我怎麼能告訴我ClientRequestFactory或我的ApacheHttpClientExecutor或我的HttpClient信任所有證書?

請注意:我使用的是RestEasy的2.2.1(JBoss的5.1)我不能遷移到JBoss 7或使用不同的RestEasy的版本,所以我不能接受的是使用ResteasyClientBuilder

我已經可以任何回答看到那個回答「」的好人,你不應該相信所有證書,它是邪惡的!「。這是一個用於集成測試的HTTP客戶端,因此在此測試級別考慮SSL證書毫無意義。我絕對會不是在生產中做到這一點。

+0

這可能是重複與http://stackoverflow.com/questions/21257455/whats-an-easy-way-to-totally-ignore-ssl-with-java-url-connections/21257694#21257694 –

+1

不,它不是!因爲我在談論Resteasy客戶端,而不是HTTPUrlConnection,也不是基於Apache HttpClient,我已經檢查這些答案,但沒有找到任何有用的信息 – thermz

回答

2

有點晚了,但看看這裏:https://stackoverflow.com/a/22444115/1328942

private DefaultHttpClient createAllTrustingClient() throws GeneralSecurityException { 
     SchemeRegistry registry = new SchemeRegistry(); 
     registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); 

     TrustStrategy trustStrategy = new TrustStrategy() { 

      @Override 
      public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { 
       LOG.info("Is trusted? return true"); 
       return true; 
      } 
     }; 

     SSLSocketFactory factory = new SSLSocketFactory(trustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 
     registry.register(new Scheme("https", 443, factory)); 

     ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(registry); 
     mgr.setMaxTotal(1000); 
     mgr.setDefaultMaxPerRoute(1000); 

     DefaultHttpClient client = new DefaultHttpClient(mgr, new DefaultHttpClient().getParams()); 
     return client; 
    } 

,這是它如何工作的:

@Test 
public void testCatchingTheUnknownHostException() throws Exception { 
    ApacheHttpClient4Executor apacheHttpClient4Executor = new ApacheHttpClient4Executor(
      createAllTrustingClient()); 

    ClientRequest clientRequest = new ClientRequest(host, apacheHttpClient4Executor); 
} 

與RestEasy的2.3.2.Final(Jboss的7.1.1)

測試它
+0

感謝您的迴應,但問題是關於Resteasy 2.2.1 – thermz

+0

鏈接是3 *版本,但我的代碼是爲了2.3.2。它是相同的主要版本,是差異那麼大? –