JavaFX WebView和客户端证书

时间:2016-03-22 15:41:36

标签: ssl javafx webview certificate client

我正在尝试将.net应用程序移植到具有集成Web浏览器的JavaFx,该浏览器打开需要证书的网站。在Windows中,证书是从提供的.pfx文件和密码短语安装的。 当浏览器调用网站时,弹出窗口显示已安装的证书,如果有多个证书,则用户选择正确的证书,并打开网站。 使用以下代码,我可以使用我的证书打开网站连接。

private void Connect() throws NoSuchAlgorithmException, FileNotFoundException, KeyStoreException, IOException, CertificateException, UnrecoverableKeyException, KeyManagementException {
    SSLContext ctx = SSLContext.getInstance("TLS");
    KeyManager[] keyManagers;
    KeyStore keyStore = KeyStore.getInstance("pkcs12");
    FileInputStream keyStoreFile = new FileInputStream(new File("Certificate.pfx"));
    String keyStorePassword = "password";
    keyStore.load(keyStoreFile, keyStorePassword.toCharArray());
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmf.init(keyStore, keyStorePassword.toCharArray());
    keyManagers = kmf.getKeyManagers();
    ctx.init(keyManagers, null, new SecureRandom());

    SSLSocketFactory sslSocketFactory = ctx.getSocketFactory();
    URL url = new URL("https://example.com");
    HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
    urlConnection.setSSLSocketFactory(sslSocketFactory);
    BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null) {
        System.out.println(inputLine);
    }
    in.close();
}

如何在WebView控件中使用它?

由于

1 个答案:

答案 0 :(得分:0)

经过数天的研究后,我解决了这个问题。 在打开网站之前添加这行代码将使其正常工作。

System.setProperty("javax.net.ssl.keyStore", "/path/to/certificate.pfx");
System.setProperty("javax.net.ssl.keyStorePassword","password");

希望这有帮助!