无法在 JavaFX WebView 中登录 Google
Cannot sign in to Google in JavaFX WebView
我无法在 JavaFX WebView 中登录 Google。单击 'Next' 按钮时页面未加载。
其他网站登录正常。
这是一个你可以运行:
的例子
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;
public class App extends Application
{
@Override
public void start(Stage primaryStage) throws Exception
{
WebView browser = new WebView();
WebEngine webEngine = browser.getEngine();
webEngine.load("https://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)
{
launch(args);
}
}
Screenshot here
短版:
在加载页面之前,将以下行添加到您的主要方法中:
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
长版:
我的第一直觉是 JavaScript 不工作,但我测试了虚拟电子邮件并正确地得到了错误:
Couldn't find your Google Account
所以看起来有些 JavaScript 在工作,但不是使用户能够继续输入密码的部分。我添加了以下侦听器来侦听控制台错误,which I found here:
com.sun.javafx.webkit.WebConsoleListener.setDefaultListener(
(webView, message, lineNumber, sourceId) ->
System.out.println("Console: [" + sourceId + ":" + lineNumber + "] " + message)
);
这导致了以下错误:
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.
这是一项名为 Same-Origin Policy 的安全功能。它旨在阻止页面从潜在的恶意第三方网站加载脚本。
我搜索了 "Same Origin Policy JavaFX" 和 found the following question 可以解决您的问题。
包含修复和附加日志记录的完整应用程序是:
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);
}
}
我无法在 JavaFX WebView 中登录 Google。单击 'Next' 按钮时页面未加载。
其他网站登录正常。
这是一个你可以运行:
的例子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;
public class App extends Application
{
@Override
public void start(Stage primaryStage) throws Exception
{
WebView browser = new WebView();
WebEngine webEngine = browser.getEngine();
webEngine.load("https://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)
{
launch(args);
}
}
Screenshot here
短版:
在加载页面之前,将以下行添加到您的主要方法中:
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
长版:
我的第一直觉是 JavaScript 不工作,但我测试了虚拟电子邮件并正确地得到了错误:
Couldn't find your Google Account
所以看起来有些 JavaScript 在工作,但不是使用户能够继续输入密码的部分。我添加了以下侦听器来侦听控制台错误,which I found here:
com.sun.javafx.webkit.WebConsoleListener.setDefaultListener(
(webView, message, lineNumber, sourceId) ->
System.out.println("Console: [" + sourceId + ":" + lineNumber + "] " + message)
);
这导致了以下错误:
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.
这是一项名为 Same-Origin Policy 的安全功能。它旨在阻止页面从潜在的恶意第三方网站加载脚本。
我搜索了 "Same Origin Policy JavaFX" 和 found the following question 可以解决您的问题。
包含修复和附加日志记录的完整应用程序是:
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);
}
}