Request with automatic or user selection of appropriate client certificate

后端 未结 2 938
一生所求
一生所求 2021-01-02 14:49

I\'m developing an hybrid cordova app which might connect to different servers. Some of them do require a client certificate.

On an Android mobile the corresponding

2条回答
  •  猫巷女王i
    2021-01-02 15:37

    If your URLs are still in development stage (not production version), you can skip those SSL/NON-SSL certificates installing to access the URLs.

    Here is how to skip SSL validation : Call when activity onCreate() or where your need before accessing URL.

    public static void skipSSLValidation() {
            try {
                TrustManager[] trustAllCerts = new TrustManager[]{
                        new X509TrustManager() {
                            public X509Certificate[] getAcceptedIssuers() {
                        /* Create a new array with room for an additional trusted certificate. */
                                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) {
                        return true;
                    }
                });
            } catch (Exception e) {
                // pass
            }
        }
    

    Note : If your HTTPS URLs are valid, you will no require to use server-generated certificates. You should using this method for testing/development only. For release/production you don't have to use this method.

提交回复
热议问题