From 535050ad483990f36c73e037a900b3e848e75a82 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 19 Jan 2021 11:28:34 +0000 Subject: [PATCH] Automatically register HttpSessionIdListener's with the servlet context Closes gh-24879 --- .../ServletListenerRegistrationBean.java | 5 +++- .../ServletContextInitializerBeansTests.java | 25 ++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletListenerRegistrationBean.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletListenerRegistrationBean.java index ebee46669d..f8f7896334 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletListenerRegistrationBean.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletListenerRegistrationBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,6 +27,7 @@ import javax.servlet.ServletContextListener; import javax.servlet.ServletRequestAttributeListener; import javax.servlet.ServletRequestListener; import javax.servlet.http.HttpSessionAttributeListener; +import javax.servlet.http.HttpSessionIdListener; import javax.servlet.http.HttpSessionListener; import org.springframework.util.Assert; @@ -44,6 +45,7 @@ import org.springframework.util.ClassUtils; *
  • {@link ServletRequestListener}
  • *
  • {@link ServletRequestAttributeListener}
  • *
  • {@link HttpSessionAttributeListener}
  • + *
  • {@link HttpSessionIdListener}
  • *
  • {@link HttpSessionListener}
  • *
  • {@link ServletContextListener}
  • * @@ -63,6 +65,7 @@ public class ServletListenerRegistrationBean extends Re types.add(ServletRequestListener.class); types.add(ServletRequestAttributeListener.class); types.add(HttpSessionAttributeListener.class); + types.add(HttpSessionIdListener.class); types.add(HttpSessionListener.class); types.add(ServletContextListener.class); SUPPORTED_TYPES = Collections.unmodifiableSet(types); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/ServletContextInitializerBeansTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/ServletContextInitializerBeansTests.java index a1de503506..ef5c1226d6 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/ServletContextInitializerBeansTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/ServletContextInitializerBeansTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,6 +24,7 @@ import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpSessionIdListener; import org.junit.jupiter.api.Test; @@ -70,6 +71,17 @@ class ServletContextInitializerBeansTests { assertThat(initializerBeans.iterator()).toIterable().hasOnlyElementsOfType(TestServletContextInitializer.class); } + @Test + void whenAnHttpSessionIdListenerBeanIsDefinedThenARegistrationBeanIsCreatedForIt() { + load(HttpSessionIdListenerConfiguration.class); + ServletContextInitializerBeans initializerBeans = new ServletContextInitializerBeans( + this.context.getBeanFactory()); + assertThat(initializerBeans).hasSize(1); + assertThat(initializerBeans).first().isInstanceOf(ServletListenerRegistrationBean.class) + .extracting(ServletListenerRegistrationBean.class::cast) + .extracting(ServletListenerRegistrationBean::getListener).isInstanceOf(HttpSessionIdListener.class); + } + private void load(Class... configuration) { this.context = new AnnotationConfigApplicationContext(configuration); } @@ -109,6 +121,17 @@ class ServletContextInitializerBeansTests { } + @Configuration(proxyBeanMethods = false) + static class HttpSessionIdListenerConfiguration { + + @Bean + HttpSessionIdListener httpSessionIdListener() { + return (event, oldId) -> { + }; + } + + } + static class TestServlet extends HttpServlet implements ServletContextInitializer { @Override