4

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

有任何想法吗?

4

5 回答 5

3

我在谷歌搜索这个问题时找到了这个解决方案。它与您尝试的操作相同,即删除 cookie 映射,但它是在一个带有注释的方法中完成的@Finally

我相信 cookie 地图是在任何带注释的课程之后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”的控制器,用

@With(NoCookieFilter.class)

(在 Play 1.2.5 中测试)

于 2012-08-03T14:07:30.753 回答
1

I managed to wrap the response with a wrapper with response.current.set(new CookieLessResponseWrapper(response.current())). Works ok for me.

Here is the code for the Response wrapper if anyone is interested.

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);
    }

}
于 2010-12-13T19:58:27.953 回答
0

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

Http.Response.current().reset();
于 2010-12-13T14:55:13.253 回答
0

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

于 2010-12-13T15:50:57.123 回答
0

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

丢弃整个会话

Ok("Bye").withNewSession

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

于 2015-11-05T07:18:00.460 回答