title: Spring Security Session Timeout - Java
body: <p>My Spring security works well but after some afk time I start getting tons of exceptions when the user goes to the pages.</p>
  
  <p>I noticed that <code>session and principal is null</code> that's why I get error 500.</p>
  
  <p>How do I redirect the user that to the login again? </p>
  
  <p>Or I can simply remove the session timeout (i dont really need it)</p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  @EnableGlobalMethodSecurity(securedEnabled=true)
  @ComponentScan("pt.impactzero.atp")
  public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      private MyUserDetailsService myUserDetailsService;
      @Autowired
      private AuthenticationSuccessHandler authenticationSuccessHandler;
      @Autowired
      private LogoutSuccessHandler logoutSuccessHandler;
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http.authorizeRequests().antMatchers("/").hasAnyRole("Administrator" , "Member")
                  .and()
                  .formLogin()
                      .loginPage("/login").permitAll()
                      .defaultSuccessUrl("/dashboard",true)
                      .failureUrl("/login?error")
                      .successHandler(authenticationSuccessHandler)
                  .and()
                  .logout()
                      .logoutUrl("/logout").permitAll()
                      .logoutSuccessUrl("/login")
                      .logoutSuccessHandler(logoutSuccessHandler)
                  .and()
                  .csrf().disable();
      }
  
      @Override
      public void configure(WebSecurity web) {
          web.httpFirewall(allowUrlEncodedSlashHttpFirewall());
      }
  
      @Autowired
      public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
          auth.userDetailsService(myUserDetailsService).passwordEncoder(passwordEncoder());
      }
      @Bean
      public PasswordEncoder passwordEncoder() {
          BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
          return passwordEncoder;
      }
  
      @Bean
      public HttpFirewall allowUrlEncodedSlashHttpFirewall() {
          DefaultHttpFirewall firewall = new DefaultHttpFirewall();
          firewall.setAllowUrlEncodedSlash(true);
          return firewall;
      }
  
      @Bean
      public CommonsMultipartResolver filterMultipartResolver(){
          return new CommonsMultipartResolver();
      }
  
      //Online users
      @Bean
      public ActiveUsers activeUsers(){
          return new ActiveUsers();
      }
  }
  </code></pre>