【问题标题】:HTTP Resteasy client SSL trust all certificateHTTP Resteasy 客户端 SSL 信任所有证书
【发布时间】:2014-02-14 04:52:27
【问题描述】:

我使用 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 证书是没有意义的。我绝对不会在生产中这样做。

【问题讨论】:

  • 不,不是!因为我说的是 Resteasy Client,而不是 HTTPUrlConnection 或基础 Apache HttpClient,所以我已经检查了这些答案,但没有找到任何有用的信息

标签: java ssl jboss resteasy


【解决方案1】:

有点晚了,但是看这里: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
  • 该链接适用于 3* 版本,但我的代码适用于 2.3.2。同一个主版本,差别那么大吗?
猜你喜欢
  • 2020-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-02
  • 2013-08-04
  • 2010-10-16
  • 2017-04-28
  • 2012-02-15
相关资源
最近更新 更多