Clear session / cache / cookie in JavaFX WebView

I had a Swing dialog that uses JavaFX WebView to display oAuth 2.0 URLs from a Google server.

 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; } } } 

Each time, I get the following URL from Google. I will use SimpleSwingBrowser to load the following URL.

https://accounts.google.com/o/oauth2/auth?client_id=xxx&redirect_uri=http://localhostORE5757/Callback&response_type=code&scope=email%20https://www.googleapis.com/auth/drive.appdata% 20profile

During the first time , the following user interface will be displayed.

Screen 1

enter image description here

Screen 2

enter image description here

After I

  • Log in successfully on the One screen.
  • Presented with screen 2.
  • Click Accept.
  • Close the web browser dialog.
  • Again, create the same URL as the first time.
  • Create a completely new instance of SimpleSwingBrowser to load the URL generated in step

I expect Google to show me Screen One again, as this is a new browsing session. However, what I get the second time is Screen Two.

It appears that WebView has several stored sessions / cache / cookie, although this is a completely new instance.

I expect to return to Screen One to support multiple user accounts.

How to clear session / cache / cookie in WebView ?

+7
java swing javafx webview
source share
2 answers

Session cookies for JavaFX WebView are stored in java.net.CookieHandler .

To manage cookies yourself, create a new instance of java.net.CookieManager :

 java.net.CookieManager manager = new java.net.CookieManager(); 

Then set it as the default:

 java.net.CookieHandler.setDefault(manager); 

To clear cookies, simply call the removeAll method:

 manager.getCookieStore().removeAll(); 

or just create a new cookie manager instance and set it as the default:

 java.net.CookieHandler.setDefault(new java.net.CookieManager()); 
+17
source share

I used JavaFX 8 WebView to output OAuth 2.0 from Google as well as Dropbox. Turned off setting up a new java.net.CookieManager() default instance that worked with Google (and, of course, deleted session cookies), but I was no longer able to log into my Dropbox account. The Sing button just doesn't work.

I debugged and found out that the default instance is com.sun.webkit.network.CookieManager . So i used

 java.net.CookieHandler.setDefault(new com.sun.webkit.network.CookieManager()); 

who solved my problem. Due to its javadoc, it is compatible with RFC 6265, which is the current definition of HTTP Cookies and Set-Cookie headers.

You need to use the JDK (not just the JRE) as your project system library due to some restrictions on access to the JRE.

+7
source share

All Articles