gpt4 book ai didi

Android WebView 处理 onReceivedClientCertRequest

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:01:58 25 4
gpt4 key购买 nike

我正在 WebView 中使用客户端证书身份验证开发 Android 应用程序。证书 (cert.pfx) 和密码嵌入在应用程序中。

在 WebView 中使用 ajax 调用执行客户端证书身份验证请求时,将调用以下函数:

@Override
public void onReceivedClientCertRequest(WebView view, final ClientCertRequest request) {}

据我所知,我需要调用:

request.proceed(PrivateKey privateKey, X509Certificate[] chain)

知道如何从嵌入式证书创建 PrivateKey 和 X509Certificate 对象以便继续处理请求。顺便说一句,这是在 Android 应用程序上实现客户端证书身份验证的正确方法吗?如果没有,请指教。

最佳答案

使用 KeyStore 获取 PrivateKey 和 X509Certificate 对象解决:

    private X509Certificate[] mCertificates;
private PrivateKey mPrivateKey;

private void loadCertificateAndPrivateKey() {
try {
InputStream certificateFileStream = getClass().getResourceAsStream("/assets/cert.pfx");

KeyStore keyStore = KeyStore.getInstance("PKCS12");
String password = "password";
keyStore.load(certificateFileStream, password != null ? password.toCharArray() : null);

Enumeration<String> aliases = keyStore.aliases();
String alias = aliases.nextElement();

Key key = keyStore.getKey(alias, password.toCharArray());
if (key instanceof PrivateKey) {
mPrivateKey = (PrivateKey)key;
Certificate cert = keyStore.getCertificate(alias);
mCertificates = new X509Certificate[1];
mCertificates[0] = (X509Certificate)cert;
}

certificateFileStream.close();

} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}


private WebViewClient mWebViewClient = new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}

@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}

@Override
public void onReceivedClientCertRequest(WebView view, final ClientCertRequest request) {
if (mCertificates == null || mPrivateKey == null) {
loadCertificateAndPrivateKey();
}
request.proceed(mPrivateKey, mCertificates);
}
};

关于Android WebView 处理 onReceivedClientCertRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35135225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com