2011-02-01 90 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); 
    } 
}