Android 8:不允许明文 HTTP 流量

Android 8: Cleartext HTTP traffic not permitted

提问人:david.s 提问时间:8/29/2017 更新时间:1/11/2023 访问量:1458294

问:

收到 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:usesCleartextTraffictrueNetworkSecurityPolicy.getInstance().isCleartextTrafficPermitted()falsetrue

安卓 HTTP HTTPS

评论

6赞 Big McLargeHuge 5/3/2019
这种情况发生在我维护的应用程序上,因为在某些情况下,服务器会从 HTTPS 重定向到 HTTP。
0赞 ikmazameti 4/25/2021
点击这里查看 codechacha.com/en/android-cleartext-http-traffic-issue
0赞 masterxilo 10/29/2022
我正在为 API 级别 30、Android 11 构建,并在清单中的应用程序标签中添加 android:usesCleartextTraffic=“true”
0赞 mckenzm 11/24/2023
在其他地方报道,直到 Android 9 才发生。这对我来说是新闻。

答:

13赞 david.s 8/30/2017 #1

好的,我已经想通了。这是由于我添加了 Manifest 参数,因为我们也有 Instant App 版本 - 它应该确保用户从 Instant App 升级到常规应用程序后,他不会在传输时丢失数据。然而,正如模糊的描述所暗示的那样:android:targetSandboxVersion="2"

指定此应用要使用的目标沙盒。更高的 sanbox 版本将具有更高的安全级别。

此属性的默认值为 1。

它显然也增加了新的安全策略级别,至少在 Android 8 上是这样。

182赞 byOnti 3/21/2018 #2

在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

评论

2赞 1x2x3x4x 4/6/2020
Perfetct。更多信息在这里: codelabs.developers.google.com/codelabs/...
2赞 Gilbert 5/2/2020
更改后重新安装应用程序
0赞 Carlos López Marí 9/15/2021
它对我有用的唯一答案(我必须创建文件并添加参数)。
4赞 c-sharp-and-swiftui-devni 9/20/2021
禁用 ssl 要求不是很好的做法,除非这当然是针对内部应用程序的。但即便如此,也不应该被禁用。
3327赞 Hrishikesh Kadam 6/13/2018 #3

根据网络安全配置 -

从 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 -

android:使用CleartextTraffic文档

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>

评论

7赞 spartygw 8/9/2018
@HrishikeshKadam您的回答非常感谢,但似乎在最新版本的 P 中必须还有另一个步骤?请看我的问题 stackoverflow.com/questions/51770323/...
8赞 Tom Hammond 10/31/2018
明文HTTP是否意味着他们只是使用http站点而不是https?
304赞 林果皞 1/24/2019
如果每个开发人员都要添加,那么这个 Android 安全功能有什么意义?android:usesCleartextTraffic="true"
98赞 Christian Brüggemann 1/29/2019
这甚至没有提到这个问题的最佳解决方案:使用HTTPS。这个答案中提到的选项应该只是最后的手段。
11赞 Karan Harsh Wardhan 2/8/2019
@林果皞,Google Play 商店最终很容易禁止使用此标志的应用程序
54赞 Lorence 9/16/2018 #4
<?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 然后它开始工作。

评论

15赞 The incredible Jan 11/22/2018
域 != URL。“http”是协议。该协议从来都不是域的一部分。
0赞 Martin Zeitler 11/18/2019
这是正确的,那里只支持 FQDN,没有 IP 地址(上面修复了)。
0赞 Gilbert 5/2/2020
不适用于域 10.0.2.2。我应该添加端口号吗?
0赞 Panadol Chong 1/25/2021
如果我使用 IP 地址而不是域怎么办?
35赞 El_o.di 9/17/2018 #5

它可能对某人有用。

我们最近在 Android 9 上遇到了同样的问题,但我们只需要在 WebView 中显示一些 Url,没什么特别的。因此,添加到 Manifest 是有效的,但我们不想为此损害整个应用程序的安全性。 所以解决方法是将链接从android:usesCleartextTraffic="true"httphttps

评论

