如何使cookie可用于域中的所有路径?

时间:2022-02-04 17:51:00

I created a cookie in a java filter and added back to the response

我在java过滤器中创建了一个cookie并添加回响应

response.addCookie()

before returning to the client node.js application. This web application is accessed using a localhost URL in the browser. After reading about cookie domain issue while using 'localhost', i did not set any domain or path in the cookie, while creating it.

在返回客户端node.js应用程序之前。使用浏览器中的localhost URL访问此Web应用程序。在使用'localhost'阅读有关cookie域问题后,我在创建cookie时没有在cookie中设置任何域或路径。

Now the Chrome or Firefox browsers don't show-up the cookie in the browser. All my URLs are http://localhost but, each page having different path.

现在Chrome或Firefox浏览器不会在浏览器中显示cookie。我的所有URL都是http:// localhost但是,每个页面都有不同的路径。

Step 1: During a request to http://localhost/app/login cookie is created and set in the response
Step 2: When the page loads after response, no cookies are shown in Chrome
Step 3: During the next request http://localhost/app/customer the previously created cookie is not recieved when trying request.getCookies().
Step 4: Before returning back to client application, a cookie is created
Step 5: Now the cookie created in Step 4 is shown in Chrome
Step 6: The next request is also sent to http://localhost/app/customer , now the cookie created in step 4 is recieved in the server as well

步骤1:在http:// localhost / app / login请求期间创建并在响应中设置cookie步骤2:响应后加载页面时,Chrome中不显示任何cookie步骤3:在下一个请求期间http:/ / localhost / app / customer尝试request.getCookies()时,不会收到以前创建的cookie。第4步:在返回客户端应用程序之前,创建一个cookie步骤5:现在在Chrome中显示在步骤4中创建的cookie步骤6:下一个请求也会发送到http:// localhost / app / customer,现在步骤4中创建的cookie也会在服务器中收到

If cookie creation for localhost is an issue, how does it work for Steps 4-6 only ?

如果为localhost创建cookie是个问题,那么它仅适用于步骤4-6?

How can i make the created cookie available to all paths under the localhost domain ? I tried using cookie.addPath("/") but, no change.

如何使创建的cookie可用于localhost域下的所有路径?我尝试使用cookie.addPath(“/”),但没有变化。

Note: Due to admin privilege issues in my development machine, i am not able to set-up a domain name to my localhost IP in etc/hosts file.

注意:由于我的开发计算机中存在管理员权限问题,我无法在etc / hosts文件中为我的localhost IP设置域名。

2 个解决方案

#1


Not sure path is the issue. Path does not affect whether a cookie is created; it only determines whether it is presented. If cookies aren't showing up in the browser's cookie jar they are being rejected for some reason other than path.

不确定路径是什么问题。路径不影响是否创建cookie;它只决定它是否呈现。如果cookie没有出现在浏览器的cookie jar中,那么它们会因路径以外的某些原因而被拒绝。

Chrome will not accept cookies for localhost because it does not accept cookies in the top level domain. The domain in the URL has to have a dot in it somewhere. So you could either add a hosts entry (recommended) or just trying using 127.0.0.1 instead of localhost.

Chrome不接受localhost的Cookie,因为它不接受*域名中的Cookie。 URL中的域必须在某处包含一个点。因此,您可以添加主机条目(推荐),也可以尝试使用127.0.0.1而不是localhost。

Also, none of this will work if the cookie is marked as secure or is being set with a domain attribute. If either of those is the case, you MUST use a hosts entry instead of localhost or 127.0.0.1.

此外,如果cookie被标记为安全或正在使用域属性设置,则这些都不起作用。如果是这种情况之一,则必须使用hosts条目而不是localhost或127.0.0.1。

#2


In your Java server, you should call cookie.setPath("/") before adding it to response.

在Java服务器中,应在将cookie.setPath(“/”)添加到响应之前调用它。

Such cookie will match all request URIs. It's a pity that it is not the default behavior.

这样的cookie将匹配所有请求URI。很遗憾它不是默认行为。

I have a more detailed explanation of cookie path here - http://bayou.io/release/0.9/javadoc/bayou/http/Cookie.html#path

我在这里有更详细的cookie路径解释 - http://bayou.io/release/0.9/javadoc/bayou/http/Cookie.html#path

#1


Not sure path is the issue. Path does not affect whether a cookie is created; it only determines whether it is presented. If cookies aren't showing up in the browser's cookie jar they are being rejected for some reason other than path.

不确定路径是什么问题。路径不影响是否创建cookie;它只决定它是否呈现。如果cookie没有出现在浏览器的cookie jar中,那么它们会因路径以外的某些原因而被拒绝。

Chrome will not accept cookies for localhost because it does not accept cookies in the top level domain. The domain in the URL has to have a dot in it somewhere. So you could either add a hosts entry (recommended) or just trying using 127.0.0.1 instead of localhost.

Chrome不接受localhost的Cookie,因为它不接受*域名中的Cookie。 URL中的域必须在某处包含一个点。因此,您可以添加主机条目(推荐),也可以尝试使用127.0.0.1而不是localhost。

Also, none of this will work if the cookie is marked as secure or is being set with a domain attribute. If either of those is the case, you MUST use a hosts entry instead of localhost or 127.0.0.1.

此外,如果cookie被标记为安全或正在使用域属性设置,则这些都不起作用。如果是这种情况之一,则必须使用hosts条目而不是localhost或127.0.0.1。

#2


In your Java server, you should call cookie.setPath("/") before adding it to response.

在Java服务器中,应在将cookie.setPath(“/”)添加到响应之前调用它。

Such cookie will match all request URIs. It's a pity that it is not the default behavior.

这样的cookie将匹配所有请求URI。很遗憾它不是默认行为。

I have a more detailed explanation of cookie path here - http://bayou.io/release/0.9/javadoc/bayou/http/Cookie.html#path

我在这里有更详细的cookie路径解释 - http://bayou.io/release/0.9/javadoc/bayou/http/Cookie.html#path