背景是app中使用的文件下载是采用系统DownloadManager,但没有发现此类中如何配置不校验ssl或者配置在定义证书校验,故发现通过网络安全性配置来解决。即配置AndroidManifest.xml中的NetworkSecureConfig来解决。
-
把自定义的CA证书放到raw目录下;网络安全配置文件放到xml目录下;
如图:
image.png
- CA证书是配置在服务器上的ca证书
- network_security_config.xml内容如下:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config>
<trust-anchors>
<certificates src="@raw/dev_ca_file"/>
<certificates src="system"/>
</trust-anchors>
</base-config>
</network-security-config>
- 在AndroidMinifest.xml文件中配置
<application
android:name="xxx"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:networkSecurityConfig="@xml/network_security_config">
对于服务器自定义https证书的话,此方法解决还是不错的。
如果使用Okhttp库进行下载,则可以在Okhttp通过配置socketFactory的途径来解决受信问题。
[参考链接]
网络安全配置
网友评论