4赞 The incredible Jan 11/22/2018
如果我只想显示一些 URL,我不需要 WebView。我只使用TextView。;)我想你的意思是你显示一些html页面。只有当您的服务器提供 SSL 时,您的修复才有效。您不能简单地更改链接。
2赞 Dakatine 1/25/2019
这肯定是最好的选择,但不能总是选择它 - 无论是出于性能原因,还是因为资源可能在明文 HTTP 中不可用。
0赞 Robert Williams 5/6/2019
“我们不想损害整个应用程序的安全性”,会造成哪些安全风险?就我而言,没有一个 URL,所以我无法将它们添加到清单中。
0赞 El_o.di 6/11/2019
嗨,@RobertWilliams这只是意味着允许清除流量,不允许非加密流量。这是一篇博文 medium.com/@son.rommer/...
60赞 eli 10/5/2018 #6

如果可能,请将您的 URL 从 HTTP 更改为 HTTPS;

它奏效了!!

评论

162赞 Karan Harsh Wardhan 2/8/2019
这是怎么投票的?如果服务器 URL 不是 HTTPS,则会出现握手异常
27赞 beetstra 2/18/2019
投了赞成票,因为这是正确的做法(在生产环境中)。HTTPS 应该是默认值,而不是 HTTP。
54赞 Martin Price 2/22/2019
@beetsta 您认为您可以完全控制提供内容的内容。因此,这个答案本质上是幼稚或轻率的。
14赞 Bevor 2/24/2019
@beetstra 为什么在调试时 LOCAL 机器上应该是 HTTPS 默认值?这太愚蠢了,只是谷歌家长式作风的另一个例子。幸运的是,可以通过 Tyler 的解决方案来解决调试模式的问题。
12赞 Nick Turner 8/28/2019
答案是对这个问题一无所知。与小公司的人不同,有时您不会为每个临时服务器配备 SSL。答案就像有人在Facebook帖子中纠正语法一样糟糕,这根本没有回答问题,也没有解决问题。
151赞 Tyler 12/12/2018 #7

您可能只想在调试时允许明文,但保留在生产环境中拒绝明文的安全优势。这对我来说很有用,因为我针对不支持 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}"

评论

1赞 Rik van Velzen 3/1/2019
不过,它仅用于 API 23+。如果你想要一个独立于api的解决方案,在以下位置批准的解决方案:stackoverflow.com/questions/46302058/...是一个不错的选择...
1赞 whyoz 6/1/2019
问:当应用程序使用设计上可以是 http 或 https 的 Web 服务器时,如果 http url 需要能够使用 Web 服务,是否也使用 CleartextTraffic:“false”?因此,将其设置为 true 意味着默认情况下 https 服务无论如何都不会发送明文?
260赞 Pablo Cegarra 1/19/2019 #8

我在 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>

评论

0赞 Morozov 11/6/2019
我可以以某种方式重写它吗?gradle
1赞 Gilbert 5/2/2020
对我不起作用。我无法让我的模拟器连接到 10.0.2.2
0赞 Murciegalo84 6/9/2020
在发布版本上,这不是“最安全”的事情。
0赞 Panadol Chong 1/25/2021
如果我想把端口号放在一起怎么办?
71赞 suther 1/25/2019 #9

好的,这不是⇒⇒⇐⇐成千上万的重复,而是一个基于此的提示,但会给你带来额外的好处(也许还有一些背景信息)。add it to your Manifest


以下解决方案允许您为每个环境设置协议(HTTP / HTTPS)。

这样,您就可以用于您的开发环境和生产环境,而无需一直更改它! 这是必需的,因为通常您没有用于本地或开发环境的 https 证书,但它是生产(也许用于暂存)环境的必备条件。httphttps


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,重载你的权限。

它的最大好处是......你的生产清单中没有调试的东西,你保持了一个直接和易于维护的结构

评论

9赞 Coo 4/23/2019
这绝对是正确的解决方案。Android 添加这些安全设置是有原因的,因此它们应该有效。您的解决方案允许我们在本地不安全的环境中进行测试,而生产版本仍将具有建议的安全设置。谢谢!
0赞 mckenzm 11/24/2023
当然,无论如何,在生产中,最后一站几乎总是明文。有人会认为,在开发人员模式下,我们可以使用切换来覆盖它。
30赞 Erick M. Sprengel 3/14/2019 #10

对于 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>

检查接受的答案以了解根本原因。

