Clear the session/cache/cookie in the JavaFX WebView

前端 未结 2 1761
星月不相逢
星月不相逢 2020-12-10 05:29

I had a Swing dialog, which uses JavaFX WebView to display oAuth 2.0 URL from Google server.

public class SimpleSwingBrowser extends JDialog {

         


        
相关标签:
2条回答
  • 2020-12-10 05:57

    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());
    
    0 讨论(0)
  • 2020-12-10 06:17

    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.

    0 讨论(0)
提交回复
热议问题