【问题标题】:Re-enable SSL validation in Java (Android)在 Java (Android) 中重新启用 SSL 验证
【发布时间】:2014-03-30 05:19:30
【问题描述】:

在我的 Android 应用程序(这是一个测试应用程序)中,我执行以下 disableSslValidation 方法来禁用 SSL 证书验证。现在无需重新启动该过程,我想启用 SSL 证书验证。我该如何做到这一点?

编辑:我完全了解禁用 SSL 验证所涉及的风险,并在知情的情况下接受上述风险。

private void disableSslValidation() throws KeyManagementException, NoSuchAlgorithmException {
    // 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(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);
}

【问题讨论】:

标签: java android ssl httpsurlconnection


【解决方案1】:

因为这是测试代码,我建议你把它扔掉。您不希望不安全的代码泄漏到生产环境中。您甚至不希望它出现在应用程序中。

【讨论】:

  • 请允许我不同意,因为当生产服务器出现 SSL 问题时,脚本可以保存生产环境如果在正确的竞争条件下运行。 (当程序员不得不与糟糕的系统管理员合作时)。而且你没有回答这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多