JavaFX WebView and Client Certificate

2019-04-02 10:42发布

I am trying to port a .net application to JavaFx that has an integrated web browser that opens a website that requires a certificate. In windows the certificate was installed from a provided .pfx file and a pass phrase. When the browser calls the website, a pop up shows the installed certificates, user chose the right one if there is more than one and website opens. With the following code, I get the website to open connect using my certificate.

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();
}

How can I get this working in WebView control?

Thanks

1条回答
男人必须洒脱
2楼-- · 2019-04-02 10:48

I solved the issue after spending days researching. Adding this lines of code before opening the website will make it work.

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

Hope this help!

查看更多
登录 后发表回答