title: Spring Security return to login page AFTER a successful login
body: <p>My Sring application have the following Spring Security configuration:</p>
  
  <pre><code>@Configuration
  @ComponentScan(value="org.webapp")
  @EnableWebSecurity
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      private DataSource restDataSource;
  
      @Autowired
      public void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth
              .jdbcAuthentication()
              .dataSource(restDataSource)
              .usersByUsernameQuery(getUserQuery())
              .authoritiesByUsernameQuery(getAuthoritiesQuery());
      }
  
      protected void configure(HttpSecurity http) throws Exception {
          http
              .csrf()
                  .disable()
              .authorizeRequests()
                  .antMatchers("/css/**", "/fonts/**", "/image/**", "/js/**").permitAll()
                  .anyRequest().authenticated()
                  .and()
              .formLogin()
                  .loginPage("/spring/index").permitAll()
                  .loginProcessingUrl("/spring/login").permitAll()
                  .usernameParameter("login")
                  .passwordParameter("senha")
                  .successHandler(new CustomAuthenticationSuccessHandler())
                  .failureHandler(new CustomAuthenticationFailureHandler())
                  .and()
              .logout()
                  .logoutUrl("/spring/logout")
                  .logoutSuccessUrl("/spring/index").permitAll();
      }
  
      private String getUserQuery() {
          return "SELECT login as username, senha as password "
                  + "FROM usuario "
                  + "WHERE login = ?";
      }
  
      private String getAuthoritiesQuery() {
          return "SELECT DISTINCT usuario.login as username, autorizacao.descricao as authority "
                  + "FROM usuario, autorizacao_usuario, autorizacao "
                  + "WHERE usuario.id = autorizacao_usuario.fk_usuario "
                  + "AND autorizacao.id = autorizacao_usuario.fk_autorizacao "
                  + "AND usuario.login = ? ";
      }
  
  }
  </code></pre>
  
  <p>But I have one problem with that: After I inform the login credencials, the system return to login page instead the target page (/spring/home). I look in the stacktrack and no error is displayed.</p>
  
  <p>Someone can see what's wrong?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>CustomAuthenticationSuccessHandler</p>
  
  <pre><code>public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
  
      @Override
      public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication auth) throws IOException, ServletException {
          System.out.println("CustomAuthenticationSuccessHandler");
          HttpSession session = request.getSession();
          SavedRequest savedReq = (SavedRequest) session.getAttribute(WebAttributes.ACCESS_DENIED_403);
          if (savedReq == null) {
              response.sendRedirect(request.getContextPath() + "/spring/home");
          }
          else {
              response.sendRedirect(savedReq.getRedirectUrl());
          }
      }
  
  }
  </code></pre>
  
  <p>CustomAuthenticationFailureHandler</p>
  
  <pre><code>public class CustomAuthenticationFailureHandler implements AuthenticationFailureHandler {
  
      @Override
      public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException auth) throws IOException, ServletException {
          System.out.println("CustomAuthenticationFailureHandler");
          response.sendRedirect(request.getContextPath() + "/spring/erro-login");
      }
  
  }
  </code></pre>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>the full source code of project can be seen here: <a href="https://github.com/klebermo/webapp1" rel="nofollow">https://github.com/klebermo/webapp1</a></p>