使用 Servlet 处理 Cookie

发布于 2024-12-03 03:38:37 字数 1209 浏览 1 评论 0原文

我在 Servlet 内正确设置(持久/跨浏览器会话)cookie 数据并在过滤器中读取它时遇到问题。

Servlet 的代码(在登录时运行)是:

    String encodedValue = new String(Base64
        .encodeBase64(req.getParameter("account").getBytes()));
    Cookie cookie = new Cookie("projectAuthenticationCookie", encodedValue );
    cookie.setMaxAge(24*60*60);
    cookie.setPath("/");
    res.addCookie(cookie);

这将在响应中获取 cookie,但是当我使用以下代码在过滤器中读取它时:

    Cookie authenticationCookie = null;
    Cookie[] cookies = ((HttpServletRequest) request).getCookies();
    for (Cookie cookie : cookies){
        if ("projectAuthenticationCookie".equals(cookie.getName())) {
            authenticationCookie = cookie;
        }
    }

我只获取我设置正确的值,所有其他字段要么为 null,要么为空,要么不同。例如,Max Age 总是返回 -1,因此 cookie 永远不会持久。

cookie return value

我尝试使用以下方式设置过期标头:

    res.setDateHeader("Expires", System.currentTimeMillis() + 24*60*60*1000);

正如我读到的,如果没有有效的过期标头,会话将超时无论如何(如果我错了,请纠正我),但这也没有帮助...

我想到的一个问题是我在本地主机上运行(尝试设置 cookie.setDomain("localhost") 但也没有运气)。我的网络服务器/serclet 容器是 Jetty 7 但我不认为这是相关的......

有什么提示吗?

I am having a problem of setting the data of a (persistent/cross browser session) cookie correctly inside a Servlet and the reading it in a Filter.

the code of the Servlet (running at log-in time) is:

    String encodedValue = new String(Base64
        .encodeBase64(req.getParameter("account").getBytes()));
    Cookie cookie = new Cookie("projectAuthenticationCookie", encodedValue );
    cookie.setMaxAge(24*60*60);
    cookie.setPath("/");
    res.addCookie(cookie);

This will get the cookie inside the response, but the when I read it within my filter with the following code:

    Cookie authenticationCookie = null;
    Cookie[] cookies = ((HttpServletRequest) request).getCookies();
    for (Cookie cookie : cookies){
        if ("projectAuthenticationCookie".equals(cookie.getName())) {
            authenticationCookie = cookie;
        }
    }

I only get the value I set right, all other fields are either null, empty or different. Max age for example always returns -1 and thus the cookie will never persist.

cookie return values

I tried setting the expires-header with:

    res.setDateHeader("Expires", System.currentTimeMillis() + 24*60*60*1000);

as I read that without a valid expires-header the session will timeout anyway (correct me if I am wrong), but that didn't help either...

One issue I am thinking of is that I am running on localhost (tried setting cookie.setDomain("localhost") but also no luck). My web server/serclet container is Jetty 7 but I do not think that this is relevant...

Any hints?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

墟烟 2024-12-10 03:38:38

除了名称和值之外的字段不会填充(因此没有意义)在您从请求获得的 cookie 上。

这些字段旨在告知浏览器最大年龄; cookie的路径等,但浏览器不会将此信息发送回服务器。唯一需要正确的最大期限、路径等的情况是当您创建 cookie 并将其添加到响应中时。使用浏览器检查它是否存储了正确的信息,而不是尝试在服务器端查找它。

The fields other than name and value are not populated (and thus not meaningful) on cookies you get from a request.

These fields are intended to inform the browser about the max age; path, etc. of the cookie, but the browser doesn't send back this information to the server. The only time where it's important to have the correct max age, path, etc. is when you create a cookie and add it to the response. Use your browser to check if it stores the correct information instead of trying to find it at server-side.

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