Set tracking mode to cookie to remove appended session id, without using web.xml

前端 未结 4 1513
失恋的感觉
失恋的感觉 2020-12-29 16:43

I am setting up a completely java based spring app with no xml config :

public class WebApp extends AbstractAnnotationConfigDispatcherServletInitializer {
           


        
相关标签:
4条回答
  • 2020-12-29 17:04

    you can do it as in below

    public class WebConfig implements WebApplicationInitializer {
    
        @Override
        public void onStartup(ServletContext servletContext)
                throws ServletException {
            HashSet<SessionTrackingMode> set = new HashSet<SessionTrackingMode>();
            set.add(SessionTrackingMode.COOKIE);
            servletContext.setSessionTrackingModes(set);
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-29 17:04

    Since 3.2.0.RC1 this is available in the AbstractSecurityWebApplicationInitializer like so:

    public class WebSecutityInit extends AbstractSecurityWebApplicationInitializer {
    
        @Override
        protected Set<SessionTrackingMode> getSessionTrackingModes() {
            return EnumSet.of(SessionTrackingMode.SSL);
        }
    }
    
    0 讨论(0)
  • 2020-12-29 17:12

    In a Spring Boot app, you can configure the mode using the application property server.session.tracking-modes.

    In your application.properties add:

    server.session.tracking-modes=cookie
    

    Or if you use application.yml:

    server:
      session:
        tracking-modes: 'cookie'
    

    The Spring Boot autoconfiguration internally uses the same call to servletContext.setSessionTrackingModes which Bassem recommended in his answer.

    0 讨论(0)
  • 2020-12-29 17:13

    Another solution, that works for me, has been the code below inside the SecurityConfig class.

    @Override
    protected void configure(HttpSecurity http) throws Exception {    
     http.httpBasic()
      .and()
      .sessionManagement()
      .sessionCreationPolicy(SessionCreationPolicy.STATELESS) //No sessionId eppended  
      ...
    }
    
    0 讨论(0)
提交回复
热议问题