HTTPS 主机名错误:应为。这是什么原因造成的?

发布于 2024-08-12 17:10:51 字数 875 浏览 5 评论 0原文

尝试使用 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 检查吗?

I am getting this 'HTTPS hostname wrong:' error when trying to connect to a server using https. My url looks something like this

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

I connect using the following code

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

but then get an error

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

This is code which has worked in the past but no longer. There have been some changes to the system architecture but I need to get more data before approaching those responsible.

What can cause this error? Can I turn off the URLSpoofing check?

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

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

发布评论

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

评论(7

孤凫 2024-08-19 17:10:51

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

It looks like the SSL certificate for domain.com has been given to sub.domain.com. Or, more likely, what was domain.com has been renamed to sub.domain.com without updating the SSL certificate.

甩你一脸翔 2024-08-19 17:10:51

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

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

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

您可以通过调用 setHostnameVerifier 问题代码中的连接对象。

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

我发现该查询的链接:http://www.google.com/search?q=https+hostname+wrong+should+be

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

cletus is right about the probable cause.

There is a way to turn off the spoof checking, too.

You can create an object that implements HostnameVerifier that returns true under more circumstances than 'usual'.

You would replace the default HostnameVerifier by calling setHostnameVerifier on the connection object in the code in the question.

This answer was 'inspired by': http://www.java-samples.com/showtutorial.php?tutorialid=211

I found that link with this query: http://www.google.com/search?q=https+hostname+wrong+should+be

One more note: think twice before you do this. You will create an exploitable weakness in the security between your client and server components.

假扮的天使 2024-08-19 17:10:51

我收到此异常 - java.io.IOException: HTTPS 主机名错误:应该是

我的解决方案是更改我的自签名证书并设置 CN=localhost

或者

将您的证书域名 cn= 添加到可能位于 c:/windows/system32/drivers/etc/... 的主机文件中

I got this exception - java.io.IOException: HTTPS hostname wrong: should be <localhost>.

My solution is I changed my self-signed certificate and make the CN=localhost.

OR

Add your certificate domain-name cn=<domain-name> to your host file probably located at c:/windows/system32/drivers/etc/...

病毒体 2024-08-19 17:10:51

以下代码解决了我的问题

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

The following code resolved my problem

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;
        }
    });
}
只是在用心讲痛 2024-08-19 17:10:51

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

Java by default verifies that the certificate CN (Common Name) is the same as hostname in the URL. If the CN in the certificate is not the same as the host name, your web service client fails with the following exception: java.io.IOException: HTTPS hostname wrong: should be hostname as in the certificates.

给妤﹃绝世温柔 2024-08-19 17:10:51

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

static {

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

This is just an alternative of 'svarog' post

static {

    HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> hostname.equals("domain name"));
}
千仐 2024-08-19 17:10:51

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

前任:

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

Use host name (dns name) as Alias name.

Ex:

keytool -import -alias <google.com> -file certificate_one.cer -keystore cacerts
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文