无法在JavaFX WebView中登录Google [英] Cannot sign in to Google in JavaFX WebView
问题描述
我无法在JavaFX WebView中登录Google。单击下一步按钮时,页面无法加载。
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 an example you can run:
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);
}
}
推荐答案
简短版本:
在加载页面之前,在主方法中添加以下行:
Add the following line to your main method, before you load the page:
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
长版本:
我的第一直觉是JavaScript无法正常工作,但我测试了虚拟电子邮件并正确地收到错误:
My first instinct was that JavaScript wasn't working, but I tested dummy emails and correctly got the error:
couldn'找到你的Google帐户
Couldn't find your Google Account
所以看起来有些JavaScript正在运行,但不是让用户继续输入他们的部分密码。我添加了以下监听器来监听控制台错误,我在这里找到的:
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)
);
这导致以下错误:
控制台:[null:0] XMLHttpRequest无法加载
https://ssl.gstatic.com/accounts/static/_/js/blahblahblah
来源 Access-Control-Allow-Origin不允许使用=noreferrer> https://accounts.google.com 。
这是一项名为同源政策的安全功能。它旨在阻止页面从潜在的恶意第三方网站加载脚本。
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.
我搜索了同源策略JavaFX和发现以下问题,这将解决您的问题。
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);
}
}
这篇关于无法在JavaFX WebView中登录Google的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!