Playframework:如何在特定操作上禁用会话/ cookie?

时间:2010-12-13 14:01:40

标签: java playframework

对于特定的控制器操作,我想关闭cookie。我试图删除cookie Map,但这似乎不起作用。我需要完全删除除我自己以外的所有响应头。

有什么想法吗?

5 个答案:

答案 0 :(得分:2)

我在谷歌搜索此问题时找到了this解决方案。它会尝试使用相同的thinng,删除cookie映射,但它是在使用@Finally注释的方法中完成的。

我认为Cookie地图在render()之后但在任何@Finally注释类之前填充。

在Google网上论坛上向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”的控制器,用

注释它
  

@With(NoCookieFilter.class)

(在Play 1.2.5中测试)

答案 1 :(得分:1)

我设法使用response.current.set(new CookieLessResponseWrapper(response.current()))的包装器包装响应。适合我。

如果有人感兴趣,这是Response包装器的代码。

package helpers;

import play.mvc.Http.Response;

public class CookieLessResponseWrapper extends Response {
    private Response wrappedResponse;

    public CookieLessResponseWrapper(Response response) {
        this.wrappedResponse = response;
    }

    @Override
    public void accessControl(String allowOrigin, boolean allowCredentials) {
        wrappedResponse.accessControl(allowOrigin, allowCredentials);
    }

    @Override
    public void accessControl(String allowOrigin, String allowMethods,
            boolean allowCredentials) {
        wrappedResponse.accessControl(allowOrigin, allowMethods, allowCredentials);
    }

    @Override
    public void accessControl(String allowOrigin) {
        wrappedResponse.accessControl(allowOrigin);
    }

    @Override
    public void cacheFor(String etag, String duration, long lastModified) {
        wrappedResponse.cacheFor(etag, duration, lastModified);
    }

    @Override
    public void cacheFor(String duration) {
        wrappedResponse.cacheFor(duration);
    }

    @Override
    public String getHeader(String name) {
        return wrappedResponse.getHeader(name);
    }

    @Override
    public void print(Object o) {
        wrappedResponse.print(o);
    }

    @Override
    public void removeCookie(String name) {
        wrappedResponse.removeCookie(name);
    }

    @Override
    public void reset() {
        wrappedResponse.reset();
    }

    @Override
    public void setContentTypeIfNotSet(String contentType) {
        wrappedResponse.setContentTypeIfNotSet(contentType);
    }

    @Override
    public void setCookie(String name, String value, String domain,
            String path, Integer maxAge, boolean secure, boolean httpOnly) {
    }

    @Override
    public void setCookie(String name, String value, String domain,
            String path, Integer maxAge, boolean secure) {
    }

    @Override
    public void setCookie(String name, String value, String duration) {
    }

    @Override
    public void setCookie(String name, String value) {
    }

    @Override
    public void setHeader(String name, String value) {
        wrappedResponse.setHeader(name, value);
    }

}

答案 2 :(得分:0)

您应该能够在以下操作中清除所有标题:

Http.Response.current().reset();

答案 3 :(得分:0)

请记住,Session是Cookie。我不相信你可以删除它。

答案 4 :(得分:0)

如何使用丢弃每个请求的整个会话作为解决方法?

放弃整个会话

Ok("Bye").withNewSession

https://www.playframework.com/documentation/2.3.x/ScalaSessionFlash#Discarding-the-whole-session