IONIC 5.2.2:无法加载资源:net::ERR_CLEARTEXT_NOT_PERMITTED.标准解决方案不起



network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config>
<domain includeSubdomains="true">localhost</domain>
<domain includeSubdomains="true">192.168.1.3</domain>
</domain-config>
</network-security-config>

配置.xml

<platform name="android">
<edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android">
<application android:usesCleartextTraffic="true" />
<application android:networkSecurityConfig="@xml/network_security_config" />
</edit-config>
<resource-file src="resources/android/xml/network_security_config.xml" target="app/src/main/res/xml/network_security_config.xml" />
<resource-file src="google-services.json" target="app/google-services.json" />
<allow-intent href="market:*" />
<icon density="ldpi" src="resourcesandroidicondrawable-ldpi-icon.png" />
<icon density="mdpi" src="resourcesandroidicondrawable-mdpi-icon.png" />
<icon density="hdpi" src="resourcesandroidicondrawable-hdpi-icon.png" />
<icon density="xhdpi" src="resourcesandroidicondrawable-xhdpi-icon.png" />
<icon density="xxhdpi" src="resourcesandroidicondrawable-xxhdpi-icon.png" />
<icon density="xxxhdpi" src="resourcesandroidicondrawable-xxxhdpi-icon.png" />
<splash density="land-ldpi" src="resourcesandroidsplashdrawable-land-ldpi-screen.png" />
<splash density="land-mdpi" src="resourcesandroidsplashdrawable-land-mdpi-screen.png" />
<splash density="land-hdpi" src="resourcesandroidsplashdrawable-land-hdpi-screen.png" />
<splash density="land-xhdpi" src="resourcesandroidsplashdrawable-land-xhdpi-screen.png" />
<splash density="land-xxhdpi" src="resourcesandroidsplashdrawable-land-xxhdpi-screen.png" />
<splash density="land-xxxhdpi" src="resourcesandroidsplashdrawable-land-xxxhdpi-screen.png" />
<splash density="port-ldpi" src="resourcesandroidsplashdrawable-port-ldpi-screen.png" />
<splash density="port-mdpi" src="resourcesandroidsplashdrawable-port-mdpi-screen.png" />
<splash density="port-hdpi" src="resourcesandroidsplashdrawable-port-hdpi-screen.png" />
<splash density="port-xhdpi" src="resourcesandroidsplashdrawable-port-xhdpi-screen.png" />
<splash density="port-xxhdpi" src="resourcesandroidsplashdrawable-port-xxhdpi-screen.png" />
<splash density="port-xxxhdpi" src="resourcesandroidsplashdrawable-port-xxxhdpi-screen.png" />
</platform>

验证码

import { HttpClient, HttpHeaders } from '@angular/common/http';
...
async login(email:string, password:string) {
let loginObject = {email, password};
let httpOptions = {
headers: new HttpHeaders({
'Content-Type':  'application/json'
})
};
let res:any = await this.http.post("http://192.168.1.3:3001/login", loginObject, httpOptions).toPromise()
console.log("res,",res);
if (res.msg == 'auth-success') {
console.log("res token:", res.token);
await this.storage.setItem(TOKEN_KEY, res.token);
this.authenticationState.next(true);
return 'auth-success';
}
else
{
this.authenticationState.next(false);
return 'login-failed';
}
}

错误图像

我在网上尝试了多个"奇怪"的解决方案,但没有一个奏效。我不明白为什么当我启用明文时,仍然不允许我通过我的内部 IP 进行调试。

此外,值得一提的是,我之前的离子项目"随机"中断了无效的.json。我没有正确提交我的更改,最终陷入困境,所以我开始了一个全新的项目,并一直在构建我以前的配置。此外,我已经验证服务器是否在我的路由器上工作。以前,发布的代码解决了我的问题。但现在,它没有。把我的头发!

提前谢谢。

编辑:network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config cleartextTrafficPermitted="true">
<base-config cleartextTrafficPermitted="true">
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">localhost</domain>
<domain includeSubdomains="true">192.168.1.3</domain>
</domain-config>
</network-security-config>

问题仍未解决。

使用:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
</base-config>
</network-security-config>

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">localhost</domain>
<domain includeSubdomains="true">192.168.1.3</domain>
</domain-config>
</network-security-config>

为什么?

cleartextTrafficPermitted="true" 在 Android 9+ 中默认为 false。

<domain>标签特定于特定域(在您的情况下为"localhost"或"192.168.1.3"(

<base-config>标记适用于未在<domain>中指定的任何域

目标为 的所有连接使用的默认配置 域配置未涵盖。

详细说明:

当您请求不使用加密的服务器(即没有https支持(时,会发生明文错误。

从 Android 9(API 级别 28(开始,明文支持已停用 默认情况下。

要解决此错误,您需要告诉 android 启用明文支持。这是在network_security_config.xml文件中完成的。

阅读更多: https://developer.android.com/training/articles/security-config#domain

编辑:

Android 的新安全策略可防止通过非加密连接 (http( 传输文本。如果您在装有 Android 版本 8 或更低版本的设备上测试以前的配置,则不会收到此错误。在 Android 9+(SDK 28(之后,我们需要明确告诉 Android 我们使用的是 http 连接。所以我们在network_security_config.xml文件中指定域。<base-config>标记用于在所有域上允许 clearTextTraffic。如果您确定应用将使用的域,则可以在<domain>标记和 use 标记中指定这些域。

最新更新