title: Spring security: successful login after unsuccessful login leads to not found page
body: <p>I have configured the authentication of the website with Spring Security. The issue is that after an unsuccessful login, Spring appends <code>?error</code> to the URL (which is good, because I use it to show the error). But then when the users log in successfully from the second attempt, they see error 404 not found. This, however, doesn't happen if the users successfully log in from the first attempt. How can I tackle this issue?</p>
  
  <p>Here's how login page <code>login.jsp</code> looks like:</p>
  
  <pre><code>&lt;c:choose&gt;&lt;c:when test="${param.error != null}"&gt;
    &lt;div class="error""&gt;Invalid user or password&lt;/div&gt;
  &lt;/c:when&gt;&lt;/c:choose&gt;
  
  &lt;form action="&lt;c:url value="/login"/&gt;" method="POST" novalidate="" accept-charset="UTF-8"&gt;
    &lt;label for="username"&gt;Login&lt;/label&gt;
    &lt;input type="text" name="username" id="username" required /&gt;
    &lt;label for="password"&gt;Password&lt;/label&gt;
    &lt;input type="password" name="password" id="password" required /&gt;
    &lt;input type="hidden"  name="${_csrf.parameterName}"   value="${_csrf.token}"/&gt;
    &lt;input type="submit" name="login" value="Login" id="login-submit" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>The SecurityConfig is configured in the following way:</p>
  
  <pre><code>protected void configure(HttpSecurity http) throws Exception {
      http.authorizeRequests()
              .anyRequest().authenticated();
      http.formLogin().loginPage("/login").permitAll().and().logout().logoutSuccessUrl("/");
      http.logout().logoutUrl("/logout");
  }
  </code></pre>
  
  <p>And below is my controller class:</p>
  
  <pre><code>@Controller
  @RequestMapping("/")
  public class MainController {
      @RequestMapping(value = "login")
      public String login() {
          return "login";
      }
  }
  </code></pre>