提问人:david.s 提问时间:8/29/2017 更新时间:1/11/2023 访问量:1458294
Android 8:不允许明文 HTTP 流量
Android 8: Cleartext HTTP traffic not permitted
问:
收到 Android 8 用户的报告,称我的应用(使用后端 Feed)不显示内容。经过调查,我发现Android 8上发生了以下异常:
08-29 12:03:11.246 11285-11285/ E/: [12:03:11.245, main]: Exception: IOException java.io.IOException: Cleartext HTTP traffic to * not permitted
at com.android.okhttp.HttpHandler$CleartextURLFilter.checkURLPermitted(HttpHandler.java:115)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:458)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:127)
at com.deiw.android.generic.tasks.AbstractHttpAsyncTask.doConnection(AbstractHttpAsyncTask.java:207)
at com.deiw.android.generic.tasks.AbstractHttpAsyncTask.extendedDoInBackground(AbstractHttpAsyncTask.java:102)
at com.deiw.android.generic.tasks.AbstractAsyncTask.doInBackground(AbstractAsyncTask.java:88)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
(我删除了软件包名称、URL 和其他可能的标识符)
在 Android 7 及更低版本上一切正常,我没有在 Manifest 中设置(并将其设置为无济于事,无论如何这是默认值),我也不使用网络安全信息。如果我调用 ,它会返回 Android 8,对于旧版本,使用相同的 apk 文件。
我试图在 Google 上找到一些关于 Android O 的信息,但没有成功。android:usesCleartextTraffic
true
NetworkSecurityPolicy.getInstance().isCleartextTrafficPermitted()
false
true
答:
好的,我已经想通了。这是由于我添加了 Manifest 参数,因为我们也有 Instant App 版本 - 它应该确保用户从 Instant App 升级到常规应用程序后,他不会在传输时丢失数据。然而,正如模糊的描述所暗示的那样:android:targetSandboxVersion="2"
指定此应用要使用的目标沙盒。更高的 sanbox 版本将具有更高的安全级别。
此属性的默认值为 1。
它显然也增加了新的安全策略级别,至少在 Android 8 上是这样。
在AndroidManifest中,我找到了这个参数:
android:networkSecurityConfig="@xml/network_security_config"
@xml/network_security_config在network_security_config.xml中定义为:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<!--Set application-wide security config using base-config tag.-->
<base-config cleartextTrafficPermitted="false"/>
</network-security-config>
只是我把cleartextTrafficAllowed改成了true
评论
根据网络安全配置 -
从 Android 9(API 级别 28)开始,明文支持已停用 默认情况下。
还可以看看 Android M 和对明文流量的战争
来自 Google 的 Codelabs 说明
选项 1 -
首先尝试点击 URL,而不是https://
http://
选项 2 -
创建文件res/xml/network_security_config.xml
-
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">api.example.com(to be adjusted)</domain>
</domain-config>
</network-security-config>
AndroidManifest.xml
-
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:networkSecurityConfig="@xml/network_security_config"
...>
...
</application>
</manifest>
选项 3 -
AndroidManifest.xml
-
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:usesCleartextTraffic="true"
...>
...
</application>
</manifest>
此外,正如 @david.s 的回答所指出的,也可能是一个问题——android:targetSandboxVersion
根据 Manifest Docs -
android:targetSandboxVersion
此应用要使用的目标沙盒。沙盒版本越高 数字,安全级别越高。其默认值为 1;你 也可以设置为 2。将此属性设置为 2 会将应用切换为 一个不同的 SELinux 沙盒。以下限制适用于 2 级沙盒:
- 网络安全配置中的默认值为 false。
usesCleartextTraffic
- 不允许共享 Uid。
所以选项 4 -
如果你有,那么把它减少到android:targetSandboxVersion
<manifest>
1
AndroidManifest.xml
-
<?xml version="1.0" encoding="utf-8"?>
<manifest android:targetSandboxVersion="1">
<uses-permission android:name="android.permission.INTERNET" />
...
</manifest>
评论
android:usesCleartextTraffic="true"
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">***Your URL(ex: 127.0.0.1)***</domain>
</domain-config>
</network-security-config>
在上面提供的建议中,我提供了我的 URL 作为 http://xyz.abc.com/mno/
我把它改成 xyz.abc.com 然后它开始工作。
评论
它可能对某人有用。
我们最近在 Android 9 上遇到了同样的问题,但我们只需要在 WebView 中显示一些 Url,没什么特别的。因此,添加到 Manifest 是有效的,但我们不想为此损害整个应用程序的安全性。
所以解决方法是将链接从android:usesCleartextTraffic="true"
http
https
评论
如果可能,请将您的 URL 从 HTTP
更改为 HTTPS
;
它奏效了!!
评论
您可能只想在调试时允许明文,但保留在生产环境中拒绝明文的安全优势。这对我来说很有用,因为我针对不支持 https 的开发服务器测试我的应用程序。以下是如何在生产环境中强制执行 https,但在调试模式下允许明文:
在 build.gradle 中:
// Put this in your buildtypes debug section:
manifestPlaceholders = [usesCleartextTraffic:"true"]
// Put this in your buildtypes release section
manifestPlaceholders = [usesCleartextTraffic:"false"]
在 AndroidManifest.xml 中的应用程序标记中
android:usesCleartextTraffic="${usesCleartextTraffic}"
评论
我在 Android 9 中的问题是使用 http 在域上的 Web 视图上导航 这个答案的解决方案
<application
android:networkSecurityConfig="@xml/network_security_config"
...>
和:
res/xml/network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
</network-security-config>
评论
gradle
好的,这不是⇒⇒⇐⇐成千上万的重复,而是一个基于此的提示,但会给你带来额外的好处(也许还有一些背景信息)。add it to your Manifest
以下解决方案允许您为每个环境设置协议(HTTP / HTTPS)。
这样,您就可以用于您的开发环境和生产环境,而无需一直更改它!
这是必需的,因为通常您没有用于本地或开发环境的 https 证书,但它是生产(也许用于暂存)环境的必备条件。http
https
Android 对 src-Directory 有一种覆盖功能。
默认情况下,您有
/app/src/main
但是您可以添加其他目录来覆盖您的 AndroidManifest.xml。其工作原理如下:
- 创建目录 /app/src/debug
- 在内部创建 AndroidManifest.xml
在这个文件中,你不必把所有的规则都放进去,而只需要把你喜欢从你的/app/src/main/AndroidManifest.xml中覆盖的规则
下面是一个请求的 CLEARTEXT-Permission 的示例:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yourappname">
<application
android:usesCleartextTraffic="true"
android:name=".MainApplication"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:allowBackup="false"
android:theme="@style/AppTheme">
</application>
</manifest>
有了这些知识,现在可以很容易地像 1,2,3 一样,根据你的调试 | main | release Enviroment,重载你的权限。
它的最大好处是......你的生产清单中没有调试的东西,你保持了一个直接和易于维护的结构
评论
对于 React Native 项目
它已经固定在 RN 0.59 上。 您可以在升级时找到从 0.58.6 到 0.59 的差异,您可以在不升级 RN 版本的情况下应用它,请按照以下步骤操作:
创建文件:
android/app/src/debug/res/xml/react_native_config.xml -
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="false">localhost</domain>
<domain includeSubdomains="false">10.0.2.2</domain>
<domain includeSubdomains="false">10.0.3.2</domain>
</domain-config>
</network-security-config>
android/app/src/debug/AndroidManifest.xml -
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<application tools:targetApi="28"
tools:ignore="GoogleAppIndexingWarning"
android:networkSecurityConfig="@xml/react_native_config" />
</manifest>
检查接受的答案以了解根本原因。
评论
升级到 React Native 0.58.5 或更高版本。
他们在 RN 0.58.5 的配置文件中有。includeSubdomain
在 Rn 0.58.5 中,他们使用其服务器域进行了声明。网络安全配置允许应用允许来自特定域的明文流量。因此,无需通过在清单文件的应用程序标记中声明来付出额外的努力。升级 RN 版本后,该问题将自动解决。network_security_config
android:usesCleartextTraffic="true"
要将这些不同的答案应用于 ,可以使用类和程序集级别的 Attributes,而不是手动编辑Xamarin.Android
AndroidManifest.xml
当然需要互联网许可(呃..):
[assembly: UsesPermission(Android.Manifest.Permission.Internet)]
注: 通常,程序集级别属性会添加到文件中,但任何文件,在 works 下方和上方。AssemblyInfo.cs
using
namespace
然后,在 Application 子类(如果需要,创建一个子类)上,可以添加对文件的引用:NetworkSecurityConfig
Resources/xml/ZZZZ.xml
#if DEBUG
[Application(AllowBackup = false, Debuggable = true, NetworkSecurityConfig = "@xml/network_security_config")]
#else
[Application(AllowBackup = true, Debuggable = false, NetworkSecurityConfig = "@xml/network_security_config"))]
#endif
public class App : Application
{
public App(IntPtr javaReference, Android.Runtime.JniHandleOwnership transfer) : base(javaReference, transfer) { }
public App() { }
public override void OnCreate()
{
base.OnCreate();
}
}
在文件夹中创建一个文件(如果需要,请创建文件夹)。Resources/xml
xml
示例文件,根据需要进行调整(请参阅其他答案)xml/network_security_config
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">www.example.com</domain>
<domain includeSubdomains="true">notsecure.com</domain>
<domain includeSubdomains="false">xxx.xxx.xxx</domain>
</domain-config>
</network-security-config>
您还可以在以下位置使用参数:UsesCleartextTraffic
ApplicationAttribute
#if DEBUG
[Application(AllowBackup = false, Debuggable = true, UsesCleartextTraffic = true)]
#else
[Application(AllowBackup = true, Debuggable = false, UsesCleartextTraffic = true))]
#endif
评论
UsesCleartextTraffic = true
在开发我的应用程序时,我也遇到了同样的“不允许明文 HTTP 流量”错误。我在我的应用程序中使用 Retrofit2 进行网络调用,我有两个项目环境(开发和生产)。我的生产域具有带有 HTTPS 调用的 SSL 证书,而开发人员没有 https。配置将添加到构建风格中。但是当我更改为 dev 时,会触发此问题。因此,我为此添加了以下解决方案。
我在清单中添加了明文流量
android:usesCleartextTraffic="true"
然后,我在改造配置类 OKHttp 创建时添加了一个连接规范。
.connectionSpecs(CollectionsKt.listOf(ConnectionSpec.MODERN_TLS, ConnectionSpec.CLEARTEXT))
下面给出了完整的 OkHttpClient 创建
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(10, TimeUnit.SECONDS)
.connectTimeout(10, TimeUnit.SECONDS)
.cache(null)
.connectionSpecs(CollectionsKt.listOf(ConnectionSpec.MODERN_TLS, ConnectionSpec.CLEARTEXT))
.addInterceptor(new NetworkInterceptor(context))
.addInterceptor(createLoggingInterceptor())
.addInterceptor(createSessionExpiryInterceptor())
.addInterceptor(createContextHeaderInterceptor())
.build();
更改 API 版本 9.0 后,出现错误 Cleartext HTTP traffic to YOUR-API.DOMAIN.COM not allowed (targetSdkVersion=“28”)。在 Xamarin、Xamarin.Android 和 Android Studio 中。
在 xamarin、xamarin.android 和 android studio 中解决此错误的两个步骤。
步骤 1:创建文件 resources/xml/network_security_config.xml
在network_security_config.xml
<?xml version="1.0" encoding="utf-8" ?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">mobapi.3detrack.in</domain>
</domain-config>
</network-security-config>
第 2 步:更新 AndroidManifest.xml -
在应用程序标记上添加 android:networkSecurityConfig=“@xml/network_security_config”。 例如:
<application android:label="your App Name" android:icon="@drawable/icon" android:networkSecurityConfig="@xml/network_security_config">
就我而言,该URL在浏览器中也不起作用。
webView.loadUrl("https://www.google.com/")
它对我有用。
评论
在标头中添加此参数解决了我在 apiSauce React Native 中的问题
"Content-Type": "application/x-www-form-urlencoded",
Accept: "application/json"
只需在AndroidManifest.xml文件中添加android:usesCleartextTraffic=“true”
创建文件 - res / xml / network_security.xml
在 network_security.xml ->
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">192.168.0.101</domain>
</domain-config>
</network-security-config>
打开 AndroidManifests.xml :
android:usesCleartextTraffic="true" //Add this line in your manifests
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
android:theme="@style/AppTheme">
2019 年 12 月更新 ionic - 4.7.1
<manifest xmlns:tools=“http://schemas.android.com/tools”>
<application android:usesCleartextTraffic=“true” tools:targetApi=“28”>
请在android清单.xml文件中添加上述内容
Ionic 的早期版本
确保你的 In Ionic 项目中包含以下内容:
config.xml
<edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android"> <application android:networkSecurityConfig="@xml/network_security_config" /> <application android:usesCleartextTraffic="true" /> </edit-config>
运行 ionic Cordova build android。它在“平台”下创建Android文件夹
打开 Android Studio 并打开项目中存在的 Android 文件夹 project-platforms-android 中。让它停留几分钟,以便它构建 gradle
完成后,我们收到一些错误,包括在 . 现在我们要做的只是从 .
gradle build
minSdVersion
manifest.xml
<uses-sdk android:minSdkVersion="19" />
manifest.xml
确保将其从两个位置中删除:
- 应用→清单→。
AndroidManifest.xml
- CordovaLib → 表现为 → 。
AndroidManifest.xml
现在尝试再次构建 gradle,现在它构建成功了
- 应用→清单→。
请确保在应用→清单→的“应用程序”标记中具有以下内容:
Androidmanifest.xml
<application android:networkSecurityConfig="@xml/network_security_config" android:usesCleartextTraffic="true" >
打开 (app → res → xml → )。
network_security_config
network_security_config.xml
添加以下代码:
<?xml version="1.0" encoding="utf-8"?> <network-security-config> <domain-config cleartextTrafficPermitted="true"> <domain includeSubdomains="true">xxx.yyyy.com</domain> </domain-config> </network-security-config>
这是您的 HTTP API 的链接。请确保在 URL 之前不包含任何 Http。xxx.yyyy.com
注意:现在使用 Android Studio 构建应用程序(Build -- Build Bundle's/APK -- Build APK),现在您可以使用该应用程序,它在 Android Pie 中运行良好。如果您尝试使用 ionic Cordova build android 构建应用程序,它会覆盖所有这些设置,因此请确保使用 Android Studio 构建项目。
如果您安装了任何旧版本的应用程序,请卸载它们并尝试一下,否则您将留下一些错误:
未安装应用程序
评论
对于 Xamarin.Android 开发人员,请确保将 HttpClient 实现和 SSL/TLS 设置为“默认”。
它可以在 Andorid 选项 -> 高级 Android 选项下找到。
这样做是出于安全原因,您应该始终尽可能使用 HTTPS(HTTP 安全)。
你可以在这里阅读更多关于它的信息
根据您的情况,此问题有多种解决方案。
如果您尝试与第一方服务进行通信,IE:您自己的 Web 服务器
服务器端:您应该向该服务器添加 HTTPS 支持,并使用 HTTPS 而不是 HTTP。如今,您甚至可以使用 LetsEncrypt 等
服务免费执行此操作 客户端:如果您使用的是可以切换到的包中的包,则它具有相似的 API,如果不是完全相同的话,因此切换应该毫不费力。HttpURLConnection
java.net
HttpsURLConnection
java.net.ssl
如果您使用的是第三方服务,例如 Google、Facebook、天气服务等。
如果您与之通信的服务支持 HTTPS(它很可能支持 HTTPS),您只需将请求 URL 从 更改为 。http://abc.xyz
https://abc.xyz
作为最后的手段,如果您要与之通信的第三方服务不支持 HTTPS 或任何其他形式的安全通信,您可以使用此答案,但同样,不建议这样做,因为它破坏了这个急需的安全功能的目的。
评论
对我来说,有效的答案是@PabloCegarra:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
</network-security-config>
您可能会收到有关cleartextTrafficPermitted="true"
如果您知道“白名单”的域,则应将接受的答案和上述答案混合在一起:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="false">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">books.google.com</domain>
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</domain-config>
</network-security-config>
此代码对我有用,但我的应用程序只需要从 books.google.com 检索数据。 这样,安全警告就会消失。
我已经从已经存在的android清单文件中删除了这一行
android:networkSecurityConfig="@xml/network_security_config"
并添加了
android:usesCleartextTraffic="true"
这在清单中的应用程序标记中
<application
android:usesCleartextTraffic="true"
android:allowBackup="true"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme"
>
那么这个错误明文HTTP流量不允许 overlay.openstreetmap.nl 在android 9和10中消失了,我希望这也适用于android 8,如果它有帮助,你不要忘记投票谢谢
评论
cleartext support is disabled by default.Android in 9 and above
Try This one I hope It will work fine
1 Step:-> add inside android build gradle (Module:App)
useLibrary 'org.apache.http.legacy'
android {
compileSdkVersion 28
useLibrary 'org.apache.http.legacy'
}
然后 2 步:->清单 添加内部清单应用程序标记
<application
android:networkSecurityConfig="@xml/network_security_config">//add drawable goto Step 4
// Step --->3 add to top this line
<uses-library
android:name="org.apache.http.legacy"
android:required="false" />
</application>
步骤4-->>创建Drawable>>Xml文件>>名称为>> network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
</network-security-config>
评论
如果您使用的是 ionic 并在本机 http 插件期间出现此错误,则需要进行以下修复 -
转到将其更改为-resources/android/xml/network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">localhost</domain>
<domain includeSubdomains="true">api.example.com(to be adjusted)</domain>
</domain-config>
</network-security-config>
这对我有用!
明文是任何未加密或打算加密的传输或存储的信息。
当应用使用明文网络流量(如 HTTP(而非 https))与服务器通信时,可能会增加黑客攻击和篡改内容的风险。第三方可以注入未经授权的数据或泄露有关用户的信息。这就是为什么鼓励开发人员仅保护流量(例如 HTTPS)的原因。以下是解决此问题的实现和参考。
Oneliner 解决您的问题。我假设您将 URL 存储在 myURL 字符串中。添加此行,您就完成了。 myURL = myURL.replace(“http”, “https”);
尝试使用“https://”而不是“http://”点击网址
评论
我使用带有cordova-plugin-whitelist 1.3.4的Cordova 8 它的默认配置我的应用程序无法访问互联网,我只在清单中添加了一个参数.xml -> android:usesCleartextTraffic=“true”
mainfest 的路径在 Cordova 8 中发生了变化:platform/android/app/src/main/AndroidManifest.xml。
<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="10000" android:versionName="1.0.0" package="io.cordova.hellocordova" xmlns:android="http://schemas.android.com/apk/res/android">
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
<application
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:usesCleartextTraffic="true">
<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
<intent-filter android:label="@string/launcher_name">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
这真的很愚蠢,因为很明显您的应用程序需要访问互联网。
添加。。。 android:使用CleartextTraffic=“true” ...清单文件可能看起来可以解决问题,但它会对数据完整性造成威胁。
出于安全原因,我在清单文件中使用了带有android:usesCleartextTraffic的清单占位符(如已接受答案的选项3,即@Hrishikesh Kadam的响应),以仅允许在调试环境中使用明文。
在我的build.gradle(:app)文件中,我添加了一个清单占位符,如下所示:
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
manifestPlaceholders.cleartextTrafficPermitted ="true"
}
}
请注意上面这一行中的占位符名称 cleartextTrafficAllowed
manifestPlaceholders.cleartextTrafficPermitted ="true"
然后在我的 Android 清单中,我使用了相同的占位符......
AndroidManifest.xml -
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:usesCleartextTraffic="${cleartextTrafficPermitted}"
...>
...
</application>
</manifest>
这样,明文流量仅在调试环境下才被允许。
简单易懂的解决方案 [Xamarin Form]
对于Android
- 转到 ,然后单击 ,
Android Project
Properties
- 打开并粘贴此代码:
AssemblyInfo.cs
[assembly: Application(UsesCleartextTraffic =true)]
对于iOS
用:NSAppTransportSecurity
您必须在文件中将键设置为字典下。NSAllowsArbitraryLoads
YES
NSAppTransportSecurity
info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
评论
将以下内容放入您的:resources/android/xml/network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
这解决了 Android 上的 Cordova / Ionic 问题。Failed to load resource: net::ERR_CLEARTEXT_NOT_PERMITTED
videoView 打不开此视频 在线视频
创建文件 res/xml/network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
</network-security-config>
应用程序中 AndroidManifest.xml 文件中的新功能:
android:networkSecurityConfig="@xml/network_security_config"
https://techprogrammingideas.blogspot.com/2021/02/android-code-for-displaying-video-with.html
https://youtu.be/90hWWAqfdUU
评论
我在简单的 WebView 应用程序中遇到了同样的问题,因为我的 URL http:// 但当您将其转到 https:// 时,错误得到了解决。
评论
我们面临着一个非常相似的问题,弄清楚它很痛苦。对于那些将不得不处理这个问题的不幸灵魂,以下是我们所做的:
ionic 应用程序在浏览器(Chrome 和 Firefox)中运行良好,已部署到 Apple 的 App Store,并且与我们在 AWS 上的 API 通信良好。然后我们选择了 Android 版本。在模拟器中,我们的 https API 请求甚至不会被发送出去。在物理设备上,https 请求也不会被发送出去(在 Google Play 上部署到“内部测试”之后)
在模拟器中运行我们的应用程序 打开 chrome,键入“chrome://inspect”,等待,单击指向模拟器实例的链接,并能够查看网络选项卡。请求将快速从(挂起)循环到(已取消),而不会发送到服务器。
查看了整个stackoverflow,主要建议是使用cleartextAllowed工作禁用https,这是一个糟糕的主意。
另一个常见的建议是使用原生 http,但如果我们必须有 2 个代码库,并且我想保留我的 http 拦截器,那么使用 ionic 有什么意义。
添加了一个network_security_config.xml文件(这可能不是绝对必要的,但它会移除 Android Studio 中的编译警告。
通过单击浏览器中的锁定图标检查我们服务器上的SSL证书。看起来不错。(也许是因为它真的不是,但我有一个我很久以前添加并忘记的手动覆盖)。 还有很多其他的追尾......
最后使用sslshopper的工具来验证我们的SSL证书:https://www.sslshopper.com/ssl-checker.html
事实证明,虽然我们的证书没问题,但证书链却不行。这将在他们的诊断中显示为红色的折线箭头。
基本上,您必须获取 SSL 证书并创建一个捆绑包,方法是附加提供证书的组织的 SSL 证书,并附加认证验证者的任何其他 SSL 证书,直到整个链干净为止。我们使用了提供的捆绑包,但它没有充分通过链向上。
修复 SSL 链:
cat your-purchased-cert-site-com.crt > your-site.bundle.crt
cat other-org-cert-sectigoRSA-bla-bla-bla.crt >> your-site.bundle.crt
cat another-org-cert-USERTrust-bla-bla-bla.crt >> your-site.bundle.crt
cat some-final-high-level-org-cert.crt >> your-site.bundle.crt
然后,在我们的例子中,对于 ubuntu 上的 nginx:
将 your-site.bundle.crt 放在可以使用的地方。(在我们的例子中,是 /var/ssl)
update /etc/nginx/sites-available/site-name.conf:
ssl_certificate /var/ssl/your-site.bundle.crt
ssl_certificate /var/ssl/your-private-key.key
并重新启动您的 Web 服务器(在我们的例子中是 nginx:sudo systemctl restart nginx)
使用 SSLShopper 的检查器检查它,您应该一直看到绿色箭头。
启动了我们的模拟器,API 调用直接通过。
检查该域是否是 .network_security_config.xml
就我而言,我使用了一个更改域的命令。--external
ionic cordova
解决方案 1
删除--external
解决方案 2
修改您的域名network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">
YOUR_DOMAIN (ex: localhost)
</domain>
</domain-config>
</network-security-config>
我建议同时添加 dev 和 prod 网络配置:
添加 res/xml/network_security_config_dev.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">10.0.2.2</domain>
</domain-config>
</network-security-config>
添加res/xml/network_security_config_prod.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="false">
<domain includeSubdomains="true">yourdomain.com</domain>
</domain-config>
</network-security-config>
在 Gradle Scripts(在 android studio 中)下,找到 build.gradle (android.app) 并查找 buildTypes: release 和 debug(如果不存在,则创建):
buildTypes {
release {
minifyEnabled false
manifestPlaceholders.securityConfig = "@xml/network_security_config_prod"
}
debug {
manifestPlaceholders.securityConfig = "@xml/network_security_config_dev"
}
}
在 AndroidManifest.xml 中使用 securityConfig 占位符,如下所示(在 build.gradle 中定义):
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:networkSecurityConfig="${securityConfig}" <------- here
评论