File tree Expand file tree Collapse file tree 4 files changed +72
-2
lines changed
java/eu/webeid/example/config
test/java/eu/webeid/example/config Expand file tree Collapse file tree 4 files changed +72
-2
lines changed Original file line number Diff line number Diff line change 2323package eu .webeid .example .config ;
2424
2525import org .apache .tomcat .util .http .Rfc6265CookieProcessor ;
26+ import org .springframework .boot .autoconfigure .condition .ConditionalOnExpression ;
2627import org .springframework .boot .web .embedded .tomcat .TomcatContextCustomizer ;
28+ import org .springframework .boot .web .server .WebServerFactoryCustomizer ;
29+ import org .springframework .boot .web .servlet .server .ConfigurableServletWebServerFactory ;
2730import org .springframework .context .annotation .Bean ;
2831import org .springframework .context .annotation .Configuration ;
2932import 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}
Original file line number Diff line number Diff line change 11spring.profiles.active =dev
2- server.servlet.session.cookie.name =__Host-JSESSIONID
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments