Skip to content

Commit 535050a

Browse files
committed
Automatically register HttpSessionIdListener's with the servlet context
Closes gh-24879
1 parent 48002e9 commit 535050a

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletListenerRegistrationBean.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
2727
import javax.servlet.ServletRequestAttributeListener;
2828
import javax.servlet.ServletRequestListener;
2929
import javax.servlet.http.HttpSessionAttributeListener;
30+
import javax.servlet.http.HttpSessionIdListener;
3031
import javax.servlet.http.HttpSessionListener;
3132

3233
import org.springframework.util.Assert;
@@ -44,6 +45,7 @@
4445
* <li>{@link ServletRequestListener}</li>
4546
* <li>{@link ServletRequestAttributeListener}</li>
4647
* <li>{@link HttpSessionAttributeListener}</li>
48+
* <li>{@link HttpSessionIdListener}</li>
4749
* <li>{@link HttpSessionListener}</li>
4850
* <li>{@link ServletContextListener}</li>
4951
* </ul>
@@ -63,6 +65,7 @@ public class ServletListenerRegistrationBean<T extends EventListener> extends Re
6365
types.add(ServletRequestListener.class);
6466
types.add(ServletRequestAttributeListener.class);
6567
types.add(HttpSessionAttributeListener.class);
68+
types.add(HttpSessionIdListener.class);
6669
types.add(HttpSessionListener.class);
6770
types.add(ServletContextListener.class);
6871
SUPPORTED_TYPES = Collections.unmodifiableSet(types);

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/ServletContextInitializerBeansTests.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import javax.servlet.ServletRequest;
2525
import javax.servlet.ServletResponse;
2626
import javax.servlet.http.HttpServlet;
27+
import javax.servlet.http.HttpSessionIdListener;
2728

2829
import org.junit.jupiter.api.Test;
2930

@@ -70,6 +71,17 @@ void looksForInitializerBeansOfSpecifiedType() {
7071
assertThat(initializerBeans.iterator()).toIterable().hasOnlyElementsOfType(TestServletContextInitializer.class);
7172
}
7273

74+
@Test
75+
void whenAnHttpSessionIdListenerBeanIsDefinedThenARegistrationBeanIsCreatedForIt() {
76+
load(HttpSessionIdListenerConfiguration.class);
77+
ServletContextInitializerBeans initializerBeans = new ServletContextInitializerBeans(
78+
this.context.getBeanFactory());
79+
assertThat(initializerBeans).hasSize(1);
80+
assertThat(initializerBeans).first().isInstanceOf(ServletListenerRegistrationBean.class)
81+
.extracting(ServletListenerRegistrationBean.class::cast)
82+
.extracting(ServletListenerRegistrationBean::getListener).isInstanceOf(HttpSessionIdListener.class);
83+
}
84+
7385
private void load(Class<?>... configuration) {
7486
this.context = new AnnotationConfigApplicationContext(configuration);
7587
}
@@ -109,6 +121,17 @@ OtherTestServletContextInitializer otherTestServletContextInitializer() {
109121

110122
}
111123

124+
@Configuration(proxyBeanMethods = false)
125+
static class HttpSessionIdListenerConfiguration {
126+
127+
@Bean
128+
HttpSessionIdListener httpSessionIdListener() {
129+
return (event, oldId) -> {
130+
};
131+
}
132+
133+
}
134+
112135
static class TestServlet extends HttpServlet implements ServletContextInitializer {
113136

114137
@Override

0 commit comments

Comments
 (0)