对于特定的 Controller 操作,我想关闭 cookie。我试图删除 cookie 映射,但这似乎不起作用。我需要完全删除除我自己的响应 header 之外的所有响应 header 。
有什么想法吗?
最佳答案
我找到了 this谷歌搜索此问题时的解决方案。它做的事情与您尝试的一样,删除 cookies 映射,但它是在一个用 @Finally
注释的方法中完成的。
我相信 cookies 映射是在 render()
之后但在任何 @Finally
注释类之前填充的。
致谢 Google Groups 上的 Alex Jarvis,复制代码以供引用:
/**
* Removes cookies from all responses.
*
* This is because cookies are not required in stateless webservice and
* we don't want to send any unnecessary information to the client.
*
* @author Alex Jarvis
*/
public class NoCookieFilter extends Controller {
/** An empty cookie map to replace any cookies in the response. */
private static final Map<String, Http.Cookie> cookies = new HashMap<String, Http.Cookie>(0);
/**
* When the configuration property 'cookies.enabled' equals false,
* this Finally filter will replace the cookies in the response with an empty Map.
*/
@Finally
protected static void removeCookies() {
boolean cookiesEnabled = Boolean.parseBoolean(Play.configuration.getProperty("cookies.enabled"));
if (!cookiesEnabled) {
response.cookies = cookies;
}
}
}
用法:对于任何你想“无cookie”的 Controller ,用
注释@With(NoCookieFilter.class)
(在 Play 1.2.5 中测试)
关于java - Playframework:如何禁用特定操作的 session /cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4429551/