首页 > 解决方案 > 为什么我的应用无法从 Android 应用中链接到我的网站

问题描述

我有一个 Android 应用程序,我必须为我的隐私政策提供一个网站链接。我最近创建了自己的网站,其中包含我的应用程序的隐私政策页面。我希望能够从我的应用程序中链接到该页面。我使用指向政府网站的链接可以正常工作,但是当我使用指向我自己网站的链接时它不起作用。我在我的应用程序屏幕中收到以下错误。

Webpage not available
The webpage at http://retirementcalculatoraustralia.com/privacy-policy.php could not be loaded because:
net::ERR_CLEARTEXT_NOT_PERMITTED

这是我的片段中的代码。


public class PrivacyPolicyFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View root = inflater.inflate(R.layout.fragment_privacy_policy, container, false);
        WebView webview = root.findViewById(R.id.privacy_policy_text);
        String url = "http://retirementcalculatoraustralia.com/privacy-policy.php";

        webview.getSettings().setJavaScriptEnabled(true);
        webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
        webview.getSettings().setAppCacheEnabled(true);
        webview.getSettings().setDatabaseEnabled(true);
        webview.getSettings().setDomStorageEnabled(true);
        webview.getSettings().setSupportZoom(true);
        webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webview.getSettings().setBuiltInZoomControls(true);

        webview.setWebViewClient(new WebViewClient() {

            public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
                String e = error.toString();
                System.out.println("ERROR " + e);
               // handler.proceed() ;
                handler.cancel();
            }
        });
        webview.loadUrl(url);

        return root;
    }
}

我已经在浏览器中输入了链接,以确保它确实存在并且可以工作。如果我使用以下政府网站链接,那么它可以正常工作。

String url = "https://www.servicesaustralia.gov.au/individuals/topics/deeming/29656";

如果我从我的网站链接中删除前缀 http://,那么我不会收到错误消息,只会出现空白屏幕。如果我添加前缀 http://www ,那么我会收到错误消息。我在我的 AndroidManifest.xml 中进行了如下更改:

  <application
        android:usesCleartextTraffic="true"
        android:networkSecurityConfig="@xml/network_security_config"
        ......

并创建了以下名为 network_security_config.xml 的文件

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config>
        <domain includeSubdomains="true">retirementcalculatoraustralia.com</domain>
        <trust-anchors>
            <certificates src="system"/>
        </trust-anchors>
    </domain-config>
</network-security-config>

这些都没有任何区别。我的网站是在 1 到 2 天前发布的,如果我在浏览器中输入retirecalculatoraustralia.com 但无法从我的应用程序中运行,则可以正常运行。我的片段 onReceivedSslError() 中的打印语句永远不会到达。任何想法表示赞赏。

标签: androidurl

解决方案


阅读以下链接中的信息后,我找到了问题的解决方案: https ://www.nowsecure.com/blog/2018/08/15/a-security-analysts-guide-to-network-security-configuration-在-android-p/

我需要完全理解 HTTP 和 HTTPS 之间的区别以及为什么会发生这个错误。我给大家总结一下:

明文是未加密或打算加密的任何传输或存储的信息。当应用程序使用明文网络流量(例如 HTTP)与服务器通信时,可能会增加窃听和篡改内容的风险。HTTPS 使用 TLS(SSL) 来加密正常的 HTTP 请求和响应。使用 HTTP,任何登录信息或信用卡号等个人信息都可能被黑客窃取、读取或修改。

Android 7.0 Nougat 引入了 Android 网络安全配置功能,允许开发人员对安全通信更加规范。它是一个 XML 文件,开发人员可以在其中自定义 Android 应用程序的网络安全设置。它提供了一个层来保护应用程序不会无意中以未加密的明文传输敏感数据。在 Android 9 中,cleartextTrafficPermitted 默认设置为 false。这意味着如果此标志未明确设置为 false 并且应用程序的目标 API 级别低于 28,则该标志将被视为 true。您可以在 network_security_config.xml 中的特定域上将 cleartextTrafficPermitted 设置为 true。

当我尝试使用 http:// 链接到我的网站时收到错误 net::ERR_CLEARTEXT_NOT_PERMITTED 的原因是因为 cleartextTrafficPermitted 默认设置为 false。当我使用 https::// 在政府网站上对其进行测试时,它运行良好,因为这是一个使用加密的安全网站。Android 附带一组系统信任的预加载 CA(Certification Authority)根证书。

为了解决我的问题,我在 AndroidManifest 文件中添加了以下内容,删除了 android:usesCleartextTraffic="true" 行

android:networkSecurityConfig="@xml/network_security_config"

在 network_security_config.xml

<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">insecure.example.com</domain>
       <trust-anchors>
         <certificates src="system"/>
       </trust-anchors>
    </domain-config>
    <base-config cleartextTrafficPermitted="false" />
</network-security-config>

这告诉 Android 仅允许来自我自己的域和受信任的系统证书的明文,而其他所有内容的基本配置都设置为 false。


推荐阅读