2011-04-19 82 views
0

当进行到一个破碎的服务器,你可以遇到麻烦由于Android的默认行为是抛出异常SSLException在首位HTTPS连接。Android安全警告对话框

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

例如黑莓自动示出了这样的对话框,并等待提高错误之前代表用户的动作。我可以在Android中做同样的事吗?

回答

0

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

0

没有标准对话框,这样,事实上的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回调中,你问平台是否信任证书。如果不是,那么对信任管理器的调用会抛出异常。然后你可以提示用户他们想要做什么。

同样的事情可以使用web视图onReceivedSslError()此时您可以显示一个等效的警告允许用户如果他们希望继续进行。