清除 JavaFX WebView 中的 session/cache/cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23409138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clear the session/cache/cookie in the JavaFX WebView
提问by Cheok Yan Cheng
I had a Swing dialog, which uses JavaFX WebView
to display oAuth 2.0 URL from Google server.
我有一个 Swing 对话框,它使用 JavaFXWebView
显示来自 Google 服务器的 oAuth 2.0 URL。
public class SimpleSwingBrowser extends JDialog {
private final JFXPanel jfxPanel = new JFXPanel();
private WebEngine engine;
private final JPanel panel = new JPanel(new BorderLayout());
public SimpleSwingBrowser() {
super(MainFrame.getInstance(), JDialog.ModalityType.APPLICATION_MODAL);
initComponents();
}
private void initComponents() {
createScene();
panel.add(jfxPanel, BorderLayout.CENTER);
getContentPane().add(panel);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-460)/2, (screenSize.height-680)/2, 460, 680);
}
private void createScene() {
Platform.runLater(new Runnable() {
@Override
public void run() {
final WebView view = new WebView();
engine = view.getEngine();
engine.titleProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, final String newValue) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
SimpleSwingBrowser.this.setTitle(newValue);
}
});
}
});
engine.getLoadWorker()
.exceptionProperty()
.addListener(new ChangeListener<Throwable>() {
public void changed(ObservableValue<? extends Throwable> o, Throwable old, final Throwable value) {
if (engine.getLoadWorker().getState() == FAILED) {
SwingUtilities.invokeLater(new Runnable() {
@Override public void run() {
JOptionPane.showMessageDialog(
panel,
(value != null) ?
engine.getLocation() + "\n" + value.getMessage() :
engine.getLocation() + "\nUnexpected error.",
"Loading error...",
JOptionPane.ERROR_MESSAGE);
}
});
}
}
});
// http://stackoverflow.com/questions/11206942/how-to-hide-scrollbars-in-the-javafx-webview
// hide webview scrollbars whenever they appear.
view.getChildrenUnmodifiable().addListener(new ListChangeListener<Node>() {
@Override
public void onChanged(Change<? extends Node> change) {
Set<Node> deadSeaScrolls = view.lookupAll(".scroll-bar");
for (Node scroll : deadSeaScrolls) {
scroll.setVisible(false);
}
}
});
jfxPanel.setScene(new Scene(view));
}
});
}
public void loadURL(final String url) {
Platform.runLater(new Runnable() {
@Override
public void run() {
String tmp = toURL(url);
if (tmp == null) {
tmp = toURL("http://" + url);
}
engine.load(tmp);
}
});
}
private static String toURL(String str) {
try {
return new URL(str).toExternalForm();
} catch (MalformedURLException exception) {
return null;
}
}
}
Everytime, I will get the following URL from Google. I will use the SimpleSwingBrowser
to load the following URL.
每次,我都会从 Google 获取以下 URL。我将使用SimpleSwingBrowser
加载以下 URL。
During the first time, the following UI will be shown.
在第一次时,将显示以下 UI。
Screen One
屏幕一
Screen Two
屏幕二
After I
在我之后
- Perform success login at Screen One.
- Presented with Screen Two.
- Click on Accept.
- Close the web browser dialog.
- Again, generate the exact same URL as first time.
- Create a completely new instance of
SimpleSwingBrowser
, to load URL generated at step
- 在屏幕一执行成功登录。
- 与屏幕二一起呈现。
- 单击接受。
- 关闭 Web 浏览器对话框。
- 同样,生成与第一次完全相同的 URL。
- 创建一个全新的 , 实例
SimpleSwingBrowser
以加载步骤中生成的 URL
I expect Google will show me Screen One again, as this is a new browsing session. However, what I'm getting for the 2nd time, is Screen Two.
我希望 Google 会再次向我展示 Screen One,因为这是一个新的浏览会话。然而,我第二次得到的是屏幕二。
It seems that, there are some stored session/cache/cookie in the WebView
, even though it is a completely new instance.
似乎WebView
,即使它是一个全新的实例,也有一些存储的会话/缓存/cookie 。
I expect I will get myself back to Screen One, so that I can support multiple user accounts.
我希望我能回到屏幕一,这样我就可以支持多个用户帐户。
How can I clear the session/cache/cookie in the WebView
?
如何清除WebView
.
采纳答案by SimY4
Session cookies for JavaFX WebView are stored in java.net.CookieHandler
.
JavaFX WebView 的会话 cookie 存储在java.net.CookieHandler
.
To manage cookies on your own create new instance of java.net.CookieManager
:
要自行管理 cookie,请创建 的新实例java.net.CookieManager
:
java.net.CookieManager manager = new java.net.CookieManager();
Then set it as default:
然后将其设置为默认值:
java.net.CookieHandler.setDefault(manager);
To clear cookies just call removeAll method:
要清除 cookie,只需调用 removeAll 方法:
manager.getCookieStore().removeAll();
or just create new instance of cookie manager and set it as default:
或者只是创建 cookie 管理器的新实例并将其设置为默认值:
java.net.CookieHandler.setDefault(new java.net.CookieManager());
回答by gfkri
I used JavaFX 8 WebView to display OAuth 2.0 from Google as well as from Dropbox. Turned out setting a new instance of java.net.CookieManager()
as default worked with Google (and of course removed the session cookies), but I wasn't able to sign in with my Dropbox account anymore. The "Sing in" button just did not work.
我使用 JavaFX 8 WebView 来显示来自 Google 和 Dropbox 的 OAuth 2.0。原来设置一个新的java.net.CookieManager()
默认实例与谷歌一起工作(当然删除了会话 cookie),但我无法再使用我的 Dropbox 帐户登录。“Sing in”按钮不起作用。
I debugged and found out that per default an instance of com.sun.webkit.network.CookieManager
is used. So, I used
我调试并发现默认情况下com.sun.webkit.network.CookieManager
使用了一个实例。所以,我用
java.net.CookieHandler.setDefault(new com.sun.webkit.network.CookieManager());
which solved my problem. Due to its javadoc it's RFC 6265-compliant, which is the current definition of HTTP Cookies and Set-Cookie header fields.
这解决了我的问题。由于其 javadoc,它符合 RFC 6265,这是 HTTP Cookie 和 Set-Cookie 标头字段的当前定义。
You need to use the JDK (not just the JRE) as your project's system library due to some access restrictions in the JRE.
由于 JRE 中的一些访问限制,您需要使用 JDK(不仅仅是 JRE)作为项目的系统库。