简体   繁体   中英

HttpServletResponse, addCookie() not behaving as expected

I am working on a 3rd party integration with my application. 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. 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. Once I receive the cookie info, I create a new javax.servlet.http Cookie and pass this info:

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

Then, i store it in the (HttpServletResponse) response, like so: response.addCookie(cookie);

When I trigger the login event from my application, I see this cookie appears under Cookies in developer tools. But, as soon as the page refreshes, or I go to another page (within my application) the cookie is gone. 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? 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.

Well, after a lot of searching I found the cause; the default path of the cookie. As the cookie is created on domain.com/page1, this means it will only be valid for page1 and the following subdirectories. It will not be available for the whole domain.com. Hopefully this will help others with similar issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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