最近测试反馈说,到了H5页面,会显示空白页。排查后发现、由于之前的页面是http,在切换的时候,除了url之外,并未对webview的ssl校验做特殊的处理。
问题就出在这里,webview在加载https的时候,通常会用手机根证书对h5的页面进行校验,但是这个校验不一定成功。失败的时候会回调webviewclient的onReceivedSslError函数。
然后网上查了下就先给出了一个暴力的解决办法,就是在证书校验失败的情况下,将其跳过,继续加载,方法如下
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed(); // 接受信任所有网站的证书
}
事实也证明这个方法确实可以解决问题。。。
事后反过来思考的时候,我们切换https,本来就是想说利用ssl这一证书的校验,保证加载页面的安全性,现在在ssl校验的时候统一绕过了,不就有悖初衷了吗。然后就去看了下android.net.http.SslError这个类的源码,研究下具体的错误类型
public class SslError {
/**
* Individual SSL errors (in the order from the least to the most severe):
*/
/**
* 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;
/**
* The number of different SSL errors.
* @deprecated This constant is not necessary for using the SslError API and
* can change from release to release.
*/
// Update if you add a new SSL error!!!
@Deprecated
public static final int SSL_MAX_ERROR = 6;
...
}
其中
SslError.SSL_INVALID //webviewclient在校验ssl的过程中,出现了bug
然后就改进了上面的方法,在ssl校验失败的情况下,如果是校验过程遇到了bug,那就放过,其他情况就cancel:
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
if (error.getPrimaryError() == android.net.http.SslError.SSL_INVALID){
handler.proceed(); // 接受信任所有网站的证书
} else {
handler.cancel(); // 默认操作 不处理
}
}
这个就先写到这吧,后续有其他的想法再做补充