评论

2赞 Cristian Mora 5/7/2019
我使用 react-native 0.59.5 并且遇到了同样的问题,我们必须按照您的建议手动设置 AndroidManifest.xml。
0赞 Hidayat Arghandabi 5/4/2021
谢谢 它有帮助 由于谷歌政策,我必须使android:usesCleartextTraffic=“false”,然后我遇到了在模拟器中从本地运行react-native的问题,我确实应用了上述步骤,它对我有用。
1赞 Dishant Walia 3/26/2019 #11

升级到 React Native 0.58.5 或更高版本。 他们在 RN 0.58.5 的配置文件中有。includeSubdomain

更改日志

在 Rn 0.58.5 中,他们使用其服务器域进行了声明。网络安全配置允许应用允许来自特定域的明文流量。因此,无需通过在清单文件的应用程序标记中声明来付出额外的努力。升级 RN 版本后,该问题将自动解决。network_security_configandroid:usesCleartextTraffic="true"

8赞 SushiHangover 5/6/2019 #12

要将这些不同的答案应用于 ,可以使用类和程序集级别的 Attributes,而不是手动编辑Xamarin.AndroidAndroidManifest.xml

当然需要互联网许可(呃..):

[assembly: UsesPermission(Android.Manifest.Permission.Internet)]

注: 通常,程序集级别属性会添加到文件中,但任何文件,在 works 下方和上方。AssemblyInfo.csusingnamespace

然后,在 Application 子类(如果需要,创建一个子类)上,可以添加对文件的引用:NetworkSecurityConfigResources/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/xmlxml

示例文件,根据需要进行调整(请参阅其他答案)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>

您还可以在以下位置使用参数:UsesCleartextTrafficApplicationAttribute

#if DEBUG
[Application(AllowBackup = false, Debuggable = true, UsesCleartextTraffic = true)]
#else
[Application(AllowBackup = true, Debuggable = false, UsesCleartextTraffic = true))]
#endif

评论

0赞 c-sharp-and-swiftui-devni 6/13/2019
如果您不在 doamin 上并且位于 192.168 的本地主机地址上,则该应用程序将不会在 Internet 上上上线,而是在本地网络上,这是如何工作的
0赞 c-sharp-and-swiftui-devni 6/13/2019
Xamrian 表单的语法是什么
0赞 SushiHangover 6/13/2019
@rogue39nin 使用我答案的最后一部分:UsesCleartextTraffic = true
5赞 Nithinjith 6/18/2019 #13

在开发我的应用程序时,我也遇到了同样的“不允许明文 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();
1赞 Ripdaman Singh 8/3/2019 #14

更改 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">
2赞 Mayuresh Deshmukh 8/14/2019 #15

就我而言,该URL在浏览器中也不起作用。

我检查 https://www.google.com/

webView.loadUrl("https://www.google.com/")

它对我有用。

评论

0赞 Bay 9/6/2019
myWebView.loadUrl(“www.site.com”);也适用于没有SSL作为HTTPS但只有HTTP的网站管理员。可能会得到空白页,但是。
0赞 Mayuresh Deshmukh 9/6/2019
如果给定的 URL 在您的 Web 浏览器中工作,那么您可以在 WebView 中使用。否则,您会看到此错误。
0赞 Bay 9/7/2019
我知道有时它会出错,但大多数时候我看到空白页,即使 run.javascript 是“true”,我可以正确访问该网站。我不知道为什么我看到空白页,我也设置了可缩放的 true。
1赞 Waleed Arshad 9/16/2019 #16

在标头中添加此参数解决了我在 apiSauce React Native 中的问题

"Content-Type": "application/x-www-form-urlencoded",
  Accept: "application/json"
3赞 Manoj Alwis 10/17/2019 #17

只需在AndroidManifest.xml文件中添加android:usesCleartextTraffic=“true”

4赞 HandyPawan 10/23/2019 #18

创建文件 - 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">
5赞 Gvs Akhil 11/7/2019 #19

2019 年 12 月更新 ionic - 4.7.1

<manifest xmlns:tools=“http://schemas.android.com/tools”>

<application android:usesCleartextTraffic=“true” tools:targetApi=“28”>

请在android清单.xml文件中添加上述内容

