Spring 3.5 how to add HttpSessionEventPublisher to my boot configuration

前端 未结 4 2248
别那么骄傲
别那么骄傲 2021-02-08 10:44

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


   
     org.springframework.security.web.session.Http         


        
4条回答
  •  佛祖请我去吃肉
    2021-02-08 11:17

    From SpringBootServletInitializer javadoc : A handy opinionated WebApplicationInitializer for applications that only have one Spring servlet, and no more than a single filter (which itself is only enabled when Spring Security is detected). If your application is more complicated consider using one of the other WebApplicationInitializers

    So if you want to generate a war and you want to include a session listener, you should use directly a WebApplicationInitializer. Here is an example from the 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's 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("/");
        }
    
    }
    

    As you have full control on the ServletContext before it is fully initialized, it is easy to add your listener :

        container.addListener(YourListener.class);
    

提交回复
热议问题