Playframework: How to disable session / cookies for certain actions?

For a specific controller action, I want to disable cookies. I tried to delete the cookie map, but this does not work. I need to completely remove all response headers except my own.

Any ideas?

+3
source share
5 answers

I managed to wrap the response with a wrapper response.current.set(new CookieLessResponseWrapper(response.current())). It works fine for me.

Here is the code for the response shell 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);
    }

}
+1
source

I found this solution when working with Google. It does the same subtle thing as you do by deleting the cookie card, but this is done in a method that is annotated with @Finally.

, cookie render(), @Finally.

Alex Jarvis Google, :

/** 
 * 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; 
            } 
        } 

} 

: , "cookieless",

@With (NoCookieFilter.class)

( Play 1.2.5)

+2

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

, Cookie. , .

0

Source: https://habr.com/ru/post/1780014/


All Articles