4

I'm trying to set the SameSite attribute of the JSESSIONID cookie in our JHipster gateway, and upon trying to verify in Chrome, there is nothing showing up under the SameSite column for it.

Possibly of note: we're currently not deployed and running the application locally on HTTP (a localhost address). Running in TLS mode also has the same problem, however.

These are two things I've tried in order to get this working:

  1. The second approach from the first answer here How to enable samesite for jsessionid cookie - a filter that is used in JHipster's SecurityConfiguration.java file in the configure() method.

    import java.io.IOException;
    import java.util.Collection;
    
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.http.HttpHeaders;
    
    public class SameSiteFilter implements javax.servlet.Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
    
        }
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            chain.doFilter(request, response);
            addSameSiteCookieAttribute((HttpServletResponse) response); // add SameSite=strict cookie attribute
        }
    
        private void addSameSiteCookieAttribute(HttpServletResponse response) {
            Collection<String> headers = response.getHeaders(HttpHeaders.SET_COOKIE);
            boolean firstHeader = true;
            for (String header : headers) { // there can be multiple Set-Cookie attributes
                if (firstHeader) {
                    response.setHeader(HttpHeaders.SET_COOKIE, String.format("%s; %s", header, "SameSite=Strict"));
                    firstHeader = false;
                    continue;
                }
                response.addHeader(HttpHeaders.SET_COOKIE, String.format("%s; %s", header, "SameSite=Strict"));
            }
        }
    
        @Override
        public void destroy() {
    
        }
    }
    
  2. A CookieSerializer which we got from an internal partner:

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.session.web.http.DefaultCookieSerializer;
    import org.springframework.session.web.http.CookieSerializer;
    
    @Configuration
    class CookieConfiguration {
    
        @Bean
        public static CookieSerializer cookieSerializer() {
            DefaultCookieSerializer serializer = new DefaultCookieSerializer();
            serializer.setSameSite("Lax");
            return serializer;
        }
    }
    

Neither of these work. Is there something else we can try for this particular flavor of Spring?

Community
  • 1
  • 1

1 Answers1

0

In case you are using Tomcat (i.e. not WebFlux), the following configuration will add SameSite=strict to all cookies, including JSESSIONID:

@Configuration
public class SameSiteCookieConfiguration implements WebMvcConfigurer {

    @Bean
    public TomcatContextCustomizer configureSameSiteCookies() {
        return context -> {
            final Rfc6265CookieProcessor cookieProcessor = new Rfc6265CookieProcessor();
            cookieProcessor.setSameSiteCookies("strict");
            context.setCookieProcessor(cookieProcessor);
        };
    }

}
mrts
  • 16,697
  • 8
  • 89
  • 72