Google 通知我的 Android 应用程序中的 X509TrustManager 接口实现不安全,需要按如下方式更改代码:
要正确处理 SSL 证书验证,请更改中的代码
自定义 X509TrustManager 接口的 checkServerTrusted 方法
每当发生时引发CertificateException或IllegalArgumentException
服务器提供的证书不符合您的要求
期望。对于技术问题,您可以发布到 Stack Overflow
并使用标签“android-security”和“TrustManager”。
如何修改以下代码来解决上述问题?
public EasySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
super(truststore);
TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
mContext.init(null, new TrustManager[] { tm }, null);
}
我已经使用以下代码解决了这个问题:
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
try {
chain[0].checkValidity();
} catch (Exception e) {
throw new CertificateException("Certificate not valid or trusted.");
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)