代码之家  ›  专栏  ›  技术社区  ›  justinhj

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

  •  5
  • justinhj  · 技术社区  · 15 年前

    我一直在测试一个使用不同密钥访问一组https服务器的系统,其中一些是无效的,并且所有这些都不在jvm的本地密钥存储中。我只是在测试,所以我不关心这个阶段的安全性。有没有一种好的方法可以给服务器打电话并告诉Java不要担心安全证书?

    我在google上搜索到了一些代码示例,这些代码示例生成了一个类来进行验证,但始终有效,但我无法让它连接到任何服务器。

    3 回复  |  直到 6 年前
        1
  •  4
  •   BalusC    11 年前

    根据评论:

    你的意思是,在谷歌搜索的例子中 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
  •  0
  •   Community CDub    8 年前

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

    How to ignore SSL certificate errors in Apache HttpClient 4.0

        3
  •  0
  •   shrisowdhaman    6 年前

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

    静态{

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