Spring Boot + Spring MVC + Thymeleaf custom login fails without showing any error in console












0















I am trying to make a simple registration/login mechanism using Spring Boot + Thymeleaf.



I managed to register a user successfully (checked the new entry in the h2-console) but I have a problem while trying to log in.



Specifically, the application is redirected to /login?error page and there is no message at the console indicating what is wrong.



After debugging I discovered that the application does not stop at my Post controller method.



I provide you with my code:



login.html



<form th:action="@{/login}" method="post" th:object="${user}">
<div class="form-group">
<label for="username">Username</label>
<input th:field="*{username}" type="text" id="username" name="username" class="form-control" autofocus="autofocus"
placeholder="Username">
</div>
<div class="form-group">
<label for="password">Password</label>:
<input th:field="*{password}" type="password" id="password" name="password" class="form-control" placeholder="Password">
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<input type="submit"
name="login-submit"
id="login-submit"
class="form-control btn btn-success"
value="Log In">
</div>
<div class="col-sm-6 col-sm-offset-3">
<a th:href="@{/register}">Register</a>
</div>
</div>
</div>
</form>


UserController.java



    @Controller
public class UserController {

@Autowired
private UserService userService;

@Autowired
private SecurityService securityService;

@GetMapping("/login")
public String login(Model model) {
model.addAttribute("user", new User());
return "login";
}

@PostMapping("/login")
public String login(@ModelAttribute("user") User user) {
securityService.login(user.getUsername(), user.getPassword());
return "redirect:/index";
}

@GetMapping("/register")
public String register(Model model) {
model.addAttribute("user", new User());
return "register";
}

@PostMapping("/register")
public String register(@ModelAttribute("user") User user) {
userService.save(user);
return "redirect:/login";
}
}


Please help, thanks in advance!



EDIT:



