【问题标题】:Insecure HostnameVerifier in AndroidAndroid中不安全的主机名验证器
【发布时间】:2017-03-07 06:10:43
【问题描述】:

在 3 月 1 日之后,当我将我的应用上传到 beta 组时,我收到了来自 Google 引用的特定邮件:

此信息适用于使用不安全实现 HostnameVerifier 接口的应用程序的开发人员,该接口在使用 setDefaultHostnameVerifier API 与远程主机建立 HTTPS 连接时接受所有主机名

在网上搜索后,我发现了一些链接,但大多数情况下他们建议使用第三方库,但在我的整个应用程序中,我为 restcalls 做了简单的 HTTPS 请求。我用于休息调用的代码是:

TrustManager[] trustAllCerts = new TrustManager[] {
    new X509TrustManager() {

        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        public void checkClientTrusted(X509Certificate[] certs, String authType) {}
        public void checkServerTrusted(X509Certificate[] certs, String authType) {}
    }
};

// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

// Create all-trusting host name verifier
HostnameVerifier hostnameVerifier = new HostnameVerifier() {
    @Override
    public boolean verify(String hostname, SSLSession session) {
        HostnameVerifier hv =
        HttpsURLConnection.getDefaultHostnameVerifier();
        return hv.verify("something.com", session);
    }
};

URL url = new URL("Something");
urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setHostnameVerifier(hostnameVerifier);

谁能知道可以编写什么类型的代码才能从 Google 的警告中恢复过来。但是我不想使用像 Volley 这样的第三方库。

【问题讨论】:

  • 您自己的代码有一条注释说您正在创建一个全信任主机名验证程序。这不是一个好主意,不要这样做。我什至不确定你为什么要搞乱 ssl - 请求 https 站点所需要做的就是使用 https url。无需设置验证者。
  • 谢谢@Gabe 它的工作原理
  • @GabeSechan 我正在为 https 站点使用 https url,但它给出了 SSL 握手异常。如果我使用上面的代码,那么它会忽略异常,但 Google Play 不会批准它。我该怎么办?
  • 您是否在不使用任何 SSL 代码的情况下纠正了该问题? @ritwik
  • 听起来你的服务器有问题,可能是证书错误。

标签: android ssl-certificate hostname android-security trustmanager


【解决方案1】:

我认为您正在通过 URL 检索或发布数据到服务器。 如果您想使用相同的方法(信任管理器)回答问题,请点击此链接:- TrustManagerVulnerabilitySolution (要么) 我使用了另一种方法来解决这个问题。如果您想尝试另一种方法,请按照以下步骤操作:-

  1. 通过您的网络浏览器下载您的 URL 的 SSL 证书。

  2. 在目录下创建network_security_config.xml-> res/xml/network_security_config.xml

  3. 将这些代码添加到 Xml 文件中:- `

    <network-security-config>
            <domain-config>
                <domain includeSubdomains="true">example.com</domain>
                <trust-anchors>
                    <certificates src="@raw/my_ca"/>
                </trust-anchors>
            </domain-config>
        </network-security-config>`
    
  4. 将您下载的 SSL 证书放在目录中的名称“my_ca”中-> res/raw/'my_ca'

  5. 将此添加到您的清单中:- &lt;application android:networkSecurityConfig="@xml/network_security_config"&gt;

  6. 就是这样。更多详情请参考:-Networksecurityconfiguration

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-12
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多