【问题标题】:Java HttpClient 4.4.1 can't create TrustStrategy for checking SSL CertificateJava HttpClient 4.4.1 无法创建用于检查 SSL 证书的 TrustStrategy
【发布时间】:2015-05-15 21:57:42
【问题描述】:

我在新版本的 HttpClient 上遇到了问题。 TrustStrategy 似乎不能再使用了;但是,我需要在我的 TrustStrategy 上设置方法 isTrusted 来初始化 SSLSocketFactory。有没有人有其他方法可以做到这一点?

我对此做了很多研究,大部分案例都与TrustManager有关。有很多情况与忽略服务器的证书有关。它适用于我的本地,但我想检查证书是否有效,所以......我需要有 TrustStrategy。

以下是我忽略证书的代码,我想在其中添加 TrustStrategy。

        SSLContext sslContext = SSLContext.getInstance("SSL");

        // set up a TrustManager that trusts everything
        sslContext.init(null, new TrustManager[]{
            new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }

                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }
            }
        }, new SecureRandom());

        SSLSocketFactory sf = new SSLSocketFactory(sslContext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        Scheme httpsScheme = new Scheme("https", 443, sf);
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(httpsScheme);

        BasicClientConnectionManager cm = new BasicClientConnectionManager(schemeRegistry);
        DefaultHttpClient httpClient = new DefaultHttpClient(cm);

我尝试了另一种方法来新建 SSLSocketFactory(trustStrategy, ALLOW_ALL_HOSTNAME_VERIFIER),但它不起作用。感谢您的关注。

【问题讨论】:

  • 您能否更具体地说明您希望其他人提供哪些改进类型?
  • 不是叫 TrustSelfSignedStrategy 吗?还是被删除了?

标签: java ssl httpclient


【解决方案1】:

似乎 TrustSelfSignedStrategy 期望链中至少有一个证书。它似乎是 QnD 的教父,但为了测试,覆盖此方法可能是合适的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-13
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    • 1970-01-01
    • 1970-01-01
    • 2013-11-12
    • 1970-01-01
    相关资源
    最近更新 更多