【问题标题】:JavaFX WebView and Client CertificateJavaFX WebView 和客户端证书
【发布时间】:2016-03-22 15:41:35
【问题描述】:

我正在尝试将 .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 控件中工作?

谢谢

【问题讨论】:

    标签: ssl javafx webview certificate client


    【解决方案1】:

    经过几天的研究,我解决了这个问题。 在打开网站之前添加这行代码就可以了。

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

    希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-13
      • 2014-01-11
      • 2010-10-16
      • 2010-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多