Spring 3.5 how to add HttpSessionEventPublisher to my boot configuration

I want to listen to session life cycle events. I read about adding

<listener> <listener-class> org.springframework.security.web.session.HttpSessionEventPublisher </listener-class> </listener> 

in web.xml. But I do not have it. I use a class that extends SpringBootServletInitializer. How can I add this listener?

+8
java spring spring-mvc
source share
4 answers

You can use ServletListenerRegistrationBean :

 @Bean public ServletListenerRegistrationBean<HttpSessionEventPublisher> httpSessionEventPublisher() { return new ServletListenerRegistrationBean<HttpSessionEventPublisher>(new HttpSessionEventPublisher()); } 
+15
source

Adding a listener to a class extending SpringBootSelvletInitializer can be done as shown below.

  @Configuration public class Application extends SpringBootServletInitializer { protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { SpringApplicationBuilder app=application.sources(Application.class, ComponentConfiguration.class); app.listeners(bootstrapContext.commandLineListener()); return app; } 

As a builder class, it has a listener method that it uses to add all listeners that need to be registered. Github link for SpringApplicationBuilder http://goo.gl/EGj6jE

I think this will solve your problem.

Swaraj

0
source

From SpringBootServletInitializer javadoc: A handy, self-confident WebApplicationInitializer for applications that have only one Spring servlet and at most one filter (which by itself is enabled only when Spring Security is detected). If your application is more complex, consider using one of the other WebApplicationInitializers

So, if you want to create a war and want to turn on the session listener, you must use the WebApplicationInitializer directly. Here is an example from javadoc:

 public class MyWebAppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext container) { // Create the 'root' Spring application context AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(AppConfig.class); // Manage the lifecycle of the root application context container.addListener(new ContextLoaderListener(rootContext)); // Create the dispatcher servlet Spring application context AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); dispatcherContext.register(DispatcherConfig.class); // Register and map the dispatcher servlet ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); } } 

Since you have full control over the ServletContext, before it is fully initialized, it's easy to add your listener:

  container.addListener(YourListener.class); 
0
source

Just to provide a link to the official documentation, http://docs.spring.io/spring-session/docs/current-SNAPSHOT/reference/html5/#httpsession-rest

Here, if you look at the HttpSessionListener topic, you will find a clear example of how to do this in both the Java and XML paths.

if your support Redis configuration

 @Configuration @EnableRedisHttpSession public class RedisHttpSessionConfig { @Bean public HttpSessionEventPublisher httpSessionEventPublisher() { return new HttpSessionEventPublisher(); } // ... } 

In the XML configuration, it might look like

 <bean class="org.springframework.security.web.session.HttpSessionEventPublisher"/> 
0
source

All Articles