JavaFX WebView not working using a untrusted SSL certificate

后端 未结 1 1260
别跟我提以往
别跟我提以往 2020-12-29 17:45

I\'m developing a simple embedded browser using JavaFX:

final WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
相关标签:
1条回答
  • 2020-12-29 18:43

    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.

    0 讨论(0)
提交回复
热议问题