我在javafx的webview组件中打开login.microsoftonline.com页面时遇到了问题。我有简单的代码,可以毫不费力地打开这个页面:
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上执行相同的代码时,网站正在打开:

我使用的是java 10,我真的不知道出了什么问题。有没有人有同样的问题?你知道怎么解决这个问题吗?也许我可以用其他的组件来代替webview来做我的事情?
发布于 2018-10-12 03:11:47
这不是你的问题的解决方案,但可能会把你引向正确的方向。看起来网站主体是使用script加载的。这个脚本依赖于我猜测的其他脚本。似乎没有其他脚本正在加载。
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);
}
}输出:
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']HTML上面还会显示加载的。它太长,无法添加。
here中的一些代码可帮助解决此问题。
我没有Java 10,我用的是Java 8。
发布于 2018-10-22 20:17:55
我发现web view在mac os/ linux os上使用webkit引擎,在windows机器上使用IE引擎。在mac os上的WebView运行得很好,但在windows机器上却有问题。当我调查这个问题时,我发现这个IE引擎有问题。我可以访问几台安装了不同版本的IE 11的机器。在更新版本为11.0.85的计算机上,我无法打开此站点,但当我在更新版本为11.0.90的计算机上尝试时,该问题不再存在。因此,如果有人正在使用Windows操作系统,请尝试更新IE版本,也许可以解决问题。
发布于 2018-12-05 00:34:37
我也有同样的问题,我想我找到了关键点:外部脚本/链接完整性失败。这不是平台浏览器问题,JavaFX (OpenJFK)依赖于嵌入式webkit引擎。
在windows JDK 8上,回归发生在版本40和版本172之间。它在Oracle JDK 9.0.4上运行良好。它不能在Oracle JDK 11上运行
更多详情请访问:https://github.com/mguessan/davmail/issues/12
问题类似于:Javafx - open login.microsoftonline.com page in webview component
=>更新的答案:实现覆盖微软表单内容并禁用完整性检查。这不是对webkit错误的修复,而是一个变通方法。
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");
}https://stackoverflow.com/questions/52747674
复制相似问题