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


In JxBrowser 6.3 the CertificateVerifier API has been introduced. Using this API you can get information about each SSL certificate used for displaying HTTPS web pages and decide whether it should be accepted or rejected. By default, Chromium engine decides whether certificate should be accepted/rejected. You can register your own CertificateVerifier implementation to modify default behavior. For example:(在JxBrowser 6.3中,引入了CertificateVerifier API。使用此API,您可以获得有关用于显示HTTPS网页的每个SSL证书的信息,并决定应接受还是拒绝该证书。默认情况下,Chromium引擎决定是否应接受/拒绝证书。您可以注册自己的CertificateVerifier实现来修改默认行为。例如:)

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

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

/**
 * The sample demonstrates how to accept/reject SSL certificates using
 * custom SSL certificate verifier.
 */
public class CertificateVerifierSample {
    public static void main(String[] args) {
        Browser browser = new Browser();
        BrowserView view = new BrowserView(browser);

        NetworkService networkService = browser.getContext().getNetworkService();
        networkService.setCertificateVerifier(new CertificateVerifier() {
            @Override
            public CertificateVerifyResult verify(CertificateVerifyParams params) {
                // Reject SSL certificate for all "google.com" hosts.
                if (params.getHostName().contains("google.com")) {
                    return CertificateVerifyResult.INVALID;
                }
                return CertificateVerifyResult.OK;
            }
        });

        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.loadURL("http://google.com");
    }
}