【问题标题】:Make a connection to a HTTPS server from Java and ignore the validity of the security certificate从 Java 连接到 HTTPS 服务器并忽略安全证书的有效性
【发布时间】:2010-05-02 02:38:59
【问题描述】:

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

我的 google 搜索显示了一些代码示例,这些示例使一个类进行验证,这总是有效的,但我无法让它连接到任何服务器。

【问题讨论】:

  • 感谢 BalusC 完美运行。我尝试了 3 或 4 个编译并运行良好但没有工作。你能回答这个问题吗?
  • 完成。但是将来请在问题中正确提及您尝试了哪些示例以及链接:)

标签: java security ssl https


【解决方案1】:

根据 cmets:

对于谷歌搜索的例子,你的意思是this one


更新:链接坏了,所以这是我从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) {

}

【讨论】:

【解决方案2】:

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

How to ignore SSL certificate errors in Apache HttpClient 4.0

【讨论】:

  • 然后你需要把它扔掉。编写安全代码然后以不安全的方式对其进行测试是没有意义的。修复问题。如果您不想要安全性,请不要使用 SSL。我颤抖着想这东西已经投入生产了多少次。
  • @Daniel 那么您的生产系统不安全。您需要阅读 RFC2246 中关于此的备注。这是一个非常严重的问题。
【解决方案3】:

在类文件中添加下面的静态方法代码

静态{

    // Create a trust manager that does not validate certificate chains

    try {
        TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }

            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        }
        };

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

        // Create all-trusting host name verifier
        HostnameVerifier allHostsValid = new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };

        // Install the all-trusting host verifier
        HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
    } catch (Exception err ){
        log.debug(err.getMessage());;
    }
}

【讨论】:

    猜你喜欢
    • 2011-11-16
    • 2020-12-28
    • 1970-01-01
    • 2013-01-10
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多