Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Massimo Zappino
Added: May 16, 2020 5:58 PM
Views: 8
Tags: no tags
  1. package com.tagmycode.sdk;
  2.  
  3. import javax.net.ssl.*;
  4. import java.security.KeyManagementException;
  5. import java.security.NoSuchAlgorithmException;
  6. import java.security.cert.X509Certificate;
  7.  
  8. public class Ssl {
  9.     public void disableSslVerification() {
  10.         try {
  11.             // Create a trust manager that does not validate certificate chains
  12.             TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
  13.                 public java.security.cert.X509Certificate[] getAcceptedIssuers() {
  14.                     return null;
  15.                 }
  16.  
  17.                 public void checkClientTrusted(X509Certificate[] certs, String authType) {
  18.                 }
  19.  
  20.                 public void checkServerTrusted(X509Certificate[] certs, String authType) {
  21.                 }
  22.             }
  23.             };
  24.  
  25.             // Install the all-trusting trust manager
  26.             SSLContext sc = SSLContext.getInstance("SSL");
  27.             sc.init(null, trustAllCerts, new java.security.SecureRandom());
  28.             HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
  29.  
  30.             // Create all-trusting host name verifier
  31.             HostnameVerifier allHostsValid = new HostnameVerifier() {
  32.                 public boolean verify(String hostname, SSLSession session) {
  33.                     return true;
  34.                 }
  35.             };
  36.  
  37.             // Install the all-trusting host verifier
  38.             HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
  39.         } catch (NoSuchAlgorithmException e) {
  40.             e.printStackTrace();
  41.         } catch (KeyManagementException e) {
  42.             e.printStackTrace();
  43.         }
  44.     }
  45. }
  46.