【问题标题】:HTTPS hostname wrong: should be <sub.domain.com>. What causes this?HTTPS 主机名错误:应该是 <sub.domain.com>。这是什么原因造成的?
【发布时间】:2010-12-20 14:21:08
【问题描述】:

我在尝试使用 https 连接到服务器时收到此“HTTPS 主机名错误:”错误。我的网址看起来像这样

https://sub.domain.com/tamnode/webapps/app/servlet.

我使用以下代码连接

    // Create a URLConnection object for a URL
    URL url = new URL(requestedURL);
    HttpURLConnection.setFollowRedirects(false);

    // connect
    connection = (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    connection.setRequestProperty("User-Agent", USER_AGENT); //$NON-NLS-1$

    OutputStreamWriter wr = new OutputStreamWriter(connection
            .getOutputStream());

然后得到一个错误

IOException: HTTPS hostname wrong:  should be <sub.domain.com>. 
    at sun.net.www.protocol.https.HttpsClient.checkURLSpoofing
    ....

这是过去有效但不再有效的代码。系统架构发生了一些变化,但我需要在联系负责人之前获取更多数据。

什么会导致这个错误?我可以关闭 URLSpoofing 检查吗?

【问题讨论】:

    标签: java exception https


    【解决方案1】:

    看来 domain.com 的 SSL 证书已授予 sub.domain.com。或者,更有可能的是,之前的 domain.com 已被重命名为 sub.domain.com 而没有更新 SSL 证书。

    【讨论】:

      【解决方案2】:

      cletus 关于可能的原因是正确的。

      还有一种方法可以关闭欺骗检查。

      您可以创建一个实现 HostnameVerifier 的对象,该对象在比“通常”更多的情况下返回 true。

      您可以通过在问题代码中的连接对象上调用 setHostnameVerifier 来替换默认的 HostnameVerifier。

      这个答案是“灵感来自”:http://www.java-samples.com/showtutorial.php?tutorialid=211

      我找到了这个查询的链接:http://www.google.com/search?q=https+hostname+wrong+should+be

      还有一点需要注意:在您执行此操作之前请三思而后行。您将在客户端和服务器组件之间的安全性中制造可利用的弱点。

      【讨论】:

      • 2 个 oracle 链接断开
      【解决方案3】:

      我遇到了这个异常 - java.io.IOException: HTTPS hostname wrong: should be &lt;localhost&gt;

      我的解决方案是我更改了我的自签名证书并制作了CN=localhost

      将您的证书域名cn=&lt;domain-name&gt; 添加到您的主机文件中,该文件可能位于 c:/windows/system32/drivers/etc/...

      【讨论】:

        【解决方案4】:

        以下代码解决了我的问题

        static {
            //for localhost testing only
            javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
                    new javax.net.ssl.HostnameVerifier() {
        
                @Override
                public boolean verify(String hostname,
                        javax.net.ssl.SSLSession sslSession) {
                    if (hostname.equals("your_domain")) {
                        return true;
                    }
                    return false;
                }
            });
        }
        

        【讨论】:

        • 您不应编写“仅用于测试”的代码。它不可避免地会泄漏到生产环境中并危及系统的安全性。
        • 另见stackoverflow.com/questions/37724901/…。对于需要与本地主机建立出站连接的库,我需要支持这一点,其中使用的证书与本地主机名称不匹配。因此,代码使用 HostnameVerifier 进行特定调用,并且仅在存在标志 -DdevelopmentMode=true 时才应用代码。
        【解决方案5】:

        使用主机名(dns 名称)作为别名。

        例如:

        keytool -import -alias <google.com> -file certificate_one.cer -keystore cacerts
        

        【讨论】:

          【解决方案6】:

          默认情况下,Java 会验证证书 CN(通用名称)是否与 URL 中的主机名相同。如果证书中的 CN主机名 不同,则您的 Web 服务客户端将失败并出现以下异常:java.io.IOException: HTTPS hostname wrong: should是证书中的主机名。

          【讨论】:

            【解决方案7】:

            这只是“svarog”帖子的替代品

            static {
            
                HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> hostname.equals("domain name"));
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2021-12-30
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-01-07
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多