Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android中不安全的主机名验证程序_Android_Ssl Certificate_Hostname_Android Security_Trustmanager - Fatal编程技术网

Android中不安全的主机名验证程序

Android中不安全的主机名验证程序,android,ssl-certificate,hostname,android-security,trustmanager,Android,Ssl Certificate,Hostname,Android Security,Trustmanager,3月1日之后,当我将我的应用程序上传到beta group时,我收到了来自Google的一封邮件,其中引用: 此信息适用于使用HostnameVerifier接口的不安全实现的应用程序的开发人员,该接口在使用setDefaultHostnameVerifier API建立到远程主机的HTTPS连接时接受所有主机名 在网上搜索后,我发现了一些链接,但大部分都是建议使用第三方库,但在我的整个应用程序中,我已经完成了简单的HTTPS请求restcalls。我用于rest调用的代码是: TrustMan

3月1日之后,当我将我的应用程序上传到beta group时,我收到了来自Google的一封邮件,其中引用:

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

在网上搜索后,我发现了一些链接,但大部分都是建议使用第三方库,但在我的整个应用程序中,我已经完成了简单的HTTPS请求restcalls。我用于rest调用的代码是:

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);

有没有人知道,为了从谷歌发出的警告中恢复过来,可以编写什么类型的代码。但是我不想使用像Volley这样的第三方库。

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

  • 通过Web浏览器下载URL的SSL证书

  • 在目录->res/xml/network\u security\u config.xml中创建一个network\u security\u config.xml

  • 将以下代码添加到Xml文件:- `

    
    example.com
    `
    
  • 将下载的SSL证书放在目录->res/raw/'my\u ca'中的名称'my\u ca'中

  • 将此添加到您的清单:-


  • 您自己的代码中有一条注释,说明您正在创建一个完全信任的主机名验证程序。这不是一个好主意,不要这样做。我甚至不知道你为什么要搞ssl——你需要做的就是使用https url来请求https站点。没有必要设置验证器。Thanx@Gabe itworks@GabeSechan我正在为https站点使用https url,但它给出了SSL握手异常。如果我使用上面的代码,它会忽略例外情况,但谷歌游戏不会批准它。我该怎么办?您是否在不使用任何SSL代码的情况下纠正了问题@Ritwiks听起来好像你的服务器有问题,可能是坏证书。
    <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>`