【问题标题】:How to correct set the cookies for a subdomain in JSP?如何在 JSP 中正确设置子域的 cookie?
【发布时间】:2018-11-30 16:29:59
【问题描述】:

我有以下设置:

  • 所有请求都是https(我将在下面的描述中省略它)
  • 3个docker服务器:localhost:8090, localhost:8091, localhost:8092
  • 在主机中(在我的 windows 机器上)我有 3 个域:loc.localdomain、loc2.localdomain 和 loc3.localdomain 都指向我的 IP 地址
  • 所以我将在我的应用程序中使用 localhost:8090 -> loc.localdomain、localhost:8091 -> loc2.localdomain 和 localhost:8092 -> loc3.localdomain

现在我在loc 上有一个应用程序,它为loc3 子域设置了一些cookie。我看到 cookie 在 chrome 网络响应中设置(或假设设置)。

Set-Cookie: MY_COOKIE=YUMM; domain=loc3.localdomain; 
expires=Fri, 21-Jun-2019 10:48:58 GMT; path=/coolApp/bro

然后在loc 的应用程序中,我有一个按钮,可以在loc2 的另一个应用程序中将用户发送到loc3.localdomain:8092/coolApp/bro/something/moreloc3。因此,此时我应该在loc3 处看到应用请求中的 cookie,但我没有。

Cookie 设置:

FacesContext facesContext = FacesContext.getCurrentInstance();
//facesContext.getExternalContext().addResponseCookie("TEST", "TEST", properties); tried this too 
//then in properties will be the maxAge, path and domain set

Cookie cookie = (Cookie) facesContext.getExternalContext().getRequestCookieMap().get("MY_COOKIE");
if(cookie == null){
     cookie = new Cookie("MY_COOKIE", "YUMMM");
}

cookie.setMaxAge(31536000);
cookie.setPath("/coolApp/bro");
cookie.setDomain("loc3.localdomain"); // I've tried ".localdomain" too

HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
response.addCookie(cookie);

知道这个设置有什么问题吗?

【问题讨论】:

    标签: jsp servlets cookies


    【解决方案1】:

    基于此 (https://curl.haxx.se/rfc/cookie_spec.html) 域应包含至少 2 个点,因此答案是使用 localhost 的其他别名来模拟我的子域。比如:*.example.com

    更改域后,一切都按预期工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-25
      • 2011-07-12
      • 1970-01-01
      • 2012-01-12
      • 1970-01-01
      • 2011-04-10
      • 2015-11-20
      • 2020-01-04
      相关资源
      最近更新 更多