Skip to content

Commit 64dd0cf

Browse files
svenzikmrts
authored andcommitted
Cookie name prefix __Host- is added only when https is used
WE2-967 Signed-off-by: Sven Mitt <[email protected]>
1 parent 3fb6b71 commit 64dd0cf

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

example/src/main/java/eu/webeid/example/config/SameSiteCookieConfiguration.java renamed to example/src/main/java/eu/webeid/example/config/CookieConfiguration.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@
2323
package eu.webeid.example.config;
2424

2525
import org.apache.tomcat.util.http.Rfc6265CookieProcessor;
26+
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
2627
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
28+
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
29+
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
2730
import org.springframework.context.annotation.Bean;
2831
import org.springframework.context.annotation.Configuration;
2932
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
3033

3134
@Configuration
32-
public class SameSiteCookieConfiguration implements WebMvcConfigurer {
35+
public class CookieConfiguration implements WebMvcConfigurer {
3336

3437
@Bean
3538
public TomcatContextCustomizer configureSameSiteCookies() {
@@ -39,4 +42,16 @@ public TomcatContextCustomizer configureSameSiteCookies() {
3942
context.setCookieProcessor(cookieProcessor);
4043
};
4144
}
45+
46+
@Bean
47+
@ConditionalOnExpression("'${web-eid-auth-token.validation.local-origin}'.startsWith('http:')")
48+
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> httpSessionCookieCustomizer() {
49+
return factory -> factory.addInitializers(servletContext -> servletContext.getSessionCookieConfig().setName("JSESSIONID"));
50+
}
51+
52+
@Bean
53+
@ConditionalOnExpression("'${web-eid-auth-token.validation.local-origin}'.startsWith('https:')")
54+
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> httpsSessionCookieCustomizer() {
55+
return factory -> factory.addInitializers(servletContext -> servletContext.getSessionCookieConfig().setName("__Host-JSESSIONID"));
56+
}
4257
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
spring.profiles.active=dev
2-
server.servlet.session.cookie.name=__Host-JSESSIONID
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package eu.webeid.example.config;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import jakarta.servlet.ServletContext;
6+
import jakarta.servlet.SessionCookieConfig;
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
11+
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
12+
import org.springframework.test.context.TestPropertySource;
13+
14+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
15+
@TestPropertySource(properties = {"web-eid-auth-token.validation.local-origin=http://localhost"})
16+
class CookieHttpTest {
17+
18+
@Autowired
19+
private ServletWebServerApplicationContext context;
20+
21+
@Test
22+
void whenLocalOriginStartsWithHttp_thenCookeDoesNotHaveHostPrefix() {
23+
ServletContext servletContext = context.getServletContext();
24+
SessionCookieConfig cookieConfig = servletContext.getSessionCookieConfig();
25+
assertThat(cookieConfig.getName()).isEqualTo("JSESSIONID");
26+
}
27+
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package eu.webeid.example.config;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import jakarta.servlet.ServletContext;
6+
import jakarta.servlet.SessionCookieConfig;
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
11+
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
12+
import org.springframework.test.context.TestPropertySource;
13+
14+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
15+
@TestPropertySource(properties = {"web-eid-auth-token.validation.local-origin=https://localhost"})
16+
class CookieHttpsTest {
17+
18+
@Autowired
19+
private ServletWebServerApplicationContext context;
20+
21+
@Test
22+
void whenLocalOriginStartsWithHttp_thenCookeDoesNotHaveHostPrefix() {
23+
ServletContext servletContext = context.getServletContext();
24+
SessionCookieConfig cookieConfig = servletContext.getSessionCookieConfig();
25+
assertThat(cookieConfig.getName()).isEqualTo("__Host-JSESSIONID");
26+
}
27+
28+
}

0 commit comments

Comments
 (0)