Spring boot configure custom jsessionid for embedded server

后端 未结 4 1551
隐瞒了意图╮
隐瞒了意图╮ 2020-12-13 20:04

I want to configure my servlet context, such as setting a custom jsessionId key (see Changing cookie JSESSIONID name)

I believe I can use the SpringBootServlet

4条回答
  •  生来不讨喜
    2020-12-13 20:36

    Declare a ServletContextInitializer bean in your application's configuration:

    @Bean
    public ServletContextInitializer servletContextInitializer() {
        return new ServletContextInitializer() {
    
            @Override
            public void onStartup(ServletContext servletContext) throws ServletException {
                servletContext.getSessionCookieConfig().setName("yourCookieName");
            }
        };
    
    }
    

    Alternatively, your application class itself can implement ServletContextInitializer:

    @Configuration
    @EnableAutoConfiguration
    @ComponentScan
    public class Application implements ServletContextInitializer {
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(Application.class, args);
        }
    
        @Override
        public void onStartup(ServletContext servletContext) throws ServletException {
            servletContext.getSessionCookieConfig().setName("yourCookieName");
        }
    
    }
    

提交回复
热议问题