show一下证书

在 JavaFX 中,如果你想要 WebView 跳过证书验证,你可以使用自定义的 TrustManager 来实现。以下是一个简单的示例代码: ```java import javafx.application.Application; import javafx.concurrent.Worker; import javafx.scene.Scene; import javafx.scene.layout.StackPane; import javafx.scene.web.WebEngine; import javafx.scene.web.WebView; import javafx.stage.Stage; import javax.net.ssl.*; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; public class WebViewIgnoreCertificate extends Application { @Override public void start(Stage primaryStage) { WebView webView = new WebView(); WebEngine webEngine = webView.getEngine(); // 忽略证书验证 SSLContext sslContext; try { sslContext = SSLContext.getInstance("SSL"); sslContext.init(null, new TrustManager[]{new X509TrustManager() { @Override public X509Certificate[] getAcceptedIssuers() { return null; } @Override public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException { } }}, null); } catch (Exception e) { e.printStackTrace(); return; } webEngine.setCreatePopupHandler(param -> { WebView popupWebView = new WebView(); WebEngine popupWebEngine = popupWebView.getEngine(); popupWebEngine.setJavaScriptEnabled(true); popupWebEngine.setSSLContext(sslContext); // 设置 SSL 上下文 return new WebPopupController(popupWebView); }); webEngine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> { if (newValue == Worker.State.SUCCEEDED) { System.out.println("Page Loaded"); } }); webEngine.setJavaScriptEnabled(true); webEngine.setSSLContext(sslContext); // 设置 SSL 上下文 webEngine.load("https://example.com"); // 加载网页的 URL StackPane root = new StackPane(); root.getChildren().add(webView); Scene scene = new Scene(root, 800, 600); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } private static class WebPopupController implements PopupHandler { private final WebView popupWebView; public WebPopupController(WebView popupWebView) { this.popupWebView = popupWebView; } @Override public void handle(PopupFeatures config) { Stage popupStage = new Stage(); StackPane popupRoot = new StackPane(); popupRoot.getChildren().add(popupWebView); Scene popupScene = new Scene(popupRoot, 800, 600); popupStage.setScene(popupScene); popupStage.show(); } } } ``` 在上面的示例代码中,我们通过自定义的 TrustManager 来忽略证书验证。然后,我们将 SSL 上下文设置给 WebEngine,以便让 WebView 使用我们定制的证书验证规则。 请注意,忽略证书验证可能会带来安全风险,请谨慎使用。在生产环境中,建议使用有效的证书进行验证。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值