我正在编写一个 Android 应用程序,它在 webview
中查看 https
url
。此网页
使用 SSL 的自签名证书并需要客户端证书进行身份验证。
我如何通过 webview
最佳答案
您必须为您的 WebView 类提供一个 WebViewClient 对象。当服务器需要客户端身份验证时,通过 WebViewClient 对象的 onReceivedClientCertRequest
通知 WebView。
例子:
MyWebClient 类。
public class myWebClient extends WebViewClient
{
@Override
public void onReceivedClientCertRequest(WebView view, ClientCertRequest request) {
//HERE YOU CAN DO SOME STUFF TO RETRIEVE KEY AND CERTIFICATES
request.proceed(mPrivateKey, mCertificates);
}
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
super.onReceivedError( view, errorCode,
description, failingUrl);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}
主 Activity .java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
web = (WebView) findViewById(R.id.webview01);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("my_url.com");
}
proceed(PrivateKey k, X509Certificate[] chain)
方法获取客户端私钥和证书链以完成 SSL 二次握手。
关于android - WebView 中的客户端证书身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47180196/