我的离子应用程序不能在android 9设备上工作,在版本低于9的android设备上工作得很好。应用程序在加载登录页面时无法连接到后端。有什么想法吗?
我使用的环境: Ionic:
离子(离子CLI):^5.0.0角度^7.2.2
科尔多瓦:
cordova (Cordova CLI):8.0.0 Cordova平台: android 8.0.0
系统:
安卓SDK工具: 29 NodeJS : v10 npm : 6.2.0
发布于 2020-02-25 13:27:15
原因
这是由于从Android9开始的这个策略:https://developer.android.com/training/articles/security-config.html#CleartextTrafficPermitted
打算仅使用安全连接连接到目的地的
应用程序可以选择不支持对这些目的地的明文(使用未加密的HTTP协议而不是HTTPS)。
注意:本节中的指导仅适用于Android 8.1 (API级别27)或更低版本的应用程序。从Android 9 (API级别28)开始,默认情况下禁用明文支持。
明文(HTTP通信)现在默认情况下是禁用的,因此它相当于具有如下内容
<domain-config cleartextTrafficPermitted="false">
<domain includeSubdomains="true">*</domain>
</domain-config>解决方案
将所有http调用替换为https URL。
或者..。
强制HTTP (不安全)
如果你真的需要允许某些请求的HTTP流量,你可以通过在Android Manifest中添加一些配置来实现,或者创建一个插件来在每次从头重新构建应用程序时更新清单。
您需要向清单中的<application>标记添加一个属性,因此为了不覆盖所有内容,您需要将<edit-config>标记与mode="merge"一起使用(请参阅:https://cordova.apache.org/docs/en/latest/plugin_ref/spec.html#edit-config )
HTTP不被认为是一种安全协议,并且发送的每个请求(及其有效负载)都可能被攻击者读取,因此要小心
plugin.xml
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="phonegap-plugin-old-http-support-android" version="0.0.1">
<name>OldHttpSupportPlugin</name>
<description>Force HTTP for Android 9+</description>
<license>MIT</license>
<keywords>cordova,android,http</keywords>
<engines>
<engine name="cordova" version=">=6.0.0"/>
<engine name="cordova-android" version=">=7.0.0"/>
</engines>
<platform name="android">
<config-file target="res/xml/config.xml" parent="/*">
<feature name="OldHttpSupportPlugin">
<param name="android-package" value="YOUR.PACKAGE.plugin.OldHttpSupportPlugin"/>
</feature>
</config-file>
<!-- Source file for Android -->
<source-file src="src/android/network_security_config.xml" target-dir="res/xml/" />
<edit-config file="AndroidManifest.xml" target="/manifest/application" mode="merge">
<application android:networkSecurityConfig="@xml/network_security_config" />
</edit-config>
</platform>
</plugin>network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">my.domain.com</domain>
<domain includeSubdomains="true">example.org</domain>
<domain includeSubdomains="true">111.222.333.444</domain>
</domain-config>
</network-security-config>发布于 2020-05-28 00:59:55
您可以在config.xml inside android platform部分添加以下内容:
<edit-config file="AndroidManifest.xml" mode="merge" target="/manifest/application">
<application android:usesCleartextTraffic="true" />
</edit-config>最终结果:
<platform name="android">
...
<edit-config file="AndroidManifest.xml" mode="merge" target="/manifest/application">
<application android:usesCleartextTraffic="true" />
</edit-config>
...
</platform>发布于 2020-10-23 11:54:15
我在Ionic 3 and Cordova Android 9.0上遇到了这个问题
我把这些行放在config.xml文件中解决了这个问题。我的解决方案与@Emilio's Ferrer解决方案略有不同
首先,您需要在文件开头的<widget>目标中添加xmlns:android="http://schemas.android.com/apk/res/android"。没有此设置,您将无法构建您的应用程序。
您的<widget>目标将如下所示。
<widget id="your.id" version="x.x.x" xmlns="http://www.w3.org/ns/widgets" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cdv="http://cordova.apache.org/ns/1.0">然后将此代码添加到<platform name="android"标记中
<edit-config src="platforms/android/app/src/main/AndroidManifest.xml" target="AndroidManifest.xml">
<application android:usesCleartextTraffic="true" />
</edit-config>请注意,这是一种允许通过HTTP请求的解决方法,当然,建议您仅向SSL证书下的地址发出请求。
https://stackoverflow.com/questions/60309622
复制相似问题