java - 如何解决aosp中不允许的明文

标签 java android https android-source cleartext

我知道 android 默认禁用明文。我可以知道我可以在 aosp 中启用的确切位置,而不是添加所有带有网络配置文件的包吗?

我可以通过添加以下行来允许哪里?

cleartextTrafficPermitted="true

外部/okhttp/android/main/java/com/squareup/okttp/Handler

 public static OkUrlFactory createHttpOkUrlFactory(Proxy proxy) {
        OkHttpClient client = new OkHttpClient();

        // Explicitly set the timeouts to infinity.
        client.setConnectTimeout(0, TimeUnit.MILLISECONDS);
        client.setReadTimeout(0, TimeUnit.MILLISECONDS);
        client.setWriteTimeout(0, TimeUnit.MILLISECONDS);

        // Set the default (same protocol) redirect behavior. The default can be overridden for
        // each instance using HttpURLConnection.setInstanceFollowRedirects().
        client.setFollowRedirects(HttpURLConnection.getFollowRedirects());

        // Do not permit http -> https and https -> http redirects.
        client.setFollowSslRedirects(false);

        // Permit cleartext traffic only (this is a handler for HTTP, not for HTTPS).
        client.setConnectionSpecs(CLEARTEXT_ONLY);

        // When we do not set the Proxy explicitly OkHttp picks up a ProxySelector using
        // ProxySelector.getDefault().
        if (proxy != null) {
            client.setProxy(proxy);
        }

        // OkHttp requires that we explicitly set the response cache.
        OkUrlFactory okUrlFactory = new OkUrlFactory(client);

        // Use the installed NetworkSecurityPolicy to determine which requests are permitted over
        // http.
        OkUrlFactories.setUrlFilter(okUrlFactory, CLEARTEXT_FILTER);

        ResponseCache responseCache = ResponseCache.getDefault();
        if (responseCache != null) {
            AndroidInternal.setResponseCache(okUrlFactory, responseCache);
        }
        return okUrlFactory;
    }

    private static final class CleartextURLFilter implements URLFilter {
        @Override
        public void checkURLPermitted(URL url) throws IOException {
            String host = url.getHost();
            if (!NetworkSecurityPolicy.getInstance().isCleartextTrafficPermitted(host)) {
                throw new IOException("Cleartext HTTP traffic to " + host + " not permitted");
            }
        }
    }

在任何应用程序中,如果我使用 http,我都会收到错误消息,因为不允许到 124.60.5.6 的明文 HTTP 流量";

所以不是在应用程序中改变,而是可以在 aosp 中改变吗?

最佳答案

如果你这样做就足够了

builder.setCleartextTrafficPermitted(true);

第 189 行似乎就足够了,因为您使用的是较旧的应用程序,这些应用程序可能没有任何网络配置,只使用默认配置。

来源:https://android.googlesource.com/platform/frameworks/base.git/+/refs/heads/master/core/java/android/security/net/config/NetworkSecurityConfig.java#189


旧答案

我希望您已经完成了关于绕过安全功能的影响的功课。话虽这么说,负责异常的类在包 android.security.net.config 的框架中,负责的类是 NetworkSecurityConfig

在撰写此答案时,静态构建器类有一个属性 boolean mCleartextTrafficPermittedSet,默认设置为 false。您可能必须将其默认为 true,这使得 NetworkSecurityConfig 类中的方法 getEffectiveCleartextTrafficPermitted() 返回 mCleartextTrafficPermitted作为返回返回 DEFAULT_CLEARTEXT_TRAFFIC_PERMITTED 默认设置为 true

流程是

getEffectiveCleartextTrafficPermitted() 返回 mCleartextTrafficPermitted 返回 DEFAULT_CLEARTEXT_TRAFFIC_PERMITTED 默认返回 true

如果这一切都令人困惑,请在创建构建器时调用构建器上的 setCleartextTrafficPermitted(true)

该类的源代码可在此处获得:https://android.googlesource.com/platform/frameworks/base.git/+/refs/heads/master/core/java/android/security/net/config/NetworkSecurityConfig.java

注意:我没有尝试过这个,只是通过源代码并推断出以上内容。如果有什么不对,欢迎您尝试纠正我。

通过@Shadow 更新进行编辑:

在 NetworkSecurityConfig 中,将 boolean 变量从 true 更改为 false。

   //public static final boolean DEFAULT_CLEARTEXT_TRAFFIC_PERMITTED = true;
    public static final boolean DEFAULT_CLEARTEXT_TRAFFIC_PERMITTED = false;

同样在 ManifestConfigSource 中,注释下面的行,

  /*boolean usesCleartextTraffic =
                        (mApplicationInfo.flags & ApplicationInfo.FLAG_USES_CLEARTEXT_TRAFFIC) != 0
                        && mApplicationInfo.targetSandboxVersion < 2;*/

并直接申请 usesCleartextTraffic 为 true。

 boolean usesCleartextTraffic =true;

关于java - 如何解决aosp中不允许的明文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60738197/

相关文章:

java - 如何从 ControllerAdvice 类中的 ControllerAdvice 选择器检索属性

java - 将 InsnList 拆分为基本 block

java - OSGi:Java 8 上缺少包 javax.security.auth

java - 在android中通过soap ws在mysql数据库中插入微调器值

azure - 如何在 Azure 网站中禁用 HTTPS 重定向

java - Servlet 与另一个服务器通信

android - 使用 SQL 数据库自动完成 TextView

android - 设置颜色选项卡主机指示器

wordpress - 我的 wordpress 网站在 http 模式下显示幻灯片,但在 https 模式下不显示

Android:在向量中访问 http 服务器而不是 https(Android Matrix 客户端)