设置JSESSIONID cookie的SameSite属性-JHipster应用程序

时间:2020-08-03 21:29:09

标签: java spring-boot cookies spring-security jhipster

我正在尝试在JHipster网关中设置JSESSIONID cookie的SameSite属性,并尝试在Chrome中进行验证时,在SameSite列下没有任何显示。

可能值得注意的:我们目前尚未在HTTP(本地主机地址)上本地部署和运行应用程序。但是,以TLS模式运行也有同样的问题。

为了使此功能正常运行,我尝试了两件事:

  1. 这里第一个答案的第二种方法是How to enable samesite for jsessionid cookie-在configure()方法中JHipster的SecurityConfiguration.java文件中使用的过滤器。

    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. 我们从内部合作伙伴处获得的CookieSerializer

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

这些都不起作用。我们还有其他可以尝试这种特殊风味的春天吗?

1 个答案:

答案 0 :(得分:0)

如果您使用的是 Tomcat(即不是 WebFlux),以下配置会将 SameSite=strict 添加到所有 cookie,包括 JSESSIONID

@Configuration
public class SameSiteCookieConfiguration implements WebMvcConfigurer {

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

}