SecurityConfig.java



    @Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private UserDetailsService userDetailsService;

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/h2-console/**", "/register**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll();

http.csrf().disable();
http.headers().frameOptions().disable();
}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
}

@Bean("authenticationManager")
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}

@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
}









share|improve this question





























    0















    I am trying to make a simple registration/login mechanism using Spring Boot + Thymeleaf.



    I managed to register a user successfully (checked the new entry in the h2-console) but I have a problem while trying to log in.



    Specifically, the application is redirected to /login?error page and there is no message at the console indicating what is wrong.



    After debugging I discovered that the application does not stop at my Post controller method.



    I provide you with my code:



    login.html



    <form th:action="@{/login}" method="post" th:object="${user}">
    <div class="form-group">
    <label for="username">Username</label>
    <input th:field="*{username}" type="text" id="username" name="username" class="form-control" autofocus="autofocus"
    placeholder="Username">
    </div>
    <div class="form-group">
    <label for="password">Password</label>:
    <input th:field="*{password}" type="password" id="password" name="password" class="form-control" placeholder="Password">
    </div>
    <div class="form-group">
    <div class="row">
    <div class="col-sm-6 col-sm-offset-3">
    <input type="submit"
    name="login-submit"
    id="login-submit"
    class="form-control btn btn-success"
    value="Log In">
    </div>
    <div class="col-sm-6 col-sm-offset-3">
    <a th:href="@{/register}">Register</a>
    </div>
    </div>
    </div>
    </form>


    UserController.java



        @Controller
    public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private SecurityService securityService;

    @GetMapping("/login")
    public String login(Model model) {
    model.addAttribute("user", new User());
    return "login";
    }

    @PostMapping("/login")
    public String login(@ModelAttribute("user") User user) {
    securityService.login(user.getUsername(), user.getPassword());
    return "redirect:/index";
    }

    @GetMapping("/register")
    public String register(Model model) {
    model.addAttribute("user", new User());
    return "register";
    }

    @PostMapping("/register")
    public String register(@ModelAttribute("user") User user) {
    userService.save(user);
    return "redirect:/login";
    }
    }


    Please help, thanks in advance!



    EDIT:



    SecurityConfig.java



        @Configuration
    public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
    http
    .authorizeRequests()
    .antMatchers("/h2-console/**", "/register**").permitAll()
    .anyRequest().authenticated()
    .and()
    .formLogin()
    .loginPage("/login")
    .permitAll();

    http.csrf().disable();
    http.headers().frameOptions().disable();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
    }

    @Bean("authenticationManager")
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
    }

    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
    return new BCryptPasswordEncoder();
    }
    }









    share|improve this question



























      0












      0








      0








      I am trying to make a simple registration/login mechanism using Spring Boot + Thymeleaf.



      I managed to register a user successfully (checked the new entry in the h2-console) but I have a problem while trying to log in.



      Specifically, the application is redirected to /login?error page and there is no message at the console indicating what is wrong.



      After debugging I discovered that the application does not stop at my Post controller method.



      I provide you with my code:



      login.html



      <form th:action="@{/login}" method="post" th:object="${user}">
      <div class="form-group">
      <label for="username">Username</label>
      <input th:field="*{username}" type="text" id="username" name="username" class="form-control" autofocus="autofocus"
      placeholder="Username">
      </div>
      <div class="form-group">
      <label for="password">Password</label>:
      <input th:field="*{password}" type="password" id="password" name="password" class="form-control" placeholder="Password">
      </div>
      <div class="form-group">
      <div class="row">
      <div class="col-sm-6 col-sm-offset-3">
      <input type="submit"
      name="login-submit"
      id="login-submit"
      class="form-control btn btn-success"
      value="Log In">
      </div>
      <div class="col-sm-6 col-sm-offset-3">
      <a th:href="@{/register}">Register</a>
      </div>
      </div>
      </div>
      </form>


      UserController.java



          @Controller
      public class UserController {

      @Autowired
      private UserService userService;

      @Autowired
      private SecurityService securityService;

      @GetMapping("/login")
      public String login(Model model) {
      model.addAttribute("user", new User());
      return "login";
      }

      @PostMapping("/login")
      public String login(@ModelAttribute("user") User user) {
      securityService.login(user.getUsername(), user.getPassword());
      return "redirect:/index";
      }

      @GetMapping("/register")
      public String register(Model model) {
      model.addAttribute("user", new User());
      return "register";
      }

      @PostMapping("/register")
      public String register(@ModelAttribute("user") User user) {
      userService.save(user);
      return "redirect:/login";
      }
      }


      Please help, thanks in advance!



      EDIT:



      SecurityConfig.java



          @Configuration
      public class SecurityConfig extends WebSecurityConfigurerAdapter {

      @Autowired
      private UserDetailsService userDetailsService;

      @Override
      protected void configure(HttpSecurity http) throws Exception {
      http
      .authorizeRequests()
      .antMatchers("/h2-console/**", "/register**").permitAll()
      .anyRequest().authenticated()
      .and()
      .formLogin()
      .loginPage("/login")
      .permitAll();

      http.csrf().disable();
      http.headers().frameOptions().disable();
      }

      @Autowired
      public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
      auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
      }

      @Bean("authenticationManager")
      @Override
      public AuthenticationManager authenticationManagerBean() throws Exception {
      return super.authenticationManagerBean();
      }

      @Bean
      public BCryptPasswordEncoder bCryptPasswordEncoder() {
      return new BCryptPasswordEncoder();
      }
      }









      share|improve this question
















      I am trying to make a simple registration/login mechanism using Spring Boot + Thymeleaf.



      I managed to register a user successfully (checked the new entry in the h2-console) but I have a problem while trying to log in.



      Specifically, the application is redirected to /login?error page and there is no message at the console indicating what is wrong.



      After debugging I discovered that the application does not stop at my Post controller method.



      I provide you with my code:



      login.html



      <form th:action="@{/login}" method="post" th:object="${user}">
      <div class="form-group">
      <label for="username">Username</label>
      <input th:field="*{username}" type="text" id="username" name="username" class="form-control" autofocus="autofocus"
      placeholder="Username">
      </div>
      <div class="form-group">
      <label for="password">Password</label>:
      <input th:field="*{password}" type="password" id="password" name="password" class="form-control" placeholder="Password">
      </div>
      <div class="form-group">
      <div class="row">
      <div class="col-sm-6 col-sm-offset-3">
      <input type="submit"
      name="login-submit"
      id="login-submit"
      class="form-control btn btn-success"
      value="Log In">
      </div>
      <div class="col-sm-6 col-sm-offset-3">
      <a th:href="@{/register}">Register</a>
      </div>
      </div>
      </div>
      </form>


      UserController.java



          @Controller
      public class UserController {

      @Autowired
      private UserService userService;

      @Autowired
      private SecurityService securityService;

      @GetMapping("/login")
      public String login(Model model) {
      model.addAttribute("user", new User());
      return "login";
      }

      @PostMapping("/login")
      public String login(@ModelAttribute("user") User user) {
      securityService.login(user.getUsername(), user.getPassword());
      return "redirect:/index";
      }

      @GetMapping("/register")
      public String register(Model model) {
      model.addAttribute("user", new User());
      return "register";
      }

      @PostMapping("/register")
      public String register(@ModelAttribute("user") User user) {
      userService.save(user);
      return "redirect:/login";
      }
      }


      Please help, thanks in advance!



      EDIT:



      SecurityConfig.java



          @Configuration
      public class SecurityConfig extends WebSecurityConfigurerAdapter {

      @Autowired
      private UserDetailsService userDetailsService;

      @Override
      protected void configure(HttpSecurity http) throws Exception {
      http
      .authorizeRequests()
      .antMatchers("/h2-console/**", "/register**").permitAll()
      .anyRequest().authenticated()
      .and()
      .formLogin()
      .loginPage("/login")
      .permitAll();

      http.csrf().disable();
      http.headers().frameOptions().disable();
      }

      @Autowired
      public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
      auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
      }

      @Bean("authenticationManager")
      @Override
      public AuthenticationManager authenticationManagerBean() throws Exception {
      return super.authenticationManagerBean();
      }

      @Bean
      public BCryptPasswordEncoder bCryptPasswordEncoder() {
      return new BCryptPasswordEncoder();
      }
      }






      java spring-boot spring-mvc thymeleaf






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 20 at 16:35







      Paris Karagiannopoulos

















      asked Jan 20 at 14:59









      Paris KaragiannopoulosParis Karagiannopoulos

      113211




      113211
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You don't have to implement the Login by yourself.



          Simply create a login.html with a form like this:



          <form action="#" th:action="@{/login}" method="post">
          <input class="form-control" id="username" name="username" th:autofocus="true"/>
          <input class="form-control" id="password" name="password"
          <button>Login</button>
          </form>


          Spring will handle everything for you.






          share|improve this answer
























          • the result is the same. the problem is not fixed

            – Paris Karagiannopoulos
            Jan 20 at 15:29











          • How did you configure the web security? Do you have password encription?

            – Simon Martinelli
            Jan 20 at 16:10











          • I edited the original post to add my security configuration

            – Paris Karagiannopoulos
            Jan 20 at 16:35











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54277704%2fspring-boot-spring-mvc-thymeleaf-custom-login-fails-without-showing-any-erro%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You don't have to implement the Login by yourself.



          Simply create a login.html with a form like this:



          <form action="#" th:action="@{/login}" method="post">
          <input class="form-control" id="username" name="username" th:autofocus="true"/>
          <input class="form-control" id="password" name="password"
          <button>Login</button>
          </form>


          Spring will handle everything for you.






          share|improve this answer
























          • the result is the same. the problem is not fixed

            – Paris Karagiannopoulos
            Jan 20 at 15:29











          • How did you configure the web security? Do you have password encription?

            – Simon Martinelli
            Jan 20 at 16:10











          • I edited the original post to add my security configuration

            – Paris Karagiannopoulos
            Jan 20 at 16:35
















          0














          You don't have to implement the Login by yourself.



          Simply create a login.html with a form like this:



          <form action="#" th:action="@{/login}" method="post">
          <input class="form-control" id="username" name="username" th:autofocus="true"/>
          <input class="form-control" id="password" name="password"
          <button>Login</button>
          </form>


          Spring will handle everything for you.






          share|improve this answer
























          • the result is the same. the problem is not fixed

            – Paris Karagiannopoulos
            Jan 20 at 15:29











          • How did you configure the web security? Do you have password encription?

            – Simon Martinelli
            Jan 20 at 16:10











          • I edited the original post to add my security configuration

            – Paris Karagiannopoulos
            Jan 20 at 16:35














          0












          0








          0







          You don't have to implement the Login by yourself.



          Simply create a login.html with a form like this:



          <form action="#" th:action="@{/login}" method="post">
          <input class="form-control" id="username" name="username" th:autofocus="true"/>
          <input class="form-control" id="password" name="password"
          <button>Login</button>
          </form>


          Spring will handle everything for you.






          share|improve this answer













          You don't have to implement the Login by yourself.



          Simply create a login.html with a form like this:



          <form action="#" th:action="@{/login}" method="post">
          <input class="form-control" id="username" name="username" th:autofocus="true"/>
          <input class="form-control" id="password" name="password"
          <button>Login</button>
          </form>


          Spring will handle everything for you.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 20 at 15:22









          Simon MartinelliSimon Martinelli

          6,17811230




          6,17811230













          • the result is the same. the problem is not fixed

            – Paris Karagiannopoulos
            Jan 20 at 15:29











          • How did you configure the web security? Do you have password encription?

            – Simon Martinelli
            Jan 20 at 16:10











          • I edited the original post to add my security configuration

            – Paris Karagiannopoulos
            Jan 20 at 16:35



















          • the result is the same. the problem is not fixed

            – Paris Karagiannopoulos
            Jan 20 at 15:29











          • How did you configure the web security? Do you have password encription?

            – Simon Martinelli
            Jan 20 at 16:10











          • I edited the original post to add my security configuration

            – Paris Karagiannopoulos
            Jan 20 at 16:35

















          the result is the same. the problem is not fixed

          – Paris Karagiannopoulos
          Jan 20 at 15:29





          the result is the same. the problem is not fixed

          – Paris Karagiannopoulos
          Jan 20 at 15:29













          How did you configure the web security? Do you have password encription?

          – Simon Martinelli
          Jan 20 at 16:10





          How did you configure the web security? Do you have password encription?

          – Simon Martinelli
          Jan 20 at 16:10













          I edited the original post to add my security configuration

          – Paris Karagiannopoulos
          Jan 20 at 16:35





          I edited the original post to add my security configuration

          – Paris Karagiannopoulos
          Jan 20 at 16:35




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54277704%2fspring-boot-spring-mvc-thymeleaf-custom-login-fails-without-showing-any-erro%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Callistus III

          Ostreoida

          Plistias Cous