Problem with JavaFX WebView about Access-Control-Allow-Origin
I use a javafx webview in my plugin, and I have a problem in cross domain.
I use :
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
It works well in my demo:
public class App extends Application
{
@Override
public void start(Stage primaryStage) throws Exception
{
TrustManager trm = new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {return null;}
public void checkClientTrusted(X509Certificate[] certs, String authType) {}
public void checkServerTrusted(X509Certificate[] certs, String authType) {}
};
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[] { trm }, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
WebView browser = new WebView();
WebEngine webEngine = browser.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.getLoadWorker().stateProperty()
.addListener((obs, oldValue, newValue) -> {
System.out.println("state property ->" + obs.toString());
if (newValue == Worker.State.SUCCEEDED) {
System.out.println("finished loading");
}
}); // addListener()
webEngine.getLoadWorker().exceptionProperty().addListener(new ChangeListener<Throwable>() {
@Override
public void changed(ObservableValue<? extends Throwable> ov, Throwable t, Throwable t1) {
System.out.println("Received exception: "+t1.getMessage());
}
});
webEngine.load(url);
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);
}
}
but that doesn't works in the plugin, even if I added
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
before
webEngine.load(url);
and I log the info System.getProperty("sun.net.http.allowRestrictedHeaders"), the value is "true"
So, how to allow webview cross domain access
Please sign in to leave a comment.
请看我这个答案:
https://stackoverflow.com/questions/16215844/javafx-webview-disable-same-origin-policy-allow-cross-domain-requests?answertab=active#tab-top
有什么问题请在下面回复我