title: Spring Security - Cannot login with custom login page
body: <p>I am learning spring and I am currently learning traditional user / password authentication with Spring Security.</p>
  <p>At the moment, if I let Spring use a default page to login, I can do it without any problems, but if I try to set a custom page for login, even typing the correct data (something I confirmed using the Inspect tool) , I am not redirected to the main page, but to the same login page, but instead of &quot;localhost: 1812/login&quot; the page changes to &quot;localhost: 1812/login?error&quot; and I am unable to login. Basically, my WebSecurityConfig class is:</p>
  <pre><code>@Configuration
  @EnableWebSecurity
  public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
      
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          System.out.println(&quot;ccc&quot;);
          http
          .authorizeRequests()
          .anyRequest().authenticated()
          .and()
          //.formLogin().permitAll()
          .formLogin().loginPage(&quot;/login&quot;).permitAll()
          .defaultSuccessUrl(&quot;/home&quot;, true);
      
          System.out.println(&quot;ddd&quot;);
      
      }
      
      @Bean
      @Override
      protected UserDetailsService userDetailsService() {  
          System.out.println(&quot;AAA&quot;);
          UserDetails user = User.withDefaultPasswordEncoder()
                  .username(&quot;root&quot;)
                  .password(&quot;root&quot;)
                  .roles(&quot;ADM&quot;)
                  .build();
          System.out.println(&quot;bbb&quot;);
          return new InMemoryUserDetailsManager(user);
      }
  }
  </code></pre>
  <p>If I use &quot;.formLogin().permitAll()&quot; instead of &quot;.formLogin().loginPage(&quot;/login&quot;).permitAll()&quot;, I can successfully log in, but Spring will load a standard login page. I wanted to understand what could be wrong. On the Inspect tool I received the HTTP status code 302 when sending the login on the custom page, but I don't know what that means ... Here I leave the code for my LoginController and login.html, and if someone helps me I would be grateful too much because I tried hard and I couldn't solve the problem ...</p>
  <p>LoginController</p>
  <pre><code>@Controller
  public class LoginController {
      
      @GetMapping
      @RequestMapping(&quot;/login&quot;)
      public String login() {
          return &quot;login&quot;;
      }
      
  }
  </code></pre>
  <p>login.html</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head th:replace=&quot;~{base :: head}&quot;&gt;&lt;/head&gt;
      &lt;body&gt;
          &lt;div th:replace=&quot;~{base :: logo}&quot;&gt;&lt;/div&gt;
          &lt;div class=&quot;container&quot;&gt;
              &lt;div th:replace=&quot;~{base :: titulo('Login')}&quot;&gt;&lt;/div&gt;
              &lt;div class=&quot;card mb-3&quot;&gt;
                  &lt;form th:action=&quot;@{/login}&quot; method=&quot;POST&quot; class=&quot;card-body&quot;&gt;
                      &lt;div class=&quot;form-group mb-3&quot;&gt;
                          &lt;div class=&quot;mb-2&quot;&gt;
                              &lt;label for=&quot;username&quot; class=&quot;sr-only&quot;&gt;Usuário&lt;/label&gt;
                              &lt;input type=&quot;text&quot; name=&quot;username&quot; class=&quot;form-control&quot; placeholder=&quot;usuário&quot;/&gt;
                          &lt;/div&gt;
                          &lt;div class=&quot;mb-2&quot;&gt;
                              &lt;label for=&quot;password&quot; class=&quot;sr-only&quot;&gt;Senha&lt;/label&gt;
                              &lt;input type=&quot;password&quot; name=&quot;username&quot; class=&quot;form-control&quot; placeholder=&quot;senha&quot;/&gt;
                          &lt;/div&gt;
                          &lt;button class=&quot;btn btn-primary&quot; type=&quot;submit&quot;&gt;Login&lt;/button&gt;
                      &lt;/div&gt;                  
                  &lt;/form&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>Edit:
  With help of the user @Random Guy and some searching in the Google, I modifed my configure method to this:</p>
  <pre><code>protected void configure(HttpSecurity http) throws Exception {
          
          http
          .antMatcher(&quot;/**&quot;)
          .authorizeRequests()
            .antMatchers(&quot;/&quot;, &quot;/login**&quot;,&quot;/callback/&quot;, &quot;/webjars/**&quot;, &quot;/css**&quot;, &quot;/error**&quot;)
            .permitAll()
          .anyRequest()
            .authenticated()
           .and()
           .formLogin().loginPage(&quot;/login&quot;).loginProcessingUrl(&quot;/login&quot;)
           .defaultSuccessUrl(&quot;/home&quot;).failureUrl(&quot;/login?message=error&quot;).usernameParameter(&quot;username&quot;)
           .passwordParameter(&quot;password&quot;).and().logout().logoutUrl(&quot;/perform_logout&quot;)
           .logoutSuccessUrl(&quot;/login?message=logout&quot;);
  
  }
  </code></pre>
  <p>But while still login not working, when I input the username and password, i'm redirectionated to same login page but with the url now is &quot;http://localhost:1812/login?message=error&quot;, like in the line of code with &quot;.failureUrl(&quot;/login?message=error&quot;)&quot;. Does this mean that my problem is in the html? Sincerely, I have no idea...</p>
  <p>Edit 2: Using code from <a href="https://stackoverflow.com/questions/13261794/display-error-messages-in-spring-login">Display error messages in Spring login</a></p>
  <p>to try to find what type of error I getting, my login method now is:</p>
  <pre><code>@GetMapping(&quot;/login-error&quot;)
      @RequestMapping(&quot;/login&quot;)
      public String login(HttpServletRequest request, Model model) {
          HttpSession session = request.getSession(false);
          String errorMessage = null;
          if (session != null) {
              AuthenticationException ex = (AuthenticationException) session
                      .getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
              if (ex != null) {
                  errorMessage = ex.getMessage();
              }
          }
          System.out.println(&quot;---&gt;&quot; + errorMessage);
          model.addAttribute(&quot;errorMessage&quot;, errorMessage);
          return &quot;login&quot;;
      }
  </code></pre>
  <p>And, again, even typing the correct credentials, as I specified in the &quot;userDetailsService&quot; method, I can't log in, but this time I can finally see the error message on the console:</p>
  <pre><code>---&gt;User not found or invalid password
  </code></pre>
  <p>Apparently I walked but without leaving the place, since right now I have no ideia what could be wrong with my code...</p>