简体   繁体   English

HttpServletResponse,addCookie() 未按预期运行

[英]HttpServletResponse, addCookie() not behaving as expected

I am working on a 3rd party integration with my application.我正在与我的应用程序进行第 3 方集成。 I need to set a server side cookie in the user's browser, so that the third party's SDK can recognise this session as valid and allow the user to interact with it.我需要在用户的浏览器中设置一个服务器端cookie,以便第三方的SDK可以识别这个session为有效并允许用户与之交互。 The actual cookie name and value I get upon logging in and calling the (3d party's) service and now I have to save it in the browser.我在登录并调用(3d 方)服务时获得的实际 cookie 名称和值,现在我必须将其保存在浏览器中。 Once I receive the cookie info, I create a new javax.servlet.http Cookie and pass this info:收到 cookie 信息后,我创建一个新的 javax.servlet.http Cookie 并传递此信息:

Cookie cookie = new Cookie("cookieName", "cookieValue");

Then, i store it in the (HttpServletResponse) response, like so: response.addCookie(cookie);然后,我将它存储在 (HttpServletResponse) 响应中,如下所示: response.addCookie(cookie);

When I trigger the login event from my application, I see this cookie appears under Cookies in developer tools.当我从我的应用程序触发登录事件时,我看到此 cookie 出现在开发人员工具中的Cookies下。 But, as soon as the page refreshes, or I go to another page (within my application) the cookie is gone.但是,一旦页面刷新,或者我 go 到另一个页面(在我的应用程序中),cookie 就消失了。 Why, is the question.为什么,是个问题。 I want it to stay until the user logs out.我希望它一直存在,直到用户注销。

I also don't know if what I'm doing is even possible.我也不知道我正在做的事情是否可能。 Sharing cookies across domains?跨域共享 cookies? Based on the third party's instructions, as soon as their SDK is loaded in my app, it will start "searching" for this specific cookie name and based on that synchronize states.根据第三方的说明,一旦他们的 SDK 加载到我的应用程序中,它将开始“搜索”这个特定的 cookie 名称并基于该同步状态。

Well, after a lot of searching I found the cause;好吧,经过大量搜索,我找到了原因; the default path of the cookie. cookie 的默认路径。 As the cookie is created on domain.com/page1, this means it will only be valid for page1 and the following subdirectories.由于 cookie 是在 domain.com/page1 上创建的,这意味着它只对 page1 和以下子目录有效。 It will not be available for the whole domain.com.它不适用于整个域。com。 Hopefully this will help others with similar issues.希望这将帮助其他有类似问题的人。

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

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