Search in sources :

Example 1 with Certificate

use of com.teamdev.jxbrowser.net.tls.Certificate 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/");
}
Also used : ArrayList(java.util.ArrayList) CertificateException(java.security.cert.CertificateException) CertificateEncodingException(java.security.cert.CertificateEncodingException) X509Certificate(java.security.cert.X509Certificate) JFrame(javax.swing.JFrame) ByteArrayInputStream(java.io.ByteArrayInputStream) Engine(com.teamdev.jxbrowser.engine.Engine) ClientCertificate(com.teamdev.jxbrowser.net.tls.ClientCertificate) Browser(com.teamdev.jxbrowser.browser.Browser) X509Certificate(java.security.cert.X509Certificate) Certificate(com.teamdev.jxbrowser.net.tls.Certificate) ClientCertificate(com.teamdev.jxbrowser.net.tls.ClientCertificate)

Aggregations

Browser (com.teamdev.jxbrowser.browser.Browser)1 Engine (com.teamdev.jxbrowser.engine.Engine)1 Certificate (com.teamdev.jxbrowser.net.tls.Certificate)1 ClientCertificate (com.teamdev.jxbrowser.net.tls.ClientCertificate)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 CertificateEncodingException (java.security.cert.CertificateEncodingException)1 CertificateException (java.security.cert.CertificateException)1 X509Certificate (java.security.cert.X509Certificate)1 ArrayList (java.util.ArrayList)1 JFrame (javax.swing.JFrame)1