【问题标题】:Android security alert dialogAndroid 安全警报对话框
【发布时间】:2011-04-19 10:56:18
【问题描述】:

当与损坏的服务器建立 HTTPS 连接时,您可能会遇到麻烦,因为 Android 的默认行为是首先抛出 SSLException

所以,我想知道是否有一个标准的安全提示对话框,要求用户对无效证书采取措施,如 WebView 所具有的(使用“继续”、“查看证书”和“取消”选项)?

例如,BlackBerry 会自动显示此类对话框,并在引发错误之前代表用户等待操作。我可以在 Android 中做同样的事情吗?

【问题讨论】:

    标签: android https ssl-certificate


    【解决方案1】:

    我想没有这样的事情 - 浏览器使用自定义对话框并且不会将其暴露给第三方应用程序。至少我找不到任何关于所需行为的提及。顺便说一句,iOS 的行为与 Android 完全一样。

    【讨论】:

      【解决方案2】:

      没有这样的标准对话框,实际上 HttpClient 的默认行为将只接受属于 android 受信任证书存储的证书。

      您可以通过构建自己的信任管理器来做到这一点,然后将其与您的 HttpClient 实例相关联。这看起来像这样:

      public class PromptUserTrustManager implements X509TrustManager
      {
          private AcceptUserSelectedCertsTrustManager(ValidateCertificateCallback callback) throws NoSuchAlgorithmException, KeyStoreException
          {
              KeyStore keyStore = null;
              TrustManagerFactory factory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
              factory.init(keyStore);
              TrustManager [] trustmanagers = factory.getTrustManagers();
              m_standardTrustManager = (X509TrustManager) trustmanagers[0];
          }
      
          @Override
          public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException
          {
          }
      
          // This is where you check the server cert and make the determination
          @Override
          public void checkServerTrusted(X509Certificate[] certChain, String authType)throws CertificateException
          {
              try
              {
                  m_standardTrustManager.checkServerTrusted(certChain,authType);
              }
              catch(CertificateException e)
              {
                  // Cert isn't trusted - popup the error here. You'll need to 
                  // make sure you switch to the UI thread since here you're on a network thread
                  if(!userAcceptsCert(certChain))
                  {
                      throw e;
                  }
              }
          }
      }
      

      所以基本上你所做的是在 checkServerTrusted 回调中询问平台是否信任证书。如果不是,则对信任管理器的调用将引发异常。然后,您可以提示用户他们想要做什么。

      同样的事情可以在 WebView 中使用 onReceivedSslError() 来完成,此时您可以显示一个等效的警告,允许用户根据需要继续操作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-25
        • 2012-02-07
        • 2011-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多