Ionic 的早期版本

  1. 确保你的 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>
    
  2. 运行 ionic Cordova build android。它在“平台”下创建Android文件夹

  3. 打开 Android Studio 并打开项目中存在的 Android 文件夹 project-platforms-android 中。让它停留几分钟,以便它构建 gradle

  4. 完成后,我们收到一些错误,包括在 . 现在我们要做的只是从 .gradle buildminSdVersionmanifest.xml<uses-sdk android:minSdkVersion="19" />manifest.xml

    确保将其从两个位置中删除:

    1. 应用→清单→。AndroidManifest.xml
    2. CordovaLib → 表现为 → 。AndroidManifest.xml

    现在尝试再次构建 gradle,现在它构建成功了

  5. 请确保在应用→清单→的“应用程序”标记中具有以下内容:Androidmanifest.xml

    <application
    android:networkSecurityConfig="@xml/network_security_config"  android:usesCleartextTraffic="true" >
    
  6. 打开 (app → res → xml → )。network_security_confignetwork_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 构建项目。

如果您安装了任何旧版本的应用程序,请卸载它们并尝试一下,否则您将留下一些错误:

未安装应用程序

评论

0赞 Weekend 11/15/2019
离子的?科尔多瓦?因此,它不是一个普通的 Android 版本,而是一个使用前端技术构建原生应用程序的框架
0赞 Gvs Akhil 11/15/2019
Ionic 在 android 应用程序中为您提供了 webivew 实现,Cordova 可帮助您访问 android 原生功能,如麦克风、摄像头。
2赞 chaosifier 11/19/2019 #20

对于 Xamarin.Android 开发人员,请确保将 HttpClient 实现和 SSL/TLS 设置为“默认”。

它可以在 Andorid 选项 -> 高级 Android 选项下找到。

enter image description here

2赞 Rosenpin 12/22/2019 #21

这样做是出于安全原因,您应该始终尽可能使用 HTTPS(HTTP 安全)。
你可以在这里阅读更多关于它的信息

根据您的情况,此问题有多种解决方案。

如果您尝试与第一方服务进行通信,IE:您自己的 Web 服务器

服务器端:您应该向该服务器添加 HTTPS 支持,并使用 HTTPS 而不是 HTTP。如今,您甚至可以使用 LetsEncrypt
服务免费执行此操作 客户端:如果您使用的是可以切换到的包中的包,则它具有相似的 API,如果不是完全相同的话,因此切换应该毫不费力。
HttpURLConnectionjava.netHttpsURLConnectionjava.net.ssl

如果您使用的是第三方服务,例如 Google、Facebook、天气服务等。

如果您与之通信的服务支持 HTTPS(它很可能支持 HTTPS),您只需将请求 URL 从 更改为 。http://abc.xyzhttps://abc.xyz

作为最后的手段,如果您要与之通信的第三方服务不支持 HTTPS 或任何其他形式的安全通信,您可以使用此答案,但同样,不建议这样做,因为它破坏了这个急需的安全功能的目的。

评论

0赞 mckenzm 11/24/2023
这样做的问题是绝大多数服务器不运行 https。它在位于服务器场前面的负载平衡器处卸载。要使用 TLS 运行,我需要安装 nginx 并将其 SSL 卸载到 8080 或本地主机上的任何内容。因为嘿,我不是在这里测试 Android,而是在后端测试。所以我不能构建 apk。
7赞 WizardingStudios 1/26/2020 #22

对我来说,有效的答案是@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 检索数据。 这样,安全警告就会消失。

22赞 creativecoder 1/28/2020 #23

我已经从已经存在的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,如果它有帮助,你不要忘记投票谢谢

评论

0赞 Anshul Tyagi 10/26/2021
挽救了我的一天。😊 ☺️ 非常感谢
0赞 Anshul Tyagi 10/26/2021
它不会影响应用程序的任何现有功能吗?
6赞 Ashif 2/18/2020 #24
 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>

评论

