【问题标题】:Why we are getting the exception 'javax.net.ssl.SSLException: Not trusted server certificate' when using https为什么我们在使用 https 时收到异常“javax.net.ssl.SSLException:不受信任的服务器证书”
【发布时间】:2011-10-10 07:37:30
【问题描述】:

当我尝试通过 HTTPS 访问 URL 时,出现异常:

javax.net.ssl.SSLException:不受信任的服务器证书原因:java.security.cert.CertificateException:java.security.cert.CertPathValidatorException:找不到CertPath的TrustAnchor。

我在 Stackoverflow 上的一些帖子中发现,它应该接受一些证书。请告诉我接受证书的需要是什么......

提前谢谢你....

【问题讨论】:

    标签: android exception https


    【解决方案1】:

    证书通过根证书颁发机构(如 Verisign 或 Thawte)进行身份验证。一些 SSL 证书提供了一系列中间证书以进行验证,这些证书提供了最高级别证书之一的验证。在这种情况下,您需要在本地导入中间证书以及页面证书。这些需要导入到本地 cacerts 文件中。它是 Java 下的 cacerts 文件,不确定它在 Android 上的位置,但我之前已经看到它在这里链接过。

    另见Adding SSL Certificate to Keystore。我认为您需要下载 BouncyCastle。

    这也可能对您的特定问题更有用How to create a BKS (BouncyCastle) format Java Keystore that contains a client certificate chain

    【讨论】:

    • 实际上我的服务器正在使用受信任的证书。是否需要在 Keystore 上创建和导入 SSL 证书才能简单地访问 url?我对这个话题完全陌生。请告诉我如何创建这个主题......
    • 我应该从哪里获得这个 SSL 证书?我完全困惑如何做这些事情?你能推荐一个很好的教程来解释建立 Https 连接的 A-Z 步骤吗?
    • 浏览 https URL。单击挂锁(在 IE8 中)。单击网站标识弹出窗口上的查看证书。单击详细信息选项卡。单击复制到文件按钮。我通常将文件保存为 DER,但 YMMV。将保存的文件导入您的密钥库。 HTH。
    【解决方案2】:

    您必须向 Keystore 提供 SSL 证书作为指定的 @mikey... 但是如果你想允许所有主机而不进行任何检查(允许所有主机)。

    public static class _FakeX509TrustManager implements X509TrustManager {
    
        private static TrustManager[] trustManagers;
        private static final X509Certificate[] _AcceptedIssuers = new X509Certificate[] {};
    
        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }
    
        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }
    
        public boolean isClientTrusted(X509Certificate[] chain) {
                return true;
        }
    
        public boolean isServerTrusted(X509Certificate[] chain) {
                return true;
        }
    
        @Override
        public X509Certificate[] getAcceptedIssuers() {
                return _AcceptedIssuers;
        }
    
        public static void allowAllSSL() {
            HttpsURLConnection.setDefaultHostnameVerifier(new 
                    HostnameVerifier(){
                    @Override
                    public boolean verify(String hostname, SSLSession session) {
                            return true;
                    }
    
            });
    
            SSLContext context = null;
            if (trustManagers == null) {
                    trustManagers = new TrustManager[] { new _FakeX509TrustManager() };
            }
    
            try {
                    context = SSLContext.getInstance("TLS");
                    context.init(null, trustManagers, new SecureRandom());
            } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
            } catch (KeyManagementException e) {
                    e.printStackTrace();
            }
    
    
            HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
    }
    

    }

    调用 _FakeX509TrustManager.allowAllSSL();在您的 http 方法的开头。 希望有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-01
      • 1970-01-01
      • 2013-05-19
      • 2012-02-26
      • 1970-01-01
      • 2012-05-05
      • 1970-01-01
      • 2012-06-29
      相关资源
      最近更新 更多