简体   繁体   中英

JavaFX WebView not working using a untrusted SSL certificate

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM