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

3
我搜索了所有可能的解决方案,以便使用Resteasy客户端来信任所有证书,但是我没有找到一个可行的解决方案。我开始觉得在Resteasy 2.2.1中没有办法实现这一点。
现在,以下是我为普通HTTP连接所做的工作示例,使用resteasy客户端设置代理:
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);

现在,我该如何告诉我的ClientRequestFactoryApacheHttpClientExecutorHttpClient信任所有证书呢?
注意:我正在使用Resteasy 2.2.1(JBoss 5.1),无法迁移到JBoss 7或使用不同的resteasy版本,因此不能接受使用ResteasyClientBuilder的任何答案。
我已经看到有人回答“你不应该信任所有证书,这是邪恶的!”这是用于集成测试的HTTP客户端,在这个测试级别上考虑SSL证书是毫无意义的。我绝对不会在生产环境中这样做。

这可能与https://dev59.com/yGEi5IYBdhLWcg3wd8EF#21257694重复。 - Yasser Zamani
2
不,不是这样的!因为我在谈论Resteasy客户端,而不是HTTPUrlConnection或基本的Apache HttpClient,我已经检查了那些答案,没有找到任何有用的信息。 - thermz
1个回答

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)上进行测试。


谢谢您的回复,不过问题是关于Resteasy 2.2.1的。 - thermz
这个链接是3*版本的,但我的代码是为2.3.2版本编写的。虽然它们都是同一个主要版本,但差异会很大吗? - Kescha Skywalker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接