0赞 Shadow 3/12/2020
是否可以在 aosp 中更改此设置?
0赞 Ashif 3/14/2020
@Shadow 是的,你可以改变它。
0赞 Shadow 3/16/2020
我可以知道我到底在哪里可以更改它吗?
0赞 Ashif 3/16/2020
@Shadow <base-config cleartextTrafficPermitted=“true”> <trust-anchors> <certificates src=“system” /> </trust-anchors> </base-config> <domain-config cleartextTrafficPermitted=“true”> <domain includeSubdomains=“true”>www.yourwebsidedomain.com</domain> </domain-config>
0赞 Shadow 3/18/2020
不!!你又是在应用程序方面说的。我在问如何在框架/<>文件夹类中更改?
2赞 Leena 4/1/2020 #25

如果您使用的是 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>

这对我有用!

1赞 Gk Mohammad Emon 4/8/2020 #26

明文是任何未加密或打算加密的传输或存储的信息。

当应用使用明文网络流量(如 HTTP(而非 https))与服务器通信时,可能会增加黑客攻击和篡改内容的风险。第三方可以注入未经授权的数据或泄露有关用户的信息。这就是为什么鼓励开发人员仅保护流量(例如 HTTPS)的原因。以下是解决此问题的实现和参考。

-3赞 Pallav Chaudhari 4/17/2020 #27

Oneliner 解决您的问题。我假设您将 URL 存储在 myURL 字符串中。添加此行,您就完成了。 myURL = myURL.replace(“http”, “https”);

0赞 Jose A. de los Santos 5/5/2020 #28

尝试使用“https://”而不是“http://”点击网址

评论

0赞 Michael Nelles 5/5/2020
这个问题完全归因于安全协议?
0赞 Farid 12/24/2020
@Nelles 但改变协议不是正确的解决方案。服务器可能不支持 SSL 连接
2赞 Chevelle 5/17/2020 #29

我使用带有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>

这真的很愚蠢,因为很明显您的应用程序需要访问互联网。

16赞 Sam Shaba 5/24/2020 #30

添加。。。 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>

这样,明文流量仅在调试环境下才被允许。

16赞 Hemant Ramphul 5/28/2020 #31

简单易懂的解决方案 [Xamarin Form]

对于Android

  1. 转到 ,然后单击 ,Android ProjectProperties

enter image description here

  1. 打开并粘贴此代码:AssemblyInfo.cs

    [assembly: Application(UsesCleartextTraffic =true)]

enter image description here

对于iOS

用:NSAppTransportSecurity

Enter image description here

您必须在文件中将键设置为字典下。NSAllowsArbitraryLoadsYESNSAppTransportSecurityinfo.plist

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSAllowsArbitraryLoads</key>
  <true/>
</dict>

Plist configuration

评论

1赞 Raj K 1/26/2021
这就像一个魅力,精湛@Hemant Ramphul
1赞 Ivan Spajić 8/7/2021
绝对一流!
4赞 Jarda Pavlíček 6/17/2020 #32

将以下内容放入您的: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

2赞 user7641341 2/25/2021 #33

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

评论

0赞 BollMose 12/23/2021
是的,这对我有用,Android 10/11/12。
0赞 Andrew Njuki 9/26/2021 #34

我在简单的 WebView 应用程序中遇到了同样的问题,因为我的 URL http:// 但当您将其转到 https:// 时,错误得到了解决。

评论

0赞 Hein 9/26/2021
这并不能真正回答这个问题。如果您有其他问题,可以通过单击“提问”来提问。要在此问题获得新答案时收到通知,您可以关注此问题。一旦你有足够的声誉,你还可以添加赏金来吸引更多人关注这个问题。- 从评论
1赞 TomEberhard 10/8/2021 #35

我们面临着一个非常相似的问题,弄清楚它很痛苦。对于那些将不得不处理这个问题的不幸灵魂,以下是我们所做的:

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 调用直接通过。

0赞 crg 1/31/2022 #36

检查该域是否是 .network_security_config.xml

就我而言,我使用了一个更改域的命令。--externalionic 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>
10赞 Jacob 10/28/2022 #37

我建议同时添加 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.gradleandroid.app) 并查找 buildTypesreleasedebug(如果不存在,则创建):

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

评论

0赞 Kaymaz 3/24/2023
为什么我首先看到这个答案?没有提到安全问题,什么都没有!我的意思是,不能简单地禁用防止 MITM、窃听等的安全功能。不过,您的答案正确地解决了 OP 问题。