use of com.teamdev.jxbrowser.net.tls.ClientCertificate in project JxBrowser-Examples by TeamDev-IP.
the class SelectClientCertificate method main.
public static void main(String[] args) {
Engine engine = Engine.newInstance(HARDWARE_ACCELERATED);
Browser browser = engine.newBrowser();
SwingUtilities.invokeLater(() -> {
view = BrowserView.newInstance(browser);
JFrame frame = new JFrame("Select Client SSL Certificate");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(view, BorderLayout.CENTER);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
browser.set(SelectClientCertificateCallback.class, (params, tell) -> {
if (params.certificates().size() != 0) {
List<Certificate> certificates = params.certificates();
List<X509Certificate> x509Certificates = new ArrayList<>();
for (Certificate certificate : certificates) {
try {
x509Certificates.add(X509Certificates.of(new ByteArrayInputStream(certificate.derEncodedValue())));
} catch (CertificateException e) {
e.printStackTrace();
}
}
Object[] selectionValues = x509Certificates.toArray();
Object selectedValue = JOptionPane.showInputDialog(view, String.format(DIALOG_MESSAGE, params.hostPort().host() + ":" + params.hostPort().port()), DIALOG_TITLE, PLAIN_MESSAGE, null, selectionValues, selectionValues[0]);
if (selectedValue != null) {
// Tell the engine which SSL certificate has been selected.
try {
X509Certificate certificate = (X509Certificate) selectedValue;
ClientCertificate clientCertificate = ClientCertificate.of(certificate, SslPrivateKey.of(certificate.getEncoded()));
tell.select(clientCertificate);
return;
} catch (CertificateEncodingException e) {
e.printStackTrace();
}
}
}
tell.cancel();
});
browser.navigation().loadUrl("https://client.badssl.com/");
}
Aggregations