从 Java 连接到 HTTPS 服务器并忽略安全证书的有效性

发布于 2024-08-31 08:21:08 字数 208 浏览 4 评论 0原文

我一直在测试一个系统,该系统使用不同的密钥访问一组 https 服务器,其中一些密钥无效,并且所有密钥都不在我的 JVM 的本地密钥存储中。我实际上只是在测试,所以现阶段我并不关心安全性。有没有一种好方法可以对服务器进行 POST 调用并告诉 Java 不要担心安全证书?

我对此进行的谷歌搜索带来了一些代码示例,这些示例使一个类进行验证,该示例始终有效,但我无法让它连接到任何服务器。

I've been testing a system that accesses a group of https servers with different keys, some of which are invalid and all of them are not in the local key store for my JVM. I am really only testing things out, so I don't care about the security at this stage. Is there a good way to make POST calls to the server and tell Java not to worry about the security certificates?

My google searches for this have brought up some code examples that make a class to do the validation, that always works, but I cannot get it to connect to any of the servers.

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

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

发布评论

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

评论(2

难忘№最初的完美 2024-09-07 08:21:08

根据评论:

对于 Google 搜索的示例,您指的是这个


更新:链接已损坏,因此这是我从 互联网档案

// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]{
    new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }
        public void checkClientTrusted(
            java.security.cert.X509Certificate[] certs, String authType) {
        }
        public void checkServerTrusted(
            java.security.cert.X509Certificate[] certs, String authType) {
        }
    }
};

// Install the all-trusting trust manager
try {
    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new java.security.SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {

}

// Now you can access an https URL without having the certificate in the truststore
try {
    URL url = new URL("https://hostname/index.html");
} catch (MalformedURLException e) {

}

As per the comments:

With Googled examples, you mean among others this one?


Update: the link broke, so here's an extract of relevance which I saved from the internet archive:

// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]{
    new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }
        public void checkClientTrusted(
            java.security.cert.X509Certificate[] certs, String authType) {
        }
        public void checkServerTrusted(
            java.security.cert.X509Certificate[] certs, String authType) {
        }
    }
};

// Install the all-trusting trust manager
try {
    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new java.security.SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {

}

// Now you can access an https URL without having the certificate in the truststore
try {
    URL url = new URL("https://hostname/index.html");
} catch (MalformedURLException e) {

}
长梦不多时 2024-09-07 08:21:08

您需要创建一个绕过所有安全检查的 X509TrustManager。您可以在我对此问题的回答中找到一个示例,

如何忽略 Apache HttpClient 4.0 中的 SSL 证书错误

You need to create a X509TrustManager which bypass all the security check. You can find an example in my answer to this question,

How to ignore SSL certificate errors in Apache HttpClient 4.0

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