Android開發(fā)中如果采用的okhttp作為網(wǎng)絡(luò)請求框架,則可以使用Chucker實(shí)現(xiàn)手機(jī)網(wǎng)絡(luò)請求日志打?。?/p>
https://github.com/ChuckerTeam/chucker
但是上面的方式有局限性,所以測試經(jīng)常會(huì)用到網(wǎng)絡(luò)抓包來查看網(wǎng)絡(luò)請求錯(cuò)誤。
在Android6.0 及以下系統(tǒng)可以抓包,而 Android7.0 及以上系統(tǒng)不能再抓包了,因?yàn)锳ndroid7.0及以上系統(tǒng)版本新增了證書驗(yàn)證,所以 app 內(nèi)不再像原來一樣默認(rèn)信任用戶的證書了。
為了讓測試能在抓包,一般都會(huì)在AndroidManifest.xml文件中配置network-security-config來實(shí)現(xiàn)。
允許抓包
為了讓測試可抓包,配置如下:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates
overridePins="true"
src="system" />
<certificates
overridePins="true"
src="user" />
</trust-anchors>
</base-config>
</network-security-config>
然后在AndroidManifest.xml文件的application節(jié)點(diǎn)配置:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.demo"
android:versionCode="1"
android:versionName="1.0" >
...
<application
android:allowBackup="true"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
android:supportsRtl="true" >
...
</application>
</manifest>
為了實(shí)現(xiàn)抓包,測試,開發(fā)階段可以這么配置,但是發(fā)布階段需要禁止抓包就不能這么配置。
禁止抓包
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">example.com</domain>
</domain-config>
</network-security-config>
這樣配置不一樣了,需要?jiǎng)討B(tài)進(jìn)行設(shè)置。
Google官方文檔配置如下:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true"/>
<debug-overrides>
<trust-anchors>
<certificates
overridePins="true"
src="system" />
<certificates
overridePins="true"
src="user" />
</trust-anchors>
</debug-overrides>
</network-security-config>
使用 debug-overrides 指定僅在 android:debuggable 為 true 時(shí)才可信的僅調(diào)試CA。
如果需要在其他條件下進(jìn)行動(dòng)態(tài)配置,就不能這么設(shè)置了。
第一種方式通過manifestPlaceholders,在app模塊的build.gradle中配置如下代碼:
android {
...
buildTypes {
debug {
...
manifestPlaceholders = [
network_security_config : "@xml/network_security_config_debug"
]
}
release {
...
manifestPlaceholders = [
network_security_config : "@xml/network_security_config_release"
]
}
}
...
}
然后在AndroidManifest.xml文件中配置如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.demo"
android:versionCode="1"
android:versionName="1.0" >
...
<application
android:allowBackup="true"
android:label="@string/app_name"
android:networkSecurityConfig="${network_security_config}"
android:supportsRtl="true" >
...
</application>
</manifest>
還可以通過配置resValue來動(dòng)態(tài)配置:
android {
...
buildTypes {
debug {
...
resValue "xml", "network_security_config", "@xml/network_security_config_debug"
}
release {
...
resValue "xml", "network_security_config", "@xml/network_security_config_release"
}
}
...
}
然后在AndroidManifest.xml文件中配置如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.demo"
android:versionCode="1"
android:versionName="1.0" >
...
<application
android:allowBackup="true"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
android:supportsRtl="true" >
...
</application>
</manifest>
這樣就能實(shí)現(xiàn)動(dòng)態(tài)配置了,非常方便。文章來源:http://www.zghlxwxcb.cn/news/detail-658652.html
感謝大家的支持,如有錯(cuò)誤請指正,如需轉(zhuǎn)載請標(biāo)明原文出處!文章來源地址http://www.zghlxwxcb.cn/news/detail-658652.html
到了這里,關(guān)于Android網(wǎng)絡(luò)安全配置network_security_config的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!