Android上的Web视图支持SSL吗?

Android上的Web视图支持SSL吗?,android,Android,android上的WebView控件是否支持SSL 我试图加载使用受信任ssl证书的网页,但WebView仅为白色 有什么建议吗?不是专家,只是我能在网上找到的。 根据我所理解的,WebVIEW确实支持SSL,但是,空白屏幕是WebVIEW不相信证书有效的指示。这可能发生在自签名的证书或来自未在android中设置的根身份验证的证书上(完全有效的证书不会验证)。在任何情况下,如果您正在使用froyo或更好的产品,您可以尝试以下方法: import android.webkit.WebView;

android上的
WebView
控件是否支持SSL

我试图加载使用受信任ssl证书的网页,但
WebView
仅为白色


有什么建议吗?

不是专家,只是我能在网上找到的。 根据我所理解的,WebVIEW确实支持SSL,但是,空白屏幕是WebVIEW不相信证书有效的指示。这可能发生在自签名的证书或来自未在android中设置的根身份验证的证书上(完全有效的证书不会验证)。在任何情况下,如果您正在使用froyo或更好的产品,您可以尝试以下方法:

import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.SslErrorHandler;
import android.net.http.SslError;

...

engine = (WebView) findViewById(R.id.my_webview);
engine.setWebViewClient(new WebViewClient() {

    @Override
    public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed();
    }
});

您必须启用webview设置才能查看基于SSL的网站:

webView.getSetting().setDomStorageEnable(true);

要根据更新的安全策略正确处理SSL证书验证日志播放,请更改代码以在服务器提供的证书满足您的期望时调用SslErrorHandler.continue(),否则调用SslErrorHandler.cancel()

例如,我添加了一个警告对话框,让用户确认,谷歌似乎不再显示警告

    @Override
    public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
    final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
    String message = "SSL Certificate error.";
        switch (error.getPrimaryError()) {
            case SslError.SSL_UNTRUSTED:
                message = "The certificate authority is not trusted.";
                break;
            case SslError.SSL_EXPIRED:
                message = "The certificate has expired.";
                break;
            case SslError.SSL_IDMISMATCH:
                message = "The certificate Hostname mismatch.";
                break;
            case SslError.SSL_NOTYETVALID:
                message = "The certificate is not yet valid.";
                break;
        }
        message += " Do you want to continue anyway?";

        builder.setTitle("SSL Certificate Error");
        builder.setMessage(message);
    builder.setPositiveButton("continue", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            handler.proceed();
        }
    });
    builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            handler.cancel();
        }
    });
    final AlertDialog dialog = builder.create();
    dialog.show();
}

更改后不会显示警告。

Google play拒绝了我的应用程序,然后我这样做了

 @Override
    public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {

        try {

            //Get the X509 trust manager from your ssl certificate
            X509TrustManager trustManager = mySslCertificate.getX509TrustManager();

            //Get the certificate from error object
            Bundle bundle = SslCertificate.saveState(error.getCertificate());
            X509Certificate x509Certificate;
            byte[] bytes = bundle.getByteArray("x509-certificate");
            if (bytes == null) {
                x509Certificate = null;
            } else {
                CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
                Certificate cert = certFactory.generateCertificate(new ByteArrayInputStream(bytes));
                x509Certificate = (X509Certificate) cert;
            }
            X509Certificate[] x509Certificates = new X509Certificate[1];
            x509Certificates[0] = x509Certificate;

            // check weather the certificate is trusted
            trustManager.checkServerTrusted(x509Certificates, "ECDH_RSA");

            Log.e(TAG, "Certificate from " + error.getUrl() + " is trusted.");
            handler.proceed();
        } catch (Exception e) {
            Log.e(TAG, "Failed to access " + error.getUrl() + ". Error: " + error.getPrimaryError());
            final AlertDialog.Builder builder = new AlertDialog.Builder(WebViewActivity.this);
            String message = "SSL Certificate error.";
            switch (error.getPrimaryError()) {
                case SslError.SSL_UNTRUSTED:
                    message = "The certificate authority is not trusted.";
                    break;
                case SslError.SSL_EXPIRED:
                    message = "The certificate has expired.";
                    break;
                case SslError.SSL_IDMISMATCH:
                    message = "The certificate Hostname mismatch.";
                    break;
                case SslError.SSL_NOTYETVALID:
                    message = "The certificate is not yet valid.";
                    break;
            }
            message += " Do you want to continue anyway?";

            builder.setTitle("SSL Certificate Error");
            builder.setMessage(message);
            builder.setPositiveButton("continue", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    handler.proceed();
                }
            });
            builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    handler.cancel();
                }
            });
            final AlertDialog dialog = builder.create();
            dialog.show();
        }
    }
在做出上述更改后,Google play接受了我的apk


要生成您的ssl信任管理器,请检查此

您能用内置的Internet应用程序打开网页吗?这篇文章帮了我很多忙!只需注意,您需要至少开发2.2版本,并为Ssl内容使用以下内容:导入android.webkit.*;导入android.net.http.*;多亏了你的回答,谷歌不得不检查所有播放中的应用程序,这些应用程序都具有相同的精确实现,并向开发者发送警告电子邮件:)要警告任何在生产应用程序中实际使用此功能的人,这将允许对你的应用程序进行MitM攻击。这里的更多信息:是的,事实上,我认为仅仅绕过SSL错误并不是一个好方法,并且会在以后引起问题。我应该想办法在手机的根密钥库中添加站点证书,这样它就可以被信任。但另一方面,仅仅为了在WebView中显示HTTPS url而创建一个完整的证书存储管理系统似乎有些过火。在此方面的任何帮助都是非常感谢的:)如果您这样做,该应用程序在发布/更新时将被google play拒绝。安全警报您的应用程序正在使用带有Apache HTTP客户端的X509TrustManager接口的不安全实现,导致安全漏洞。有关详细信息,包括修复漏洞的截止日期,请参阅谷歌帮助中心的这篇文章。将Android SystemWebView更新到v55将解决一些相关问题。非常感谢:)我非常接近解决方案,您帮助我完成了:)@Viktor Welcome mate:)@Gowsikc,什么是MySSLKC?如果原始文件夹中有证书,如何导入?@Sohail检查答案链接