Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting jsessonid cookie to SameSite=Strict attribute in spring boot?

What is the spring-boot configuration to set jsessionId cookie as SameSite=Strict.

JsessionId need to add SameSite=Strict or existing cookie not new cookie generation.Is it support?

like image 297
chakleChincken Avatar asked Oct 29 '18 11:10

chakleChincken


2 Answers

I used Rfc6265CookieProcessor to configure SameSite flag in the spring boot application as a workaround.

build.gradle:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-tomcat'
    ...
}

Config in the main class:

@Bean
public ServletWebServerFactory servletContainer() {
    return new TomcatServletWebServerFactory() {
        @Override
        protected void postProcessContext(Context context) {
            Rfc6265CookieProcessor rfc6265CookieProcessor = new Rfc6265CookieProcessor();
            rfc6265CookieProcessor.setSameSiteCookies("Strict");
            context.setCookieProcessor(rfc6265CookieProcessor);
        }
    };
}
like image 98
Dzmitry Savitski Avatar answered Sep 20 '22 02:09

Dzmitry Savitski


With Undertow 2.1.0.Final and later you can do it like this:

public static final String COOKIE_PATTERN = "JSESSIONID";

@Bean
public UndertowServletWebServerFactory undertowServletWebServerFactory() {
    UndertowServletWebServerFactory undertow = new UndertowServletWebServerFactory();
    
    undertow.addDeploymentInfoCustomizers(
            deploymentInfo -> deploymentInfo.addInitialHandlerChainWrapper(
                    handler -> new SameSiteCookieHandler(handler, CookieSameSiteMode.STRICT.name(), COOKIE_PATTERN)
            ));
    
    return undertow;
}
like image 20
Mihail Avatar answered Sep 20 '22 02:09

Mihail