如何在JSP中为子域正确设置cookie? [英] How to correct set the cookies for a subdomain in JSP?

查看:88
本文介绍了如何在JSP中为子域正确设置cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下设置:

  • 所有请求均为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网络响应中设置的(或应该设置的).

Now I have an app on loc that sets some cookies for loc3 subdomain. I see that the cookies are set (or suppose to be set) in chrome network response.

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,但我没有看到.

Then in app at loc I have a button that sends the user in another app at loc2 that redirects the user to loc3 at loc3.localdomain:8092/coolApp/bro/something/more. So at this point in time I should see the cookie(s) in the app request at loc3, but I don't.

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);

您知道此设置有什么问题吗?

Any idea what is wrong in this setup?

推荐答案

基于此( https ://curl.haxx.se/rfc/cookie_spec.html )域中至少应包含2个点,因此答案是对本地主机使用其他别名来模拟我的子域.类似于:*.example.com

based on this (https://curl.haxx.se/rfc/cookie_spec.html) the domain should contain at least 2 dots so the answer is to use other alias for localhost to simulate my subdomains. Something like: *.example.com

更改域后,所有功能均按预期工作.

After changing the domain all worked as anticipated.

这篇关于如何在JSP中为子域正确设置cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