X509TrustManager是否在每个Android应用程序会话中仅一次检查API请求?

拉吉塔·佩雷拉(Rajitha Perera)

最近,我实现了一项安全功能,以检查我的请求是否与有效主机连接。为此,我正在检查该主机的证书,并且在这种情况下使用了X509TrustManager。因此,如果X509TrustManager发现某个无效的证书,它将抛出异常,并根据该异常,向用户显示警报。但是问题是X509TrustManager仅在第一次时抛出异常。但是,当我刷新相同的请求时,我没有收到无效的证书,也没有看到任何警报。下面我添加了我的实现。让我知道我的实现中的任何问题或X509TrustManager的任何已知问题。谢谢并恭祝安康。

final X509TrustManager finalTrustManager = x509TrustManager;
    TrustManager[] trustAllCerts = new TrustManager[0];
    if (finalTrustManager != null) {
        trustAllCerts = new TrustManager[]{
                new X509TrustManager() {

                    public X509Certificate[] getAcceptedIssuers() {
                        return finalTrustManager.getAcceptedIssuers();
                    }

                    @Override
                    public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
                        try {
                            // If Application get any CertificateException in Splash screen we will show related alert in MainActivity
                            // We need to terminate app after showing alert but if we show alert in Splash screen it will get hide when Main Activity get visible.
                            // To avoid this scenario we added this implementation.
                            if (mIsSplashGetInvalidateCertificate && !(mLifecycleManager.getCurrentStackOfActivity().get(0) instanceof SplashActivity)) {
                                mAlertManager.showAlertMessageWithoutDuplicates(mLifecycleManager.getCurrentContext().getResources().getString(R.string.certificate_error_title), mLifecycleManager.getCurrentContext().getResources().getString(R.string.certificate_error_message), (FragmentActivity) mLifecycleManager.getCurrentStackOfActivity().get(0), true);
                            }

                            // Checking the certificate availability of host
                            if ((certs != null && certs.length != 0) && (authType != null && authType.length() != 0)) {
                                finalTrustManager.checkClientTrusted(certs, authType);
                            } else {
                                terminateApplicationWithAlert();
                            }
                        } catch (CertificateException e) {
                            terminateApplicationWithAlert();
                        }
                    }

                    @Override
                    public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
                        try {
                            if (mIsSplashGetInvalidateCertificate && !(mLifecycleManager.getCurrentStackOfActivity().get(0) instanceof SplashActivity)) {
                                mAlertManager.showAlertMessageWithoutDuplicates(mLifecycleManager.getCurrentContext().getResources().getString(R.string.certificate_error_title), mLifecycleManager.getCurrentContext().getResources().getString(R.string.certificate_error_message), (FragmentActivity) mLifecycleManager.getCurrentStackOfActivity().get(0), true);
                            }

                            if ((certs != null && certs.length != 0) && (authType != null && authType.length() != 0)) {
                                finalTrustManager.checkServerTrusted(certs, authType);
                            } else {
                                terminateApplicationWithAlert();
                            }
                        } catch (CertificateException e) {
                            terminateApplicationWithAlert();
                        }
                    }
                }
        };
    }
瑞安·M

您实际上并没有将证书标记为无效,因为您正在捕获CertificateException并吞下它。通过不抛出CertificateException,您告诉HTTP库无效的证书有效,大概是为了避免过多地重新验证证书而进行了缓存。

您需要允许CertificateExceptionX509TrustManager方法中抛出,在HTTP调用站点中捕获错误,并从此处显示对话框。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

多次或一次连接到Android应用程序中的推送程序?

Apache Tomcat是否要在每个托管应用程序中一次安装?

实施X509TrustManager

如何修复Android应用中X509TrustManager的不安全实现

如何知道上一次android应用程序是否崩溃

每个会话仅运行一次Jquery slideDown

如何在X509TrustManager中启用OCSP?

在React Native中每个应用程序安装仅运行一次函数(类组件)

如何更改活动当服务器中的标志更改时,应用程序应每30秒#android检查一次服务器?

上一次调试会话中的应用程序见解

如何在每个会话中仅显示一次警告?

如何在REACT INFINITE中为每个页面仅发出一次API请求,请滚动此处

在Android应用程序的服务器上仅收到一次消息

Android:如何设置仅显示一次的活动(安装应用程序时)

如何在Android应用程序中仅使一次随机数?

LibGDX Android应用程序仅运行一次

ASIO驱动程序一次仅支持一个应用程序

Android应用程序每天仅检查一次登录

仅在iOS应用程序中检查一次互联网连接

如何在Android应用程序中自动验证一次密码

第一次Android应用程序崩溃,但代码中没有错误

一次运行android应用程序

设置Java Web应用程序以仅运行每个SQL脚本一次

Google Play安全警报X509Trustmanager-我的应用程序是否会在5月17日之后从商店中删除?

每个用户会话仅显示一次div?

应用程序仅每 4 次运行一次?(Python)

如何在mysql-flask应用程序中检查一个人是否每天至少登录一次以及如何计算登录会话的长度?

在 nodejs 博客应用程序中仅发布一次请求

在 Asp.Net Core 3 Web 应用程序中仅显示一次模态