New version is available You are viewing the documentation for JxBrowser 6 which is not supported since December 2019. Go to the current documentation.
List icon 目录

This guide provides the list of supported authentication types and describes how to handle various authentication requests.

Proxy, Basic, Digest, NTLM

To handle basic, digest or NTLM authentication you can use the NetworkDelegate.onAuthRequired(AuthRequiredParams params) handler.

To display authentication dialog where user can enter valid user name and password you must register default Swing/JavaFX implementation of the NetworkDelegate or your own implementation of the NetworkDelegate interface.

The following example demonstrates how to register and override default Swing implementation of the NetworkDelegate interface in order to provide user name and password without displaying authorization dialog:

browser.getContext().getNetworkService().setNetworkDelegate(new DefaultNetworkDelegate() {
    @Override
    public boolean onAuthRequired(AuthRequiredParams params) {
        if (!params.isProxy()) {
            params.setUsername("proxy-username");
            params.setPassword("proxy-password");
            // Don't cancel authentication
            return false;
        }
        // Cancel authentication
        return true;
    }
});

HTTPS Client Certificate

At the start of a SSL or TLS session, the web server may require the client application to submit a client certificate for authentication. Upon receiving the certificate, the server would then use it to identify the certificate’s source and determine whether the client should be given an access.

Using JxBrowser DialogHandler API you can handle situation when the Select SSL Certificate dialog should be displayed. By default JxBrowser displays its own implementation of the dialog where user can select required certificate in the list of available certificates. In order to override default behavior you must register your own implementation of the DialogHandler interface with overridden DialogHandler.onSelectCertificate(CertificatesDialogParams params) method. In your implementation you can display your own dialog or suppress the dialog and select required certificate programmatically.

The following sample demonstrates how to override default implementation with custom Select SSL Certificate dialog:

import com.teamdev.jxbrowser.chromium.*;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;
import com.teamdev.jxbrowser.chromium.swing.DefaultDialogHandler;

import javax.swing.*;
import java.awt.*;
import java.util.List;

/**
 * The sample demonstrates how to display Select SSL Certificate dialog where
 * user must select required SSL certificate to continue loading web page.
 */
public class SelectSSLCertificateSample {
    public static void main(String[] args) {
        Browser browser = new Browser();
        final BrowserView view = new BrowserView(browser);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.setDialogHandler(new DefaultDialogHandler(view) {
            @Override
            public CloseStatus onSelectCertificate(CertificatesDialogParams params) {
                String message = "Select a certificate to authenticate yourself to " + params.getHostPortPair().getHostPort();
                List<Certificate> certificates = params.getCertificates();
                if (!certificates.isEmpty()) {
                    Object[] selectionValues = certificates.toArray();
                    Object selectedValue = JOptionPane.showInputDialog(view, message, "Select a certificate",
                            JOptionPane.PLAIN_MESSAGE, null, selectionValues, selectionValues[0]);
                    if (selectedValue != null) {
                        params.setSelectedCertificate((Certificate) selectedValue);
                        return CloseStatus.OK;
                    }
                }
                return CloseStatus.CANCEL;
            }
        });
        browser.loadURL("<URL that causes Select SSL Certificate dialog>");
    }
}

Custom Client Certificate

In the DialogHandler.onSelectCertificate(CertificatesDialogParams params) method you can select not only SSL certificate from the given list of certificates, but also select SSL certificate instantiated from standard Java X509Certificate class. For example:

browser.setDialogHandler(new DefaultDialogHandler(view) {
    @Override
    public CloseStatus onSelectCertificate(CertificatesDialogParams params) {
        X509Certificate x509Certificate = loadCertificate();
        params.setSelectedCertificate(new Certificate(x509Certificate));
        return CloseStatus.OK;
    }
});
Go Top