2011-02-01 28 views
2

如何忽略WebConversationWebRequest對象上下文中的SSL證書問題?我知道我可以創建一個假的TrustManager接受所有證書,但我怎麼能在HttpUnit上下文中設置它?HttpUnit WebConversation SSL問題

這是我收到的例外:

[Security:090508]Certificate chain received from my.domain.com - NUM.NUM.NUM.NUM was incomplete., 
[Security:090477]Certificate chain received from my.domain.com - NUM.NUM.NUM.NUM was not trusted causing SSL handshake failure. 

我需要以某種方式設置的SSLSocket設置到的WebConversation或WebRequest對象;查看HttpUnit的JavaDocs,沒有這樣的方法或構造函數可以這樣做。有沒有一種方法可以將它包含在某個暴露了SSLSocket屬性的對象中?

回答

1

根據this FAQ entry,似乎HttpUnit正在使用Java標準庫提供的SSL實現。編寫和安裝「照單全收」 TrustManager很簡單:

private static class AnyTrustManager implements X509TrustManager 
{ 
    public void checkClientTrusted(X509Certificate[] chain, String authType) 
    { 
    } 

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

    public X509Certificate[] getAcceptedIssuers() 
    { 
     return new X509Certificate[0]; 
    } 
} 

static { 
    try { 
     SSLContext ssl = SSLContext.getInstance("SSL"); 
     ssl.init(null, new X509TrustManager[] {new AnyTrustManager()}, null); 
     HttpsURLConnection.setDefaultSSLSocketFactory(ssl.getSocketFactory()); 
    } catch (NoSuchAlgorithmException ex) { 
     throw new Error(ex); 
    } catch (KeyManagementException ex) { 
     throw new Error(ex); 
    } 
} 

但是你應該記住,此代碼示例可能需要一些修改與HttpUnit的工作(例如,如果使用自定義庫建立連接的SocketFactory )

因爲它似乎HttpUnit的不提供任何API來這裏設置自定義SSLSocketFactry是一個替代的解決方案設置默認的SSL上下文(Java 6中只)

static { 
    try { 
     SSLContext ssl = SSLContext.getDefault(); 
     ssl.init(null, new X509TrustManager[] {new AnyTrustManager()}, null); 
    } catch (NoSuchAlgorithmException ex) { 
     throw new Error(ex); 
    } catch (KeyManagementException ex) { 
     throw new Error(ex); 
    } 
} 
+0

謝謝,所以將標準WebConversation.getResponse()調用實現這個的TrustManager? – TheWolf 2011-02-01 23:02:18

2

什麼爲我工作是使用this tool將服務器的無效證書添加到新的密鑰庫(創建文件jssecacerts),然後用新的密鑰庫替換我現有的密鑰庫。
CP的cacerts cacerts.bak CP〜/工具/ jssecacerts cacerts的

HttpUnit的的工作之後的罰款。

1

如果你有訪問Web客戶端,你可以做到這一點跳過SSL證書驗證:後期

WebClient client = new WebClient(); 
client.getOptions().setUseInsecureSSL(true); 
0

一點,我只是恰好碰到了這個問題,但我設法httpunit 1.7.2AnyTrustManager作爲工作每Jsc的用下面的代碼的答案(httpunit的使用HttpsURLConnectionOldImpl下德罩):

static { 
    try { 
     SSLContext ssl = SSLContext.getInstance("TLS"); 
     ssl.init(null, new X509TrustManager[] {new AnyTrustManager()}, null); 
     com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl.setDefaultSSLSocketFactory(ssl.getSocketFactory());    
    } catch (NoSuchAlgorithmException ex) { 
     throw new Error(ex); 
    } catch (KeyManagementException ex) { 
     throw new Error(ex); 
    } 
}