Tôi đã đối phó với điều này và thẳng thắn cho phép các cuộc tấn công MITM là một o-không. Đây là một giải pháp sạch hơn hỗ trợ ghim. Lưu chứng chỉ vào thư mục tài nguyên thô của bạn.
LƯU Ý: Đáng buồn thay, SSLError cho chúng ta một SslCertificate khi bạn gọi getCertificate(). SslCertificate là loại vô dụng. Đó là API công khai không cho phép bạn xác minh khóa công khai, chỉ được tạo vào ngày hết hạn, được cấp cho, do. Tuy nhiên, nếu bạn mở lớp này, bạn sẽ thấy một biến thành viên X509Certificate không bị lộ ra. IDK lý do tại sao quyết định thiết kế này được thực hiện. Nhưng có một API để lấy Gói, và biến thành viên Chứng chỉ X509 được lưu trữ trong đó. Vì vậy, chúng tôi truy cập theo cách đó, bởi vì Chứng chỉ có nhiều phương pháp hữu ích hơn trên đó.
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
SslCertificate sslCertificateServer = error.getCertificate();
Certificate pinnedCert = getCertificateForRawResource(R.raw.your_cert, mContext);
Certificate serverCert = convertSSLCertificateToCertificate(sslCertificateServer);
if(pinnedCert.equals(serverCert)) {
handler.proceed();
} else {
super.onReceivedSslError(view, handler, error);
}
}
public static Certificate getCertificateForRawResource(int resourceId, Context context) {
CertificateFactory cf = null;
Certificate ca = null;
Resources resources = context.getResources();
InputStream caInput = resources.openRawResource(resourceId);
try {
cf = CertificateFactory.getInstance("X.509");
ca = cf.generateCertificate(caInput);
} catch (CertificateException e) {
Log.e(TAG, "exception", e);
} finally {
try {
caInput.close();
} catch (IOException e) {
Log.e(TAG, "exception", e);
}
}
return ca;
}
public static Certificate convertSSLCertificateToCertificate(SslCertificate sslCertificate) {
CertificateFactory cf = null;
Certificate certificate = null;
Bundle bundle = sslCertificate.saveState(sslCertificate);
byte[] bytes = bundle.getByteArray("x509-certificate");
if (bytes != null) {
try {
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
Certificate cert = certFactory.generateCertificate(new ByteArrayInputStream(bytes));
certificate = cert;
} catch (CertificateException e) {
Log.e(TAG, "exception", e);
}
}
return certificate;
}
Nguồn
2017-04-24 17:41:14
xin vui lòng chấp nhận ... –
sẽ giống với anon '' 'webView.setWebViewClient (WebViewClient mới() {@Override public void onReceivedSslError (xem WebView, SslErrorHandler handler, SslError lỗi) { handler.proceed(); } }); '' ' – tyoc213
chính xác những gì tôi cần .. – MKY