Android 7.0 https/tls证书配置问题

最近系统更新到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以下通讯都正常。

猜你喜欢

转载自blog.csdn.net/u011928958/article/details/65434945