【问题标题】:Apache HttpClient resolving domain to IP address and not matching certificateApache HttpClient 将域解析为 IP 地址且证书不匹配
【发布时间】:2011-10-26 21:35:38
【问题描述】:

在使用 Apache HttpComponents HttpClient 库 (4.0.2) 时,我遇到了证书未正确验证的问题。该证书对域名有效(我们称之为 example.com),但它会根据 IP 地址进行验证:

证书中的主机名不匹配: !=

我的连接代码是:

    HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
    HttpConnectionParams.setSoTimeout(httpParams, 5000);
    DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);            
    String url = "https://www.example.com";
    HttpGet get = new HttpGet(url);
    HttpResponse httpResponse = httpClient.execute(get);
    String response = EntityUtils.toString(httpResponse.getEntity()).trim();

证书本身在通过网络浏览器连接时显示为有效,并且对我要连接的域名有效:

CN = *.example.com

证书也被添加到 Java 密钥库中(使用常规 HttpsURLConnection 测试)。

知道为什么这段代码使用 IP 地址而不是域名吗?

【问题讨论】:

  • 所以当涉及到 SSL 时,域无法解析为 IP。因此,您不能使用 IP 证书来保护域。很高兴知道。谢谢!

标签: java httpclient apache-httpcomponents


【解决方案1】:

似乎是 HttpClient 4.0.2 的一个已知错误 - https://issues.apache.org/jira/browse/HTTPCLIENT-996 该错误表明以下任何一项:

  • 升级到 4.0.3 或更高版本
  • 降级到 4.0.1
  • 使用 AllowAllHostnameVerifier

【讨论】:

    猜你喜欢
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    • 2010-09-05
    • 2015-03-06
    • 1970-01-01
    • 2011-03-12
    • 2021-08-27
    • 2011-02-11
    相关资源
    最近更新 更多