简体   繁体   English

在Java中管理会话Cookie

[英]Managing session cookies in Java

I'm working in a java web application that should not allow a user to open it in 2 different tabs, and I'm using session cookies for that. 我正在一个Java Web应用程序中工作,该应用程序不应允许用户在2个不同的选项卡中打开它,而我正在为此使用会话cookie。 It seems to work fine in most scenarios, but the problem is that the cookies are not cleared when the browsers exits. 在大多数情况下,它似乎都可以正常工作,但是问题是,当浏览器退出时,不会清除cookie。 This is how I set the cookies: 这是我设置Cookie的方法:

String sCookie = "mycookie=true;Path=/;Domain=.mydomain.com;HttpOnly";
        if (!response.containsHeader("Set-Cookie")) {
            response.setHeader("Set-Cookie", sCookie);
        } else {
            response.addHeader("Set-Cookie", sCookie);
        }

As I understand, if I don't specify the Expires field, the cookie should be deleted on browser close. 据我了解,如果未指定Expires字段,则应在浏览器关闭时删除Cookie。 This is how I validate if the cookie exists: 这是我验证Cookie是否存在的方法:

Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if ("mycookie".equals(cookie.getName()) && Boolean.valueOf(cookie.getValue())) {//some error}}

Is there any problem with this code? 这段代码有什么问题吗? meaning, can I set the cookie with response.setHeader and then check it with request.getCookies() ? 意思是,我可以使用response.setHeader设置cookie,然后使用request.getCookies()检查它吗? Sometimes I have problems deleting the cookie manually and then when I restart the browser the problem continues. 有时我在手动删除Cookie时遇到问题,然后在重新启动浏览器时问题仍然存在。

This is how I manually delete the cookie (on tab close): 这是我手动删除Cookie的方式(在选项卡关闭时):

String sCookie = "mycookie=;Path=/;Domain=.mydomain.com;HttpOnly";
        if (!response.containsHeader("Set-Cookie")) {
            response.setHeader("Set-Cookie", sCookie);
        } else {
            response.addHeader("Set-Cookie", sCookie);
        }

Thanks in advance 提前致谢
UPDATE UPDATE
This is how I create the cookie: 这是我创建Cookie的方法:

Cookie c = new Cookie("mycookie","true");
        c.setDomain(".mydomain.com");
        c.setPath("/");
        c.setValue("true");
        response.addCookie(c);


This is how I delete the cookie: 这是删除Cookie的方法:

for (Cookie c : request.getCookies()) {
            if ("mycookie".equals(c.getName())) {
                c.setMaxAge(0);
                c.setValue("");
            }
        }


But still not working. 但仍然无法正常工作。 Actually, now the cookie is not deleted when I close the tab (this was working fine in my previous version with "Set-Cookie" :S).Another detail is that I'm not seeing my cookie in the Resources tab of Chrome's developer tools 实际上,现在我关闭选项卡时未删除cookie(在我以前的版本中使用“ Set-Cookie”:S可以正常工作)。另一个细节是我没有在Chrome开发人员的“资源”选项卡中看到cookie。工具

As a general hint, you'd better use the response.addCookie(..) method and possibly use Cookie.setMaxAge(-1) . 作为一般提示,您最好使用response.addCookie(..)方法,并可能使用Cookie.setMaxAge(-1)

That said, that should be the default, so in order to understand the problem, you should use Firebug (or any browser developer tools) to inspect your cookies and check their max age. 就是说,这应该是默认设置,因此为了理解问题,您应该使用Firebug(或任何浏览器开发人员工具)检查Cookie并检查其最长期限。 Before and after closing the browser. 关闭浏览器之前和之后。 Eg you may have some leftover cookie. 例如,您可能有一些剩余的cookie。

Actually you should set the cookies in different way: 实际上,您应该以不同的方式设置Cookie:

Cookie myCookie = new Cookie();  // create your cookie
// set path, and other attributes you need

// add the cookie to the response
response.addCookie(myCookie);

Then to make a Cookie expire: : 然后使Cookie过期:

myCookie.setMaxAge(0);

Also, in order to clean completely: 另外,为了彻底清洁:

myCookie.setValue("");
myCookie.setPath("/");

So, you have to get all the cookies in the request, identify your's and clean it with something like this: 因此,您必须获取请求中的所有cookie,识别您的cookie并使用类似以下内容的方法对其进行清理:

List<Cookie> cookies = request.getCookies();

for (Cookie cookie : cookies) {
    // identify your cookie
    if (identified) {
        cookie.setMaxAge(0);
        cookie.setValue("");
        cookie.setPath("/");
    }
}

If cookie.getName("Set-Cookie") does not match your Cookie , debug your code to see what name is assigned in the response.setHeader("Set-Cookie", sCookie); 如果cookie.getName("Set-Cookie")与您的Cookie不匹配,请调试代码以查看在response.setHeader("Set-Cookie", sCookie);分配了什么名称response.setHeader("Set-Cookie", sCookie);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM