apache httpclient 4.4:HostnameVerifier从4.3.x过渡

时间:2015-03-23 10:16:39

标签: java apache-httpclient-4.x

HttpClient 4.3在org.apache.http.conn.ssl.SSLConnectionSocketFactory中有三个静态变量:

  1. STRICT_HOSTNAME_VERIFIER
  2. BROWSER_COMPATIBLE_HOSTNAME_VERIFIER
  3. ALLOW_ALL__HOSTNAME_VERIFIER
  4. 将依赖项升级到HttpClient的4.4版时,我看到所有上述常量都已弃用。 JavaDoc中的弃用说明提到使用org.apache.http.conn.ssl.DefaultHostnameVerifier。阅读文档,我认为DefaultHostnameVerifierSTRICT_HOSTNAME_VERIFIER的直接替代品。 ALLOW_ALL__HOSTNAME_VERIFIER也很容易实现:

    package org.wiztools.restclient.http;
    
    import javax.net.ssl.HostnameVerifier;
    import javax.net.ssl.SSLSession;
    
    /**
     *
     * @author subwiz
     */
    public class AllowAllHostnameVerifier implements HostnameVerifier {
    
        @Override
        public boolean verify(String string, SSLSession ssls) {
            return true;
        }
    
    }
    

    STRICT_HOSTNAME_VERIFIERBROWSER_COMPATIBLE_HOSTNAME_VERIFIER(来自JavaDoc)之间存在细微差别:

      

    BROWSER_COMPATIBLE和STRICT之间的唯一区别是带有BROWSER_COMPATIBLE的通配符(例如&#34; * .foo.com&#34;)匹配所有子域,包括&#34; abfoo.com&#34;。< / p>

    我们是否为httpclient 4.4提供了一个随时可用的BROWSER_COMPATIBLE主机名验证程序?

4 个答案:

答案 0 :(得分:7)

实际上,AllowAllHostnameVerifier的javadoc可以直接替换ALLOW_ALL__HOSTNAME_VERIFIERNoopHostnameVerifier

答案 1 :(得分:7)

您不需要AllowAllHostnameVerifier的新实现类,也不需要BrowserCompatHostnameVerifier的其他实现,只需将实例传递给新的DefaultHostnameVerifier

SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new DefaultHostnameVerifier());

此类使用以下方法签名的必要验证方法

public final boolean verify(String host, SSLSession session) (Override)

public final void verify(String host, X509Certificate cert) throws SSLException

在第二种方法中,httpcomponents检查匹配的子域名

public final void verify(String host, X509Certificate cert) throws SSLException {
    boolean ipv4 = InetAddressUtils.isIPv4Address(host);
    boolean ipv6 = InetAddressUtils.isIPv6Address(host);
    int subjectType = ((ipv4) || (ipv6)) ? 7 : 2;
    List subjectAlts = extractSubjectAlts(cert, subjectType);
    if ((subjectAlts != null) && (!(subjectAlts.isEmpty()))) {
        if (ipv4)
            matchIPAddress(host, subjectAlts);
        else if (ipv6)
            matchIPv6Address(host, subjectAlts);
        else {
            matchDNSName(host, subjectAlts, this.publicSuffixMatcher);
        }
    } else {
        X500Principal subjectPrincipal = cert.getSubjectX500Principal();
        String cn = extractCN(subjectPrincipal.getName("RFC2253"));
        if (cn == null) {
            throw new SSLException("Certificate subject for <" + host + "> doesn't contain " + "a common name and does not have alternative names");
        }

        matchCN(host, cn, this.publicSuffixMatcher);
    }
}

查看源代码以获得更多说明

org.apache.http.conn.ssl.DefaultHostnameVerifier

希望这有帮助。

答案 2 :(得分:1)

BrowserCompatHostnameVerifier本质上是与IE 5/6兼容的实现。我不确定它是否真的与更现代的浏览器应用程序兼容。 BrowserCompatHostnameVerifier本来就不应该存在,不应再使用了。

答案 3 :(得分:0)

我读了这一切,没有什么对我有用,这就是我节省了一天的时间:https://stackoverflow.com/a/36507502/3090309

我正在使用:

compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.2'