HTTP Resteasy客户端SSL信任所有证书

时间:2014-01-23 14:54:09

标签: java ssl jboss resteasy

我搜索了任何可能的解决方案,使用Resteasy客户端信任所有证书,但我找不到一个可行的解决方案。我开始认为使用Resteasy 2.2.1无法做到这一点。

现在,这是我使用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证书是没有意义的。我绝对在制作中这样做。

1 个答案:

答案 0 :(得分: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)进行测试