15

I am working with a Jersey server which returns a cookie in the following way:

return Response.ok()
    .cookie(
        new NewCookie(
            "userAccessToken", userTokenDTO.getToken(), "/", "", 
            "what is this", 3600, false
        )
    ).build();

When I call the method which returns the cookie, I get the following result in chrome: Request and response headers

I can even see that chrome has recognized my cookie: Cookie recognized

But for some reason it isn't set in the cookie tab:

No cookie shown

I have tried setting the domain both to false, null, "", creating an entry in the hosts file renaming 127.0.0.1.

return Response.ok()
    .cookie(
            new NewCookie(
                    "userAccessToken", userTokenDTO.getToken(), "/", "127.0.0.1",
                    "what is this", 3600, false)
    ).build();

Works in IE 11, but still not Chrome nor Firefox...

I have tried multiple time to insert another host name for 127.0.0.1. In this example it is text.myexample.com. It still doesn't work in any other browser than IE11.

return Response.ok()
    .cookie(
            new NewCookie(
                    "userAccessToken", userTokenDTO.getToken(), "/", "test.myexample.com",
                    "what", 7200, false)
    ).build();

I tried to do the following in the console of Google Chrome:

document.cookie = "userAccessToken=72bebbe0-44fd-45ce-a6e1-accb72201eff;Version=1;Comment=what;Domain=test.myexample.com;Path=/;Max-Age=7200"

Which is the cookie in the header returned by the server in Chrome. That works fine. I have literally no clue what is going on here.

Amnestic
  • 650
  • 2
  • 8
  • 23
  • 1
    Maybe this is related: http://stackoverflow.com/questions/1134290/cookies-on-localhost-with-explicit-domain try in your hosts file put a hostname for your application, e.g. `127.0.0.1 myapp.local` and use it as a hostname. – Alexander Arutinyants Nov 20 '16 at 18:30
  • 2
    I have already tried that with multiple different names with no luck. – Amnestic Nov 21 '16 at 20:37
  • Its a issue with localhost only ,works well on other urls see below link here cookie works but in local it fails http://jerseyexample-ravikant.rhcloud.com/rest/jws/say/Hi – gladiator Nov 23 '16 at 11:35

2 Answers2

13

Turns out the problem was related to the fetch library I am using. If you do not include { credentials: "same-origin" } in the request, the response cookie isn't being set.

For more information, see: https://github.com/github/fetch/issues/386.

aymericbeaumet
  • 6,853
  • 2
  • 37
  • 50
Amnestic
  • 650
  • 2
  • 8
  • 23
0

Its a issue with localhost only ,works well on other urls see below link here cookie works but in local it fails http://jerseyexample-ravikant.rhcloud.com/rest/jws/say/Hi

for localhost you can go with below .

return Response.status(200).entity(output)

                .header("Set-Cookie", "userAccessToken=toke;lang=en-US; Path=/; Domain=localhost")
                .build(); 

enter image description here

See the network tab

Response Headers Content-Length:18 Content-Type:text/html Date:Fri, 25 Nov 2016 10:19:15 GMT ProcessingTime:0 millisecs Server:Apache-Coyote/1.1 Set-Cookie:userAccessToken=toke;lang=en-US; Path=/; Domain=localhost

Network tab

gladiator
  • 1,249
  • 8
  • 23