我在java过滤器中创建了一个cookie并添加回响应
response.addCookie()
返回客户端node.js应用程序之前。使用浏览器中的localhost URL访问此Web应用程序。在使用'localhost'阅读有关cookie域问题后,我在创建cookie时没有在cookie中设置任何域或路径。
现在Chrome或Firefox浏览器不会在浏览器中显示cookie。我的所有网址都是http://localhost,但每个网页都有不同的路径。
步骤1:在http://localhost/app/login的请求期间创建cookie并在响应中设置
第2步:在响应后加载页面时,Chrome中没有显示任何cookie
第3步:在下一个请求http://localhost/app/customer期间,尝试request.getCookies()时,不会收到先前创建的cookie。
第4步:在返回客户端应用程序之前,创建一个cookie
第5步:现在,步骤4中创建的cookie显示在Chrome中
第6步:下一个请求也会发送到http://localhost/app/customer,现在服务器中也会收到步骤4中创建的cookie
如果为localhost创建cookie是一个问题,它仅对步骤4-6有用吗?
如何将创建的cookie提供给所有路径下的所有路径 localhost域名?我尝试使用cookie.addPath(“/”),但没有变化。
注意 :由于我的开发计算机中存在管理员权限问题,我无法在etc / hosts文件中为我的localhost IP设置域名。
答案 0 :(得分:1)
不确定路径是什么问题。路径不影响是否创建cookie;它只决定它是否呈现。如果cookie没有出现在浏览器的cookie jar中,则由于路径以外的某些原因而被拒绝。
Chrome不接受localhost的cookie,因为它不接受顶级域中的cookie。 URL中的域必须在某处包含一个点。因此,您可以添加主机条目(推荐),也可以尝试使用127.0.0.1而不是localhost。
此外,如果cookie被标记为安全或正在使用域属性设置,则这些都不起作用。如果是这种情况之一,则必须使用hosts条目而不是localhost或127.0.0.1。
答案 1 :(得分:0)
In your Java server, you should call import java.util.*;
public class Test {
public static void main(String[] args) {
boolean b = false;
if (b==true)
System.out.println("one. b = " + Boolean.toString(b));
if (b)
System.out.println("two. b = " + Boolean.toString(b));
}
}
before adding it to response.
Such cookie will match all request URIs. It's a pity that it is not the default behavior.
I have a more detailed explanation of cookie path here - http://bayou.io/release/0.9/javadoc/bayou/http/Cookie.html#path