简体   繁体   English

如何在 Play Framework 2.5.x /Java 中删除(会话)cookie

[英]How to remove a (session) cookie in Play Framework 2.5.x /Java

I am using Play 2.5.3.我正在使用 Play 2.5.3。 I want to remove a session-cookie when user did some specific actions.当用户执行某些特定操作时,我想删除会话 cookie。

My cookie is like this.我的饼干是这样的。

// Cookie: name, value, maximum age, path, domain, secure, http only Cookie("name", somestring, null, "/", somedomain, false, false) // Cookie: name, value, maximum age, path, domain, secure, http only Cookie("name", somestring, null, "/", somedomain, false, false)

I have already tried what Play documents showed.我已经尝试过 Play 文档显示的内容。

request().discardCookie(name, path, domain)

Turns out it doesn't work.事实证明它不起作用。 What should I try next.我接下来应该尝试什么。 Any help will be appreciated.任何帮助将不胜感激。

你需要使用

response().discardCookie("name");

in scala you need to pass DiscardingCookie instance as follows: response.discardingCookies(DiscardingCookie("cookie_name"))在 Scala 中,您需要按如下方式传递DiscardingCookie实例: response.discardingCookies(DiscardingCookie("cookie_name"))

see maybe similar class is available in Java api.看看 Java api 中可能有类似的类。

As of play 2.8, Http.Context got discontinued.从 play 2.8 开始,Http.Context 已停止使用。 You can now instead use您现在可以改为使用

return ok().discardingCookies("COOKIE_NAME");

So basically any changes to the response needs to be applied to the result object your function returns directly, instead of interacting with the old response() method.所以基本上对响应的任何更改都需要应用于您的函数直接返回的结果对象,而不是与旧的 response() 方法交互。

我认为你应该使用

response.removeCookie(keyName);
Cookie c = new Cookie("name", "", 0, somepath, somedomain, false, false);
return ok().withCookies(c);

It turns out, ↑ works out.事实证明,↑ 有效。

Try to set the value to "", and the maxAge to 0. Set everything else what they were .尝试将值设置为“”,并将 maxAge 设置为 0。设置其他所有内容

The new cookie should replace the old one.新的 cookie 应该取代旧的。 And vanished since the maxAge is 0.并且因为 maxAge 为 0 而消失了。

Hope this could help someone.希望这可以帮助某人。 Lol哈哈

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

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