This document describes how to work with cookies.

Overview

JxBrowser delegates the work with cookies to the Chromium engine. Chromium decides how to download cookies from a web server, extract them from the HTTP headers and store them in a local file system (persistent cookies) or in the memory (session cookies).

The CookieService class allows you to get, modify, and remove cookies. The Cookie class provides information on a particular cookie.

Supported Protocols

JxBrowser supports cookies that are sent using the following protocols:

  • HTTP
  • HTTPS
  • WS (WebSocket)
  • WSS (Secured WebSocket)

If a cookie is sent using a protocol that is not on the list, e.g. ftp://, it will not be stored in the cookie storage.

Working with Cookies

JxBrowser supports the following kinds of cookies:

  • Persistent cookies — these are stored in the Chromium user data directory. If you delete the Chromium user data directory, all the persistent cookies will be removed.
  • Session cookies — these are stored in the application memory. These cookies will be removed automatically when the application is terminated.
  • Secure cookies — these can only be transmitted over an encrypted connection, i.e. HTTPS. This makes the cookie less likely to be exposed to cookie theft via eavesdropping.
  • HttpOnly cookies — these cannot be accessed by the client-side APIs, such as JavaScript. This restriction eliminates the threat of cookie theft via cross-site scripting (XSS). However, the cookie remains vulnerable to cross-site tracing (XST) and cross-site request forgery (XSRF) attacks.

When you modify cookies please use the CookieService.saveCookies() method to save the changes.

Getting Cookies

To get all cookies, please use the getCookies() method:

Collection<Cookie> cookies = cookieService.getCookies();
for (Cookie cookie : cookies) {
    System.out.println("cookie = " + cookie);
}

To get all cookies by a URL, please use the getCookies() method that accepts a string:

Collection<Cookie> cookies = cookieService.getCookies("https://www.google.com");
for (Cookie cookie : cookies) {
    System.out.println("cookie = " + cookie);
}

Creating Cookies

Persistent

To create a persistent cookie use the following code:

boolean success = cookieService.setCookie("http://www.google.com",
        Cookie.newBuilder()
                .setCreationTime(creationTime)
                .setExpirationTime(expirationTime)
                .setName("name")
                .setValue("value")
                .setDomain(".google.com")
                .setPath("/")
                .build());
cookieService.saveCookies();

The code above creates a persistent cookie for the http://www.google.com URL. The cookie expires in an hour. The success variable will be true if the cookie is created and added to the cookie storage successfully.

Session

To create a session cookie use the following code:

boolean success = cookieService.setCookie("http://www.google.com",
        Cookie.newBuilder()
                .setName("name")
                .setValue("value")
                .setDomain(".google.com")
                .setPath("/")
                .build());
cookieService.saveCookies();

Deleting Cookies

To delete all cookies use the deleteCookies() method:

int numberOfDeletedCookies = cookieService.deleteCookies();
cookieService.saveCookies();

To delete one cookie, please use the deleteCookie(Cookie). The following code deletes all cookies one by one obtaining the result of the operation:

Collection<Cookie> cookies = cookieService.getCookies();
for (Cookie cookie : cookies) {
    boolean success = cookieService.deleteCookie(cookie);
    ...
}
cookieService.saveCookies();

Suppressing Cookies

You can control all incoming and outgoing cookies using the CanSetCookieCallback and CanGetCookiesCallback callbacks of the NetworkService.

To suppress the incoming cookies use the following code:

networkService.set(CanSetCookieCallback.class, (params, callback) ->
        callback.cannot());

To suppress the outgoing cookies use the following code:

networkService.set(CanGetCookiesCallback.class, (params, callback) ->
        callback.cannot());

Encryption

JxBrowser supports the cookie encryption by default. It uses the Chromium cookies encryption routines, so the cookies are stored exactly as in Chromium.

Linux

On Linux JxBrowser uses GNOME Keyring or KWallet to encrypt cookies. The library automatically chooses which store to use. You can manually specify which store to use via an appropriate option when constructing the Engine. For example:

Engine engine = Engine.newInstance(EngineOptions.newBuilder()
        .setPasswordStore(PasswordStoreType.GNOME_KEYRING)
        ...
        .build());

Windows

On Windows JxBrowser uses only DPAPI to encrypt cookies. There are no alternatives at the moment.

macOS

On macOS JxBrowser uses the private key stored with the Keychain Application to encrypt cookies with AES encryption.

Go Top