Add secure flag to JSESSIONID cookie in spring automatically

后端 未结 5 810
广开言路
广开言路 2020-12-11 02:08

I have a tomcat application server that is behind a nginx. SSL terminates on the nginx. The Spring web-mvc application that is deployed on the tomcat should set the secure f

5条回答
  •  时光取名叫无心
    2020-12-11 02:42

    Add another option

    You can use a ServletContextInitializer to set secure cookie and http only flag

    @Bean
    public ServletContextInitializer servletContextInitializer() {
        return new ServletContextInitializer() {
            @Override
            public void onStartup(ServletContext servletContext) throws ServletException {
                servletContext.setSessionTrackingModes(Collections.singleton(SessionTrackingMode.COOKIE));
                SessionCookieConfig sessionCookieConfig = servletContext.getSessionCookieConfig();
                sessionCookieConfig.setHttpOnly(true);
                sessionCookieConfig.setSecure(true);
            }
        };
    }
    

提交回复
热议问题