Cannot sign in to Google in JavaFX WebView

后端 未结 1 1823
忘了有多久
忘了有多久 2020-12-10 16:28

I cannot sign in to Google in JavaFX WebView. The page doesn\'t load when I click the \'Next\' button.

Other logins on different websites work fine.

Here is

相关标签:
1条回答
  • 2020-12-10 17:00

    Short Version:

    Add the following line to your main method, before you load the page:

    System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
    

    Long Version:

    My first instinct was that JavaScript wasn't working, but I tested dummy emails and correctly got the error:

    Couldn't find your Google Account

    So it seemed like some JavaScript was working, but not the part which enabled the user to continue to enter their password. I added the following listener to listen for console errors, which I found here:

    com.sun.javafx.webkit.WebConsoleListener.setDefaultListener(
        (webView, message, lineNumber, sourceId) ->
            System.out.println("Console: [" + sourceId + ":" + lineNumber + "] " + message)
    );
    

    This resulted in the following error:

    Console: [null:0] XMLHttpRequest cannot load https://ssl.gstatic.com/accounts/static/_/js/blahblahblah
    Origin https://accounts.google.com is not allowed by Access-Control-Allow-Origin.

    This is a security feature called Same-Origin Policy. It's designed to stop pages being able to load scripts from potentially malicious third-party websites.

    I searched for "Same Origin Policy JavaFX" and found the following question which will solve your problem.

    The full application with both the fix and the additional logging is:

    public class CalendarController extends Application
    {
        @Override
        public void start(Stage primaryStage) throws Exception
        {
            WebView browser = new WebView();
    
            WebEngine webEngine = browser.getEngine();
    
            com.sun.javafx.webkit.WebConsoleListener.setDefaultListener(
                (webView, message, lineNumber, sourceId)-> System.out.println("Console: [" + sourceId + ":" + lineNumber + "] " + message)
            );
    
            webEngine.load("http://calendar.google.com");
    
            StackPane root = new StackPane();
            root.getChildren().add(browser);
    
            primaryStage.setScene(new Scene(root, 600, 600));
            primaryStage.show();
        }
    
        public static void main(String[] args)
        {
            System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
            launch(args);
        }
    }
    
    0 讨论(0)
提交回复
热议问题