Apache HttpClient 将域解析为 IP 地址且不匹配证书

发布于 2024-12-12 06:09:16 字数 936 浏览 3 评论 0原文

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

证书中的主机名不匹配:<123.123.123.123> != <*.example.com>

我用于建立连接的代码是:

    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 地址而不是域名吗?

When using the Apache HttpComponents HttpClient library (4.0.2) I'm having a problem where the certificate doesn't get validated properly. The certificate is valid for the domain name (let's call it example.com) however it's getting validated against the IP address instead:

hostname in certificate didn't match: <123.123.123.123> !=
<*.example.com>

My code for making the connection is:

    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();

The certificate itself shows as valid when connecting through a web browser and is valid for the domain name I'm connecting to:

CN = *.example.com

The certificate is also added to the Java keystore (tested using regular HttpsURLConnection).

Any ideas why this code uses the IP address instead of the domain name?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜点 2024-12-19 06:09:16

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

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

Appears to be a known bug with HttpClient 4.0.2 - https://issues.apache.org/jira/browse/HTTPCLIENT-996
The bug suggests any of the following:

  • Upgrade to version 4.0.3 or newer
  • Downgrade to 4.0.1
  • Use the AllowAllHostnameVerifier
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文