4

I'm developing a simple embedded browser using JavaFX:

final WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();

When I use webEngine to load any http website, it works fine:

webEngine.load("http://google.es");

Despite this, if I try to load a website with an untrusted certificate (my own ssl certificate), webEngine does not work and I get a white screen in the browser.

Is there any way to (automatically) trust in my ssl certificate?

Universal Electricity
  • 769
  • 1
  • 12
  • 26
nach0
  • 395
  • 1
  • 3
  • 11

1 Answers1

4

Finally, I solved my question. You should add this code before loading the website:

// Create a trust manager that does not validate certificate chains
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 {
    SSLContext sc = SSLContext.getInstance("SSL"); 
    sc.init(null, trustAllCerts, new java.security.SecureRandom()); 
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (GeneralSecurityException e) {
} 
// Now you can access an https URL without having the certificate in the truststore
try { 
    URL url = new URL("https://hostname/index.html"); 
} catch (MalformedURLException e) {
} 
//now you can load the content:

webEngine.load("https://example.com");

NOTE: This code fragment just disable certificates validation, NOT TRUSTS IT.

Universal Electricity
  • 769
  • 1
  • 12
  • 26
nach0
  • 395
  • 1
  • 3
  • 11
  • 1
    There are much better (and secure) ways to do this, see [this answer](http://stackoverflow.com/a/859271/372643): create a new keystore (possibly based on a copy of the default `cacerts` file), import your self-signed certificate and then use it to initialise your trust manager. – Bruno May 21 '14 at 12:33