Cookie 未被读取

发布于 2024-11-05 06:43:54 字数 1231 浏览 0 评论 0原文

我在我的网络应用程序中实现了“记住我”功能。我使用包含使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温暖的光 2024-11-12 06:43:54

我发现为什么有时 cookie 没有被读取。它与路径属性有关。

如果有人遇到此问题,请设置 cookie 的路径,如下所示:

Cookie c = new Cookie("name", "value");
cookie.setMaxAge(86400);
cookie.setPath("/");

问候

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:

Cookie c = new Cookie("name", "value");
cookie.setMaxAge(86400);
cookie.setPath("/");

Regards

山田美奈子 2024-11-12 06:43:54

您可能想要设置 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文