Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear the session/cache/cookie in the JavaFX WebView

I had a Swing dialog, which uses JavaFX WebView to display oAuth 2.0 URL from 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;
        }
    }
}

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

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

During the first time, the following UI will be shown.

Screen One

enter image description here

Screen Two

enter image description here

After I

  1. Perform success login at Screen One.
  2. Presented with Screen Two.
  3. Click on Accept.
  4. Close the web browser dialog.
  5. Again, generate the exact same URL as first time.
  6. Create a completely new instance of SimpleSwingBrowser, to load URL generated at step

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.

It seems that, there are some stored session/cache/cookie in the WebView, even though it is a completely new instance.

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?

like image 638
Cheok Yan Cheng Avatar asked May 01 '14 13:05

Cheok Yan Cheng


People also ask

How do I clear cookies on Android WebView?

Open your browser. Android browser: Go to Menu > More > Settings or Menu > Settings > Privacy & Security. Chrome: Go to Menu > Settings > Privacy. Android browser: Tap Clear cache, Clear history, and Clear all cookie data as appropriate.

What is JavaFX WebView?

WebView is a Node that manages a WebEngine and displays its content. The associated WebEngine is created automatically at construction time and cannot be changed afterwards. WebView handles mouse and some keyboard events, and manages scrolling automatically, so there's no need to put it into a ScrollPane .

How do I display JavaFX in my browser?

The recommended way to embed a JavaFX application into a web page or launch it from inside a web browser is to use the Deployment Toolkit library. The Deployment Toolkit provides a JavaScript API to simplify web deployment of JavaFX applications and improve the end user experience with getting the application to start.


2 Answers

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.

I debugged and found out that per default an instance of com.sun.webkit.network.CookieManager is used. So, I used

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.

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

like image 173
gfkri Avatar answered Sep 22 '22 10:09

gfkri


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

To manage cookies on your own create new instance of 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:

manager.getCookieStore().removeAll();

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

java.net.CookieHandler.setDefault(new java.net.CookieManager());
like image 30
SimY4 Avatar answered Sep 20 '22 10:09

SimY4