如果不存在,只将cookie添加到响应中

时间:2021-11-19 10:40:55

I need to check if a cookie has already been added to the response.

我需要检查cookie是否已添加到响应中。

I have a strange issue where my response is receiving a Set-Cookie header twice.

我有一个奇怪的问题,我的响应是两次收到Set-Cookie标头。

Set-Cookie:foo=bar; Domain=dev.mysite.com; Expires=Fri, 05-May-2062 04:05:43 GMT; Path=/

设置Cookie:富=巴;域= dev.mysite.com; Expires = Fri,05-May-2062 04:05:43 GMT;路径= /

Set-Cookie:foo=bar; Domain=dev.mysite.com; Expires=Fri, 05-May-2062 04:05:43 GMT; Path=/

设置Cookie:富=巴;域= dev.mysite.com; Expires = Fri,05-May-2062 04:05:43 GMT;路径= /

The 2 headers are exactly the same. When I debug the stack is calling this code twice. This double up of this code is due to the web stack I am using, which I can't change.

2个标题完全相同。当我调试堆栈时,调用此代码两次。这个代码的两倍是由于我正在使用的Web堆栈,我无法更改。

I don't care about what cookies were sent in the request, but I don't want this header sent twice.

我不关心请求中发送了什么cookie,但我不希望这个头发送两次。

It seems as though there isn't an easy way to find cookies added via the addHeader method.

似乎没有一种简单的方法可以找到通过addHeader方法添加的cookie。

During a debug, strangely if I check the headers in the response object after the response.addCookie(myCookie); call I don't see the Set-Cookie header.

在调试期间,奇怪的是如果我在response.addCookie(myCookie)之后检查响应对象中的头文件;调用我没有看到Set-Cookie标头。

Is there a way I can only add a cookie to the response if a cookie of the same name is not already being sent in the response?

有没有办法只能在响应中发送相同名称的cookie时才能在响应中添加cookie?

1 个解决方案

#1


0  

Home was correct.

家是对的。

I've created a wrapper around the HttpServletResponseWrapper and store the cookies in the addCookie method.

我已经在HttpServletResponseWrapper周围创建了一个包装器,并将cookie存储在addCookie方法中。

Then I check if the cookies exist in the list before I add another.

然后我在添加另一个之前检查列表中是否存在cookie。

Thanks for your help.

谢谢你的帮助。

#1


0  

Home was correct.

家是对的。

I've created a wrapper around the HttpServletResponseWrapper and store the cookies in the addCookie method.

我已经在HttpServletResponseWrapper周围创建了一个包装器,并将cookie存储在addCookie方法中。

Then I check if the cookies exist in the list before I add another.

然后我在添加另一个之前检查列表中是否存在cookie。

Thanks for your help.

谢谢你的帮助。