2012-10-09 82 views
4

WebView或更具體的說,PhoneGaps CordovaWebView可以使用客戶端證書向服務器進行身份驗證嗎?使用Android WebView通過客戶端證書連接到安全服務器

我知道本地瀏覽器可以使用客戶端證書,但我試圖讓PhoneGap Android應用與需要客戶端證書工作的服務器通信,但無法看到如何操作。我已經嘗試了我在google上看到的各種方法,但它們不適用於Android 4.0或更高版本。任何建議都會大受歡迎。

+0

我感興趣的東西非常相似。我希望Phonegap連接到使用我們自己的證書籤名的內部安全服務器,因此Phonegap應該從我們公司導入CA證書。 – jap1968

回答

7

這是不可能的。在客戶端證書中回答問題所需的代碼在sdk中不可用。如果您在Android SDK看看源WebViewClient你會看到這個方法

/** 
* Notify the host application to handle a SSL client certificate 
* request (display the request to the user and ask whether to 
* proceed with a client certificate or not). The host application 
* has to call either handler.cancel() or handler.proceed() as the 
* connection is suspended and waiting for the response. The 
* default behavior is to cancel, returning no client certificate. 
* 
* @param view The WebView that is initiating the callback. 
* @param handler An ClientCertRequestHandler object that will 
*   handle the user's response. 
* @param host_and_port The host and port of the requesting server. 
* 
* @hide 
*/ 
public void onReceivedClientCertRequest(WebView view, 
     ClientCertRequestHandler handler, String host_and_port) { 
    handler.cancel(); 
} 

你看到的文檔節@hide?這意味着「不要向公衆提供這一點」。我們需要重寫此方法並使用ClientCertRequestHandler的能力,但我們不能。不知道什麼時候谷歌會打開這個API,但它在JellyBean中不可用。

+0

如果您必須使用隱藏的API,請按照以下步驟操作:https://devmaze.wordpress.com/2011/01/18/using-com-android-internal-part-1-introduction/ 但請保留記住API是隱藏的,並且如果可能的話避免它。 – Zlatko

+0

看起來'onReceivedClientCertRequest'在API 21+上可用:https://developer.android.com/reference/android/webkit/WebViewClient.html#onReceivedClientCertRequest(android.webkit.WebView,%20android.webkit.ClientCertRequest) – mcomella

相關問題