Java: Overriding function to disable SSL certificate check

后端 未结 3 738
刺人心
刺人心 2020-11-29 01:02

The web service is rest over SSL and it has self signed certificate, hosted in remote system.I have already created a client accessing that web service. This is done by addi

3条回答
  •  南笙
    南笙 (楼主)
    2020-11-29 01:42

    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) {
                        }
                    }
                };
                SSLContext sc = SSLContext.getInstance("SSL");
                sc.init(null, trustAllCerts, new java.security.SecureRandom());
                HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    
                HostnameVerifier allHostsValid = new HostnameVerifier() {
                    public boolean verify(String hostname, SSLSession session) {
                        return true;
                    }
                };
                HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
                GetCustomerPhone http = new GetCustomerPhone(); 
    
        System.out.println("Processing..");     
         try{
                http.sendPost();    
            }
        catch(Exception e){
                e.printStackTrace();
            }               
    }
    

    I think it will working fine.because it fine of me...

提交回复
热议问题