Note: Advice in this article will only work for JxBrowser 6. See the corresponding article for JxBrowser 7 here.(注意:本文中的建议仅适用于JxBrowser6,JxBrowser7相应文章请点击这里。)


Using JxBrowser DialogHandler API you can handle situation when 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.(使用JxBrowser DialogHandler API,可以处理显示“选择SSL证书”对话框的情况。默认情况下,JxBrowser显示其自己的对话框实现,用户可以在其中选择可用证书列表中的所需证书。为了重写默认行为,您必须使用重写的DialogHandler.onSelectCertificate(CertificatesDialogParams params)方法注册自己的DialogHandler接口实现。在您的实现中,您可以显示自己的对话框或隐藏该对话框,并以编程方式选择所需的证书。)


The following sample demonstrates how to override default implementation with custom Select SSL Certificate dialog: (以下示例演示如何使用自定义“选择SSL证书”对话框覆盖默认实现:)

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>");
    }
}