webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
super.onReceivedSslError(view, handler, error);
}
}
/**
* SSL证书错误,手动校验https证书
*
* @param cert https证书
* @param sha256Str sha256值
* @return true通过,false失败
*/
public static boolean isSSLCertOk(SslCertificate cert, String sha256Str) {
byte[] SSLSHA256 = hexToBytes(sha256Str);
Bundle bundle = SslCertificate.saveState(cert);
if (bundle != null) {
byte[] bytes = bundle.getByteArray("x509-certificate");
if (bytes != null) {
try {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate ca = cf.generateCertificate(new ByteArrayInputStream(bytes));
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
byte[] key = sha256.digest(((X509Certificate) ca).getEncoded());
return Arrays.equals(key, SSLSHA256);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return false;
}
/**
* hexString转byteArr
* <p>例如:</p>
* hexString2Bytes("00A8") returns { 0, (byte) 0xA8 }
*
* @param hexString
* @return 字节数组
*/
public static byte[] hexToBytes(String hexString) {
if (hexString == null || hexString.trim().length() == 0)
return null;
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] bytes = new byte[length];
String hexDigits = "0123456789abcdef";
for (int i = 0; i < length; i++) {
int pos = i * 2; // 两个字符对应一个byte
int h = hexDigits.indexOf(hexChars[pos]) << 4; // 注1
int l = hexDigits.indexOf(hexChars[pos + 1]); // 注2
if (h == -1 || l == -1) { // 非16进制字符
return null;
}
bytes[i] = (byte) (h | l);
}
return bytes;
}
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
super.onReceivedSslError(view, handler, error);
if (error.getPrimaryError() == SslError.SSL_INVALID) {
// 如果手动校验sha256成功就允许加载页面
if (SSLCertUtil.isSSLCertOk(error.getCertificate(), "6683c9584b8287ec3a50e312f4a540c79938aaeb76bd02e40a9ca037ee5d24f4")) {
handler.proceed();
} else {
try {
new AlertDialog.Builder(MainActivity.this)
.setTitle("警告")
.setMessage("证书校验失败")
.setPositiveButton("退出", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
dialog.dismiss();
}
}).show();
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
handler.cancel();
}
}
});
/** * The certificate is not yet valid */ public static final int SSL_NOTYETVALID = 0; /** * The certificate has expired */ public static final int SSL_EXPIRED = 1; /** * Hostname mismatch */ public static final int SSL_IDMISMATCH = 2; /** * The certificate authority is not trusted */ public static final int SSL_UNTRUSTED = 3; /** * The date of the certificate is invalid */ public static final int SSL_DATE_INVALID = 4; /** * A generic error occurred */ public static final int SSL_INVALID = 5;
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有