Skip to content

Commit

Permalink
Automatically register HttpSessionIdListener's with the servlet context
Browse files Browse the repository at this point in the history
Closes gh-24879
  • Loading branch information
wilkinsona committed Jan 19, 2021
1 parent 48002e9 commit 535050a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -27,6 +27,7 @@
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;
Expand All @@ -44,6 +45,7 @@
* <li>{@link ServletRequestListener}</li>
* <li>{@link ServletRequestAttributeListener}</li>
* <li>{@link HttpSessionAttributeListener}</li>
* <li>{@link HttpSessionIdListener}</li>
* <li>{@link HttpSessionListener}</li>
* <li>{@link ServletContextListener}</li>
* </ul>
Expand All @@ -63,6 +65,7 @@ public class ServletListenerRegistrationBean<T extends EventListener> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -24,6 +24,7 @@
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSessionIdListener;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -70,6 +71,17 @@ void looksForInitializerBeansOfSpecifiedType() {
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);
}
Expand Down Expand Up @@ -109,6 +121,17 @@ OtherTestServletContextInitializer otherTestServletContextInitializer() {

}

@Configuration(proxyBeanMethods = false)
static class HttpSessionIdListenerConfiguration {

@Bean
HttpSessionIdListener httpSessionIdListener() {
return (event, oldId) -> {
};
}

}

static class TestServlet extends HttpServlet implements ServletContextInitializer {

@Override
Expand Down

0 comments on commit 535050a

Please sign in to comment.