Cookie 未被读取
我在我的网络应用程序中实现了“记住我”功能。我使用包含使用 RSA 加密的用户名/密码的 cookie 来完成此操作。
我在登录时添加cookie;如果然后我注销(不关闭浏览器),则 cookie 读取正常,并且在登录页面中我看到用户名/密码已输入。 但如果我关闭浏览器;或者关闭选项卡并再次运行应用程序,当读取 cookie 时,唯一读取的 cookie 是 JSESSIONID。带有凭据的 cookie 不在返回的数组中 ((HttpServletRequest)facesContext.getExternalContext().getRequest()).getCookies();
即使我可以在浏览器中看到它。这是为什么?
这是创建 cookie 的代码:
String credentials = username + "?" + password;
Cookie c = CookieHandler.getInstance().createCookie("vtcred", credentials, rememberMe);
FacesContext facesContext = FacesContext.getCurrentInstance();
((HttpServletResponse) facesContext.getExternalContext().getResponse()).addCookie(c);
和方法 createCookie:
public Cookie createCookie(String name, String value, boolean rememberMe) {
value = encript(value);
Cookie credCookie = new Cookie(name, value);
credCookie.setHttpOnly(true);
if(rememberMe) {
credCookie.setMaxAge(86400);
}
else {
credCookie.setMaxAge(0);
}
return credCookie;
}
编辑:我将 cookie 的最长期限设置为一天;在浏览器中我可以看到 cookie 明天就会过期,所以这不是问题
提前致谢, 达米安
edit2:这很奇怪,但现在似乎有效。我会继续测试并通知。谢谢。
I implemented "Remember me" functionality in my web app. I did this using a cookie that contains username/password encrypted using RSA.
I add the cookie when I login; if then I logout (without closing browser) the cookie is read ok and in the login page I see username/pass already typed.
But if I close the browser; or close tab and run the application again, when the cookies are read, the only cookie that is read is the JSESSIONID. the cookie with the credentials is not in the array returned by((HttpServletRequest)facesContext.getExternalContext().getRequest()).getCookies();
even though I can see it in the browser. why is that?
This is the code that creates the cookie:
String credentials = username + "?" + password;
Cookie c = CookieHandler.getInstance().createCookie("vtcred", credentials, rememberMe);
FacesContext facesContext = FacesContext.getCurrentInstance();
((HttpServletResponse) facesContext.getExternalContext().getResponse()).addCookie(c);
and method createCookie:
public Cookie createCookie(String name, String value, boolean rememberMe) {
value = encript(value);
Cookie credCookie = new Cookie(name, value);
credCookie.setHttpOnly(true);
if(rememberMe) {
credCookie.setMaxAge(86400);
}
else {
credCookie.setMaxAge(0);
}
return credCookie;
}
Edit: I am setting the cookie's max age to one day; and in the browser I can see that the cookie expires tomorrow, so that's not the problem
Thanks in advance,
Damian
edit2: this is very odd, but it seems to be working now. I'll keep testing it and notify. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现为什么有时 cookie 没有被读取。它与路径属性有关。
如果有人遇到此问题,请设置 cookie 的路径,如下所示:
问候
I found why sometimes a cookie is not read. It has to do with the path attribute.
If anyone is having this issue, set the path of the cookie, like this:
Regards
您可能想要设置 cookie 的过期日期。如果不这样做,它只会持续与浏览器会话一样长的时间。
You might want to set the cookie with an expiration date. If you dont , it will only last as long as the browser session.