java - Playframework:如何禁用特定操作的 session /cookie?

标签 java playframework

对于特定的 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/

相关文章:

java - 为什么 JavaFX 中的最终类 WebView 包含 protected 方法 getChildren()?

javascript - 悬停时交换背景

java - 在 Play Framework 中的@Entity Model 类上实现 equals() 和 hashCode() 可以吗?

scala - 从2.2迁移到2.3 : No implicit view available from Any

java - [处理/Java]可见性/分层问题

java - EntityMetadata 数据包问题

java - 是否有助手可以知道 Hibernate 是否已加载属性?

java - 用于 WebSphere Portal 开发的轻量级门户服务器?

playframework - 如何安全关闭Play Framework应用

scala - UUID 路径可绑定(bind) - Play Framework