<< No spell checker in Visual Web Developer2008 | Home | Are content creators getting rewarded? >>

How to bypass trusted host and certificate check in Java

You probably debug your secure app and have no money for getting certificate yet

I decided to share my experience, since nothing can be easily found on net, especially if you use crappy Google. Just call method below prior of using HTTPUrlConnection to connect to not trusted server when you're getting exceptions like: java.security.cert.CertificateException: Could not find trusted certificate

  public static void installAllTrustManager() {
  TrustManager[] trustAllCerts = new TrustManager[]{
            new X509TrustManager() {
             public java.security.cert.X509Certificate[] getAcceptedIssuers() {
             return null;
            }
            public void checkClientTrusted(
             java.security.cert.X509Certificate[] certs, String authType) {
            }
            public void checkServerTrusted(
             java.security.cert.X509Certificate[] certs, String authType) {
            }
            }
            };
  
  // Install the all-trusting trust manager
  try {
   sc = SSLContext.getInstance("SSL");
   sc.init(null, trustAllCerts, new java.security.SecureRandom());
   HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
   HttpsURLConnection.setDefaultHostnameVerifier(
       new HostnameVerifier() {
         public boolean verify(String urlHostname, javax.net.ssl.SSLSession _session) {
                                         return true;
         }
       }
    );
  } catch (Exception e) {
   e.printStackTrace();
  }
 }



Re: How to bypass trusted host and certificate check in Java

Dmitriy, I was wondering if you could help me. I have some video teaching files from a college class a took a few semesters ago. The problem is that when I try to view them there is some kind of java security problem that won't allow me to view the files anymore. I was wondering if you could help me get around this. Thanks, Tony

Re: How to bypass trusted host and certificate check in Java

e-mail sent

Add a comment Send a TrackBack