How to solve “Google Play will block publishing of any new apps or updates that use an unsafe implementation of HostnameVerifier”?

半城伤御伤魂 提交于 2020-06-11 10:30:07

问题


Beginning March 1, 2017, Google Play will block publishing of any new apps or updates that use an unsafe implementation of HostnameVerifier. Your published APK version will remain unaffected, however any updates to the app will be blocked unless you address this vulnerability.

Action required

To properly handle hostname verification, change the verify method in your custom HostnameVerifier interface to return false whenever the hostname of the server does not meet your expectations.

This is the message I got from the Google Play Store for one of my apps. We are doing some web service calls by using the Apache library.

How to rectify this issue?


回答1:


Branch and switch to OkHttp. You got a long night a head of you.




回答2:


As mentioned in the message, on verify() method inside HostnameVerifier class, only returns true for hostnames that your app trust.

Try this. Call this method before the connection established.

@SuppressLint("TrulyRandom")
public static void handleSSLHandshake() {
    try {
        TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }

            @Override
            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }

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

        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String arg0, SSLSession arg1) {
                if(arg0.equalsIgnoreCase("google.com") 
                    || arg0.equalsIgnoreCase("firebasedynamiclinks.googleapis.com") 
                    || arg0.equalsIgnoreCase("youtube.com")) {
                    return true;
                } else {
                    return false;
                }
            }
        });
    } catch (Exception ignored) {
    }
}


来源:https://stackoverflow.com/questions/42087342/how-to-solve-google-play-will-block-publishing-of-any-new-apps-or-updates-that

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!