您的位置:首页 > 理论基础 > 计算机网络

Android 7.0 https/tls证书配置问题

2017-03-23 09:16 309 查看
最近系统更新到7.0后https/tls网络请求出现异常

SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.


原来Android 7.0以后google增加了网络配置,在https/tls只需要在配置文件中添加相应配置即可完成https协议的网络请求,对于由正式ca签名的证书不需要配置,主要针对自签名的证书。

第一步AndroidManifestw文件添加

<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
<application android:networkSecurityConfig="@xml/network_security_config"
... >
...
</application>
</manifest>


第二步添加res/xml/network_security_config.xml:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config>
<domain includeSubdomains="true">example.com</domain>
<trust-anchors>
<certificates src="@raw/my_cer"/>
</trust-anchors>
</domain-config>
</network-security-config>


以 PEM 或 DER 格式将自签署或非公共 CA 证书添加到 res/raw/my_cer。

在我的项目中采用的是ca的证书始终报java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.后来改为直接采用服务器证书终于通过,不知道是ca证书生成的有问题还是什么,但7.0以下通讯都正常。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 网络