【问题标题】:"Expires" attribute points to a date in the past“Expires”属性指向过去的日期
【发布时间】:2014-06-23 02:20:53
【问题描述】:

我正在尝试为我的网站设置 cookie。我正在使用:

ServletActionContext.getResponse().addCookie(<my cookie instance>);

Cookie maxAge 设置为“2592000”。但是......由于某些未知原因,标题看起来像这样:

Server:Wildfly 8
Set-Cookie:LOCALE=en_GB; path=/scores;
    Max-Age=2592000; Expires=Mon, 02-Jun-2014 19:17:54 GMT

今天是根据标题:

Date:Sun, 22 Jun 2014 12:20:41 GMT

,所以我很惊讶为什么“过期”字段日期指向过去的日期。我完全没有想法。我将不胜感激任何提示。也许我需要配置一些东西?

编辑: 这是我创建 cookie 的方法:

public Cookie getCookie(final String name, final String value, final int maxAge, final HttpServletRequest request)
{
    final Cookie cookie = new Cookie(name, value); 
    cookie.setMaxAge(maxAge); 
    cookie.setPath(request.getContextPath()); 
    cookie.setSecure(false); 
    return cookie;
}

编辑2: 我刚刚注意到使用 Jboss 7 时发送到浏览器的标头格式之间存在差异。没有 Max-age 存在 - 只是计算(并正确)到期时间。

 Server:Apache-Coyote/1.1
 Set-Cookie:LOCALE=en_GB; Expires=Tue, 22-Jul-2014 13:44:00 GMT; Path=/scores

似乎应该以某种方式配置 Jboss 8 Wildfly 来计算 cookie 的到期时间。不幸的是,我必须使用 Wildfly。

【问题讨论】:

  • 展示如何创建 cookie 实例。
  • 这里没什么特别的:final Cookie cookie = new Cookie(name, value); cookie.setMaxAge(maxAge); cookie.setPath(request.getContextPath()); cookie.setSecure(false);返回 cookie;
  • 不要在 cmets 中发布代码,编辑问题以提供详细信息。这样做是为了获得更好的帮助,请尽早准备SSCCE。

标签: cookies struts2 jboss


【解决方案1】:

我仍然在浪费时间(我今天已经浪费了 8 个小时)试图解决这个问题,但是......我有一个解决方法(它可以在未来帮助某人)和一个有趣的事实。

如下代码行:

 ServletActionContext.getResponse().addCookie(newInstance(name, value, maximumAge, request));

我已经用这个代替了:

protected void set(final String name, final String value, final long maximumAgeInSeconds, final HttpServletRequest request)
{
    // ServletActionContext.getResponse().addCookie(newInstance(name, value, maximumAgeInSeconds, request));
    ServletActionContext.getResponse().addHeader("Set-Cookie", cookieValue(name, value, maximumAge, request));
}
protected static String cookieValue(final String name, final String value, final long maximumAgeInSeconds, final HttpServletRequest request)
{
    final SimpleDateFormat cookieExpiresHeaderFormat = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss zzz");
    cookieExpiresHeaderFormat.setTimeZone(new SimpleTimeZone(0, "GMT"));
    final Date d = new Date();
    d.setTime(d.getTime() + maximumAgeInSeconds * 1000L);
    final String cookieLifeTime = cookieExpiresHeaderFormat.format(d);
    return name + "=" + value + "; Expires=" + cookieLifeTime + "; Max-Age=" + maximumAge + "; Path=" + request.getContextPath();
}

那个工作正常,似乎我在 Jboss 8 中发现了一个错误,或者......任何将 cookie 转换为标头的库。 如果 maxAgeInSeconds 变量等于 30 天,则转换为毫秒 2592000*1000=2592000000 与 MAX_INT(即 2147483647)重叠,因此它的值变为“-1702967296”!!!!

例子:

当前时间:1403460809297 - 1702967296 =(以秒为单位)1401757842

这是: 格林威治标准时间:格林威治标准时间 2014 年 6 月 3 日星期二 01:10:42

这就是我收到过去的到期日期的原因。这似乎是一种机制,将 Cookie 对象 Max-Age 属性转换为 Cookie 标头的“Expires”属性,使用 INTeger 而不是更宽的 Long 类型意外设置过去的 cookie 到期日期。 有趣的事实是,该问题适用于 IE,但不适用于 Chrome,例如,Chrome 使用 Max-age 进行内部到期时间计算,忽略了 Cookie 中的到期时间。在这种情况下,IE 忽略了 Max-Age 属性值,该属性值已经为接收到的 Cookie 计算了 Expires 属性值。

【讨论】:

    猜你喜欢
    • 2020-12-20
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    • 2019-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多