本文介绍了HttpUnit WebConversation SSL问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 WebConversation WebRequest 对象的上下文中忽略SSL证书问题?我知道我可以创建一个假的 TrustManager ,它接受所有证书但是如何在HttpUnit上下文中设置它?

How can I ignore SSL certificate issues from the context of the WebConversation or WebRequest object? I know I can create a fake TrustManager that accepts all certificates but how can I set this in the HttpUnit context?

以下是我得到的例外情况:

Here is the exception I am getting:

[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属性的对象中?

I need to somehow set the SSLSocket settings to the WebConversation or WebRequest object; looking at the JavaDocs for HttpUnit there is no such method or constructor to do so. Is there a way I can wrap this inside some object which has exposed SSLSocket properties?

推荐答案

根据,似乎HttpUnit正在使用Java标准库提供的SSL实现。编写和安装全部接受 TrustManager 非常简单:

According to this FAQ entry, it seems that HttpUnit is using the SSL implementation provided by the Java standard library. Writing and installing an "accept all" TrustManager is straightforward:

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建立连接)

However you should keep in mind that this code sample may need some modifications to work with HttpUnit (for instance if the library establishes the connections using a custom SocketFactory)

因为似乎HttpUnit没有提供任何API来设置自定义这里的SSLSocketFactry是另一种设置默认SSL上下文的解决方案(仅限Java 6)

Since it seems that HttpUnit does not provide any API to set a custom SSLSocketFactry here is an alternative solution setting the default SSL context (Java 6 only)

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);
    }
}

这篇关于HttpUnit WebConversation SSL问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 06:30