从Android API level 24开始,可以定义网络安全配置并从Android Manifest中引用它:
<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
<application android:networkSecurityConfig="@xml/network_security_config"
... >
...
</application>
</manifest>来源:https://developer.android.com/training/articles/security-config.html#manifest (accessed 2021-08-10)
我有一个用例,其中我正在使用的Android库中包含了许多CA证书。我想将我的安全配置限制为使用信任锚的这些证书。
网络安全配置允许:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config>
<domain includeSubdomains="true">example.com</domain>
<trust-anchors>
<certificates src="@raw/my_ca"/>
</trust-anchors>
</domain-config>
</network-security-config>来源:https://developer.android.com/training/articles/security-config.html#ConfigCustom (accessed 2021-08-10)
但是证书位于我的Android库中,我不想复制这些文件。如何从我的应用网络安全配置中引用库CA证书?
发布于 2021-08-11 08:49:53
CA证书应放在库项目中的res/raw文件夹中,而不是assets文件夹中的中。这样,就可以通过网络安全配置使用库引用来自应用程序的证书。
至少现在一切都可以编译了。虽然,由于另一个问题,我还没有验证完全工作的设置,请参阅:Combine Network Security Configuration with OkHttp and custom CAs
https://stackoverflow.com/questions/68723761
复制相似问题