Why is my cookie always null?

I do not understand. It worked an hour ago, and suddenly I cannot return the cookie I just set. In Chrome, I see that there is actually a cookie, but if I try to return it null:

private void setLoggedInCookie(String sessionId) {
    String domain = this.getDomain();

    Cookies.setCookie(ApiParameters.LOGIN_COOKIE, sessionId, expires, domain, "/", true);
    String cookie = Cookies.getCookie(ApiParameters.LOGIN_COOKIE);

    // Getting NOTHING from this ..
    for (String string : Cookies.getCookieNames()) {
        LOGGER.info("Cookie name: " + string);
    }

    if(cookie == null) {
        throw new RuntimeException("Cookie is 'null'.");
    }
}

private String getDomain() {
    LOGGER.fine("Host name: " + Window.Location.getHostName());
    String domain = Window.Location.getHostName().replaceAll(".*//", "").replaceAll("/", "").replaceAll(":.*", "");
    return "localhost".equalsIgnoreCase(domain) ? "localhost" : domain;
}

What's happening?

0
source share
1 answer

You are transferring the null domain name. Browsers only allow access to cookies associated with the current domain. Since you are trying to access it from a page that is not “zero,” you cannot get it.

Also, make sure that you are trying to access it using SSL, because the "secure" parameter is set to true.

+2
source

Source: https://habr.com/ru/post/1783816/


All Articles