问题描述
我尝试将.net应用程序移植到具有集成Web浏览器的JavaFx,该浏览器打开需要证书的网站。在Windows中,证书是从提供的.pfx文件和密码短语安装的。
当浏览器调用网站时,弹出窗口显示已安装的证书,如果有多个证书,则用户选择正确的证书,并且网站打开。
使用以下代码,我得到网站使用我的证书打开连接。
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();
}
如何在WebView控制中使用此工具?
How can I get this working in WebView control?
感谢
推荐答案
我花了几天研究后解决了这个问题。
在打开网站之前添加这行代码将使它工作。
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");
希望有帮助!
这篇关于JavaFX WebView和客户端证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!