【问题标题】:Bypass SSL Warning from google play绕过谷歌播放的 SSL 警告
【发布时间】:2016-04-28 11:43:14
【问题描述】:

我使用 JSOUP 连接到 许多 https 网站。我使用方法 Jsoup.connect(url) ,但它抛出异常:

javax.net.ssl.SSLHandshakeException: Certificate not valid or trusted.

所以我使用此代码来信任所有证书 ssl:

public static void enableSSLSocket() throws KeyManagementException, NoSuchAlgorithmException {
   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());
    HttpsURLConnection.setDefaultHostnameVerifier(
        new HostnameVerifier() {
            public boolean verify(String urlHostName, SSLSession session) {
                return true;
            }
        });
}
catch ( Exception e ) {
    //We can not recover from this exception.
    e.printStackTrace();
}}

但是我使用上面的代码时,Play 商店会出现 goole 警告。

您的应用正在通过 Apache HTTP 客户端使用不安全的 X509TrustManager 接口实现,从而导致安全漏洞。请参阅此 Google 帮助中心文章了解详细信息,包括修复漏洞的截止日期。

如果我添加此代码:

 public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                try {
                    chain[0].checkValidity();
                } catch (Exception e) {
                    throw new CertificateException("Certificate not valid or trusted.");
                }
            }

警告消失了,但 JSOUP 无法正常工作,但同样的异常也无法再次工作。 那么有什么方法可以信任所有 ssl 并绕过谷歌警告?提前谢谢你。

【问题讨论】:

  • "那么有什么方法可以信任所有 ssl 并绕过 google 警告?" ——希望不会。 “但 JSOUP 不起作用”——那么也许您应该考虑打开一个单独的 Stack Overflow 问题,在其中提供您的 JSoup 问题的minimal reproducible example 并详细解释“不起作用”的含义。
  • @CommonWare 我只是更新我的问题。希望你能帮助我!
  • 我建议你完全消除你的enableSSLSocket() 方法。
  • @CommonsWare 抱歉,我更新了我的问题。如果我删除 enableSSLSocket(),我将无法使用 Jsoup 连接到 https 网站。
  • 那么该站点的 SSL 证书有缺陷,或者它使用的证书颁发机构无法被您的 Android 设备识别。 “好吧,我们将忽略所有 SSL,如果我们的用户受到攻击,好吧,那很好”的答案不再被 Google 接受,这是有充分理由的。要么停止使用该网站,要么追查特定问题,并针对它提出一个不会削弱每个人的安全性的有针对性的解决方案。

标签: android ssl jsoup


【解决方案1】:

我找到了解决方案,希望有人需要它:

 public static void trustSSLCertificate(final Activity mActivity, final DownloadPortalTask task){
        try {
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });

            SSLContext context = SSLContext.getInstance("TLS");
            context.init(null, new X509TrustManager[]{new X509TrustManager() {
                public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }

                public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    try {
                        chain[0].checkValidity();
                    } catch (final Exception e) {

                        mActivity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
                                AlertDialog alertDialog = builder.create();
                                alertDialog.setCancelable(false);
                                String message = "There a problem with the security certificate for this web site.";
                                message += "\nDo you want to continue anyway?";
                                alertDialog.setTitle("SSL Certificate Error");
                                alertDialog.setMessage(message);
                                alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        acceptSSL = true;
                                        return;

                                    }
                                });

                                alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        acceptSSL = true;
                                        task.onInterruptedDownload();
                                    }
                                });
                                alertDialog.show();

                            }

                        });

                        while( !acceptSSL){
                            try{
                                Thread.sleep(1000);
                            } catch( InterruptedException er) { }
                        }

                    }
                }
                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }
            }}, new SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
        } catch (Exception e) { // should never happen
            e.printStackTrace();
        }
    }

在调用 Jsoup 之前调用此方法。 google play 上的 SSL 警告也消失了。

【讨论】:

    猜你喜欢
    • 2021-02-04
    • 2019-01-26
    • 1970-01-01
    • 2022-08-09
    • 2015-01-24
    • 2018-07-14
    • 1970-01-01
    • 2022-06-16
    • 2012-01-24
    相关资源
    最近更新 更多