Javafx - 在 webview 组件中打开 login.microsoftonline.com 页面 [英] Javafx - open login.microsoftonline.com page in webview component

查看:14
本文介绍了Javafx - 在 webview 组件中打开 login.microsoftonline.com 页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从 javafx 打开 webview 组件中的 login.microsoftonline.com 页面时遇到问题.我有简单的代码可以毫无问题地打开此页面:

I have problem with opening login.microsoftonline.com page in webview component from javafx. I have simply code that should open this page without any trouble:

   WebView webView = new WebView();
    WebEngine webEngine = webView.getEngine();
    var url = "https://login.microsoftonline.com/";
    webEngine.load(url);

    VBox root = new VBox();
    root.getChildren().add(webView);
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();

    webEngine.getLoadWorker().stateProperty().addListener((obs, oldValue, newValue) -> { 
     System.out.println(webEngine.getLocation());
    });

当我尝试在装有 Windows 操作系统的机器上执行此代码时,我收到空白页:当我在 macbook 上执行相同的代码时,网站正在打开:

When I try to execute this code on machine with windows operating system I receive blank page: When I execute the same code on macbook, site is opening:

我使用的是 java 10,真的不知道出了什么问题.有人有同样的问题吗?知道如何解决这个问题吗?也许还有其他组件而不是 webview 可以用来做我的事情?

I'm using java 10 and really no idea what's wrong. Does anybody have the same issue? Any idea how to solve this problem? maybe there is other component instead of webview that I can use to do my stuff?

推荐答案

遇到了同样的问题,我想我找到了关键点:外部脚本/链接完整性失败.这不是平台浏览器问题,JavaFX (OpenJFK) 依赖于嵌入式 webkit 引擎.

Had the same issue and I think I found the critical point: external script/link integrity fails. This is not a platform browser issue, JavaFX (OpenJFK) relies on an embedded webkit engine.

Windows JDK 8 上的版本 40 和版本 172 之间发生了回归.它在 Oracle JDK 9.0.4 上运行良好它不适用于 Oracle JDK 11

The regression happened between version 40 and version 172 on windows JDK 8. It's working fine with Oracle JDK 9.0.4 It's not working with Oracle JDK 11

更多详情请访问:https://github.com/mguessan/davmail/issues/12

问题类似于:Javafx - 打开webview组件中的login.microsoftonline.com页面

=> 更新答案:已实施以覆盖 Microsoft 表单内容并禁用完整性检查.这不是 webkit 错误的修复,只是一种解决方法

=> Updated answer: implemented to override Microsoft form content and disable integrity check. This is not a fix of the webkit bug, just a workaround

try {
    URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {
        @Override
        public URLStreamHandler createURLStreamHandler(String protocol) {
            if ("https".equals(protocol)) {
                return new sun.net.www.protocol.https.Handler() {
                    @Override
                    protected URLConnection openConnection(URL url, Proxy proxy) throws IOException {
                        System.out.println("openConnection " + url);

                        if (url.toExternalForm().endsWith("/common/handlers/watson")) {
                            System.out.println("Failed: form calls watson");
                        }
                        final HttpsURLConnectionImpl httpsURLConnection = (HttpsURLConnectionImpl) super.openConnection(url, proxy);
                        if ("login.microsoftonline.com".equals(url.getHost())
                                && "/common/oauth2/authorize".equals(url.getPath())) {

                            return new URLConnection(url) {
                                @Override
                                public void connect() throws IOException {
                                    httpsURLConnection.connect();
                                }

                                public InputStream getInputStream() throws IOException {
                                    byte[] content = readFully(httpsURLConnection.getInputStream());
                                    String contentAsString = new String(content, "UTF-8");
                                    System.out.println(contentAsString);
                                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                    baos.write(contentAsString.replaceAll("integrity", "integrity.disabled").getBytes("UTF-8"));
                                    return new ByteArrayInputStream(baos.toByteArray());
                                }

                                public OutputStream getOutputStream() throws IOException {
                                    return httpsURLConnection.getOutputStream();
                                }

                            };

                        } else {
                            return httpsURLConnection;
                        }
                    }

                };
            }
            return null;
        }
    });
} catch (Throwable t) {
    System.out.println("Unable to register custom protocol handler");
}

这篇关于Javafx - 在 webview 组件中打开 login.microsoftonline.com 页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