java Cookie 未在 chrome 或 firefox 中的本地主机上设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40664959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:23:38  来源:igfitidea点击:

Cookie is not set on localhost in chrome or firefox

javagoogle-chromecookiesjersey

提问by Amnestic

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

我正在使用 Jersey 服务器,它以以下方式返回 cookie:

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

当我调用返回 cookie 的方法时,我在 chrome 中得到以下结果: 请求和响应头

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

我什至可以看到 chrome 已经识别出我的 cookie: Cookie 已识别

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

但由于某种原因,它没有在 cookie 选项卡中设置:

No cookie shown

没有显示 cookie

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

我曾尝试将域设置为 false、null、"",在重命名 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...

适用于 IE 11,但仍不适用于 Chrome 或 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.

我曾多次尝试为 127.0.0.1 插入另一个主机名。在此示例中,它是 text.myexample.com。它仍然无法在 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:

我尝试在 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.

这是Chrome中服务器返回的标头中的cookie。这很好用。我真的不知道这里发生了什么。

采纳答案by Amnestic

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.

原来问题与我正在使用的 fetch 库有关。如果您不在请求中包含 {credentials: 'same-origin'},则不会设置响应 cookie。

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

有关更多信息,请参阅https://github.com/github/fetch/issues/386

回答by gladiator

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

它仅与本地主机有关,在其他 url 上运行良好,请参阅下面的链接,此处 cookie 有效,但在本地它失败 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

响应标题内容长度:18 内容类型:文本/html 日期:2016 年 11 月 25 日星期五 10:19:15 GMT 处理时间:0 毫秒服务器:Apache-Coyote/1.1 Set-Cookie:userAccessToken=toke;lang=en-我们; 路径=/;域=本地主机

Network tab

网络选项卡