JavaFX WebView不工作使用不受信任的SSL证书 [英] JavaFX WebView not working using a untrusted SSL certificate

查看:1189
本文介绍了JavaFX WebView不工作使用不受信任的SSL证书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JavaFX开发一个简单的嵌入式浏览器:

I'm developing a simple embedded browser using JavaFX:

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

当我使用 webEngine 网站,工作正常:

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

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

尽管如此,如果我尝试加载一个不受信任的证书(我自己的SSL证书) webEngine 不工作,我在浏览器中显示白屏。

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.

有任何方法信任我的SSL证书?

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");

注意:此代码片段只是禁用证书验证,NOT TRUSTS IT。

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

这篇关于JavaFX WebView不工作使用不受信任的SSL证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