Javafx - open login.microsoftonline.com page in webview component

前端 未结 3 2285
执念已碎
执念已碎 2020-12-19 01:51

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:



        
相关标签:
3条回答
  • 2020-12-19 02:38

    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.

    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

    More details at: https://github.com/mguessan/davmail/issues/12

    Issue similar to: Javafx - open login.microsoftonline.com page in webview component

    => 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");
    }
    
    0 讨论(0)
  • 2020-12-19 02:43

    This is not the solution to your problem but may lead you in the right direction. It seems that the site body is loaded using script. That script depends on other scripts I am guessing. It appears none of the other scripts are loading.

    import com.sun.javafx.webkit.WebConsoleListener;
    import java.security.KeyManagementException;
    import java.security.NoSuchAlgorithmException;
    import java.security.cert.X509Certificate;
    import javafx.application.Application;
    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.HttpsURLConnection;
    import javax.net.ssl.SSLContext;
    import javax.net.ssl.TrustManager;
    import javax.net.ssl.X509TrustManager;
    
    /**
     *
     * @author blj0011
     */
    public class JavaFXApplication281 extends Application
    {
    
        @Override
        public void start(Stage primaryStage)
        {
            try {
                TrustManager trm = new X509TrustManager()
                {
                    @Override
                    public X509Certificate[] getAcceptedIssuers()
                    {
                        return null;
                    }
    
                    @Override
                    public void checkClientTrusted(X509Certificate[] certs, String authType)
                    {
                    }
    
                    @Override
                    public void checkServerTrusted(X509Certificate[] certs, String authType)
                    {
                    }
                };
    
                SSLContext sc = SSLContext.getInstance("SSL");
                sc.init(null, new TrustManager[]{trm}, null);
                HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    
                WebView webView = new WebView();
                WebEngine webEngine = webView.getEngine();
                WebConsoleListener.setDefaultListener(new WebConsoleListener()
                {
                    @Override
                    public void messageAdded(WebView webView, String message, int lineNumber, String sourceId)
                    {
                        System.out.println("Console: [" + sourceId + ":" + lineNumber + "] " + message);
                    }
                });
                webEngine.setJavaScriptEnabled(true);
                String url = "https://login.microsoftonline.com/";//"https://login.microsoftonline.com/jsdisabled";//
                webEngine.load(url);
    
                webEngine.getLoadWorker().stateProperty().addListener((obs, oldValue, newValue) -> {
                    System.out.println(newValue);
                    String html = (String) webEngine.executeScript("document.documentElement.outerHTML");
                    System.out.println(html);
                });
    
                webEngine.setOnError((event) -> {
                    System.out.println(event.getMessage());
                });
    //        webEngine.getLoadWorker().exceptionProperty().addListener((obs, oldExc, newExc) -> {
    //            if (newExc != null) {
    //                newExc.printStackTrace();
    //            }
    //        });
                StackPane root = new StackPane();
    
                root.getChildren().add(webView);
    
                Scene scene = new Scene(root, 300, 250);
    
                primaryStage.setTitle("Hello World!");
                primaryStage.setScene(scene);
                primaryStage.show();
            }
            catch (KeyManagementException ex) {
                ex.printStackTrace();
            }
            catch (NoSuchAlgorithmException ex) {
                ex.printStackTrace();
            }
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args)
        {
            launch(args);
        }
    
    }
    

    Output:

    Console: [null:0] Cannot load stylesheet https://secure.aadcdn.microsoftonline-p.com/ests/2.1.8233.17/content/cdnbundles/converged.v2.login.min_jumzhgrnvlj7lwxqltrteq2.css. Failed integrity metadata check.
    Console: [https://login.microsoftonline.com/common/oauth2/authorize?client_id=4345a7b9-9a63-4910-a426-35363201d503&response_mode=form_post&response_type=code+id_token&scope=openid+profile&state=OpenIdConnect.AuthenticationProperties%3d5xhL9s5iN_65agH7ctGnRfQlJHUHgSrEvD4vkaO323RyB1klBHD6Qh5qidm6GuaIHM8_GaSANKH6y1ohWHalX4QU_YyqGJqXV8wphi2TVMAAY3yyXQk3GB-yqWm0j3oh&nonce=636748812038183968.MmMxNjY2YjEtNDIwZS00ZDhhLWI3YmItMWRhMWM5ZmRmMzk4MjJkMGExMDItZDAxZi00MTZmLWIxYjctOTNmZWU2YjgzZDRi&redirect_uri=https%3a%2f%2fwww.office.com%2f&ui_locales=en-US&mkt=en-US:30] Cannot load stylesheet https://secure.aadcdn.microsoftonline-p.com/ests/2.1.8233.17/content/cdnbundles/converged.v2.login.min_jumzhgrnvlj7lwxqltrteq2.css. Failed integrity metadata check.
    Console: [https://login.microsoftonline.com/common/oauth2/authorize?client_id=4345a7b9-9a63-4910-a426-35363201d503&response_mode=form_post&response_type=code+id_token&scope=openid+profile&state=OpenIdConnect.AuthenticationProperties%3d5xhL9s5iN_65agH7ctGnRfQlJHUHgSrEvD4vkaO323RyB1klBHD6Qh5qidm6GuaIHM8_GaSANKH6y1ohWHalX4QU_YyqGJqXV8wphi2TVMAAY3yyXQk3GB-yqWm0j3oh&nonce=636748812038183968.MmMxNjY2YjEtNDIwZS00ZDhhLWI3YmItMWRhMWM5ZmRmMzk4MjJkMGExMDItZDAxZi00MTZmLWIxYjctOTNmZWU2YjgzZDRi&redirect_uri=https%3a%2f%2fwww.office.com%2f&ui_locales=en-US&mkt=en-US:30] Cannot load stylesheet https://secure.aadcdn.microsoftonline-p.com/ests/2.1.8233.17/content/cdnbundles/converged.v2.login.min_jumzhgrnvlj7lwxqltrteq2.css. Failed integrity metadata check.
    Console: [https://login.microsoftonline.com/common/oauth2/authorize?client_id=4345a7b9-9a63-4910-a426-35363201d503&response_mode=form_post&response_type=code+id_token&scope=openid+profile&state=OpenIdConnect.AuthenticationProperties%3d5xhL9s5iN_65agH7ctGnRfQlJHUHgSrEvD4vkaO323RyB1klBHD6Qh5qidm6GuaIHM8_GaSANKH6y1ohWHalX4QU_YyqGJqXV8wphi2TVMAAY3yyXQk3GB-yqWm0j3oh&nonce=636748812038183968.MmMxNjY2YjEtNDIwZS00ZDhhLWI3YmItMWRhMWM5ZmRmMzk4MjJkMGExMDItZDAxZi00MTZmLWIxYjctOTNmZWU2YjgzZDRi&redirect_uri=https%3a%2f%2fwww.office.com%2f&ui_locales=en-US&mkt=en-US:30] Failed to load external resource ['https://secure.aadcdn.microsoftonline-p.com/ests/2.1.8233.17/content/cdnbundles/converged.v2.login.min_jumzhgrnvlj7lwxqltrteq2.css']
    Console: [null:0] Cannot load script https://secure.aadcdn.microsoftonline-p.com/ests/2.1.8233.17/content/cdnbundles/oldconvergedlogin_pcore.min_lwozjqawrstmtzsn2yunha2.js. Failed integrity metadata check.
    Console: [null:0] Cannot load script https://secure.aadcdn.microsoftonline-p.com/ests/2.1.8233.17/content/cdnbundles/convergedloginpaginatedstrings-en.min_uzcugprrg6vz0z16am4meq2.js. Failed integrity metadata check.
    Console: [null:0] Cannot load script https://secure.aadcdn.microsoftonline-p.com/ests/2.1.8233.17/content/cdnbundles/oldconvergedlogin_pcore.min_lwozjqawrstmtzsn2yunha2.js. Failed integrity metadata check.
    Console: [null:0] Cannot load script https://secure.aadcdn.microsoftonline-p.com/ests/2.1.8233.17/content/cdnbundles/convergedloginpaginatedstrings-en.min_uzcugprrg6vz0z16am4meq2.js. Failed integrity metadata check.
    Console: [null:0] Cannot load script https://secure.aadcdn.microsoftonline-p.com/ests/2.1.8233.17/content/cdnbundles/oldconvergedlogin_pcore.min_lwozjqawrstmtzsn2yunha2.js. Failed integrity metadata check.
    Console: [https://login.microsoftonline.com/common/oauth2/authorize?client_id=4345a7b9-9a63-4910-a426-35363201d503&response_mode=form_post&response_type=code+id_token&scope=openid+profile&state=OpenIdConnect.AuthenticationProperties%3d5xhL9s5iN_65agH7ctGnRfQlJHUHgSrEvD4vkaO323RyB1klBHD6Qh5qidm6GuaIHM8_GaSANKH6y1ohWHalX4QU_YyqGJqXV8wphi2TVMAAY3yyXQk3GB-yqWm0j3oh&nonce=636748812038183968.MmMxNjY2YjEtNDIwZS00ZDhhLWI3YmItMWRhMWM5ZmRmMzk4MjJkMGExMDItZDAxZi00MTZmLWIxYjctOTNmZWU2YjgzZDRi&redirect_uri=https%3a%2f%2fwww.office.com%2f&ui_locales=en-US&mkt=en-US:30] Failed to load external resource ['https://secure.aadcdn.microsoftonline-p.com/ests/2.1.8233.17/content/cdnbundles/oldconvergedlogin_pcore.min_lwozjqawrstmtzsn2yunha2.js']
    Console: [null:0] Cannot load script https://secure.aadcdn.microsoftonline-p.com/ests/2.1.8233.17/content/cdnbundles/convergedloginpaginatedstrings-en.min_uzcugprrg6vz0z16am4meq2.js. Failed integrity metadata check.
    Console: [https://login.microsoftonline.com/common/oauth2/authorize?client_id=4345a7b9-9a63-4910-a426-35363201d503&response_mode=form_post&response_type=code+id_token&scope=openid+profile&state=OpenIdConnect.AuthenticationProperties%3d5xhL9s5iN_65agH7ctGnRfQlJHUHgSrEvD4vkaO323RyB1klBHD6Qh5qidm6GuaIHM8_GaSANKH6y1ohWHalX4QU_YyqGJqXV8wphi2TVMAAY3yyXQk3GB-yqWm0j3oh&nonce=636748812038183968.MmMxNjY2YjEtNDIwZS00ZDhhLWI3YmItMWRhMWM5ZmRmMzk4MjJkMGExMDItZDAxZi00MTZmLWIxYjctOTNmZWU2YjgzZDRi&redirect_uri=https%3a%2f%2fwww.office.com%2f&ui_locales=en-US&mkt=en-US:30] Failed to load external resource ['https://secure.aadcdn.microsoftonline-p.com/ests/2.1.8233.17/content/cdnbundles/convergedloginpaginatedstrings-en.min_uzcugprrg6vz0z16am4meq2.js']
    

    The above will also show you the HTML that was loaded. It is too long to add.

    Some code from here to help troubleshoot the problem.

    I do not have Java 10. I used Java 8.

    0 讨论(0)
  • 2020-12-19 02:45

    I found out that web view is using webkit engine for mac os/ linux os and IE engine for windows machines. WebView on mac os is working fine but there is problem on windows machines. When I was investigating this issue I find out that there is problem in this IE engine. I have access to few machines with different version of IE 11 installed on. On machines with update version 11.0.85 I wasn't able to open this site, but when I tried on machine with update version 11.0.90 the problem doesn't exist anymore. So if someone is using Windows OS please try to update IE version maybe it will solve problem.

    0 讨论(0)
提交回复
热议问题