【问题标题】:java the host name doesn't match the certificatejava主机名与证书不匹配
【发布时间】:2016-04-23 12:12:13
【问题描述】:

这是我遇到的例外情况

主机名“bla bla bla.com”与对等方提供的证书主题不匹配(CN=*.bla bla bla.com, OU=PositiveSSL Wildcard, OU=Domain Control Validated)

我已经看到了这个问题:

Ignoring SSL certificate in Apache HttpClient 4.3

我按照它的建议做了,但是没有用。我已经看到了很多与该问题相关的问题,但它们都已被弃用。

这是我的代码:

SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf =
    new SSLConnectionSocketFactory(builder.build());
CloseableHttpClient httpclient =
    HttpClients.custom().setSSLSocketFactory(sslsf).build();
HttpGet httpGet = new HttpGet("https://b.blablabla.com");
CloseableHttpResponse response1 = httpclient.execute(httpGet);
try {
    System.out.println(response1.getStatusLine());
    HttpEntity entity1 = response1.getEntity();
    // do something useful with the response body
    // and ensure it is fully consumed
    EntityUtils.consume(entity1);
} finally {
    response1.close();
}

我怎样才能绕过这个证书? 这只是为了测试;这不是一个真正的生产环境。

【问题讨论】:

  • 不应该是new HttpGet("https://blablanla.com")吗?
  • @TamasHegedus 实际上是这样,我只是隐藏了实际的域名。错字,现已更正
  • 服务器回复了子域证书,请求https://x.blablabla.com 可能会有所帮助
  • @TamasHegedus 抱歉,我没有收到您的最后评论。你能再描述一下吗?
  • 服务器回答的证书是通配符证书*.blabla.com。该证书对blabla.com 域无效,仅对它的子域有效。尝试向子域发送请求:new HttpGet("https://x.blahblahblah.com")

标签: java ssl httpclient


【解决方案1】:

除了本地主机,您还可以将自己的自定义主机名添加到您的开发机器。在示例中使用C:/windows/system32/etc/hosts 添加主机名anjadavid.blablabla.com

现在打开浏览器并转到https://anjadavid.blablabla.com,错误就会消失。

【讨论】:

    猜你喜欢
    • 2016-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    • 2015-04-30
    • 1970-01-01
    • 2021-07-07
    相关资源
    最近更新 更多