java – Why isn't registerSubmit executed when user submits registration form? spring-ThrowExceptions

Exception or error:

I do not know why registerSubmit function is not running when submitting the registration form (register.html), my guess is that it has something to do with how Spring Security works. Even tried with a simple controller method that returns a view, but that is not executed at all, all I see when clicking submit is the same page.

 @RequestMapping(value = {"/register"}, method = RequestMethod.POST)
    public String registerSubmit(@Valid  User user, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return "register";
       User userFound = userRepository.findByEmail(user.getEmail());

        if (userFound != null)
            System.out.println("User already in database");
            return "register";

        return "result";



<!DOCTYPE html>
<html lang="en" xmlns:th="">
    <meta charset="UTF-8">
    <title>Register Page</title>
<h1>Register page</h1>
<form action="#" th:action="@{/register}" th:object="${user}" method="post">

           <td> <label for="firstName">First name:</label> </td>
            <td><input type="text" th:field="*{firstName}" id="firstName" /> </td>
            <td th:if="${#fields.hasErrors('firstName')}" th:errors="*{firstName}">firstName error</td>
            <td><label for="lastName">Last name:</label> </td>
            <td><input type="text" th:field="*{lastName}" id="lastName" /></td>
            <td th:if="${#fields.hasErrors('lastName')}" th:errors="*{lastName}">lastName error</td>
            <td><label for="email">email:</label> </td>
            <td><input type="text" th:field="*{email}" id="email" /></td>
            <td th:if="${#fields.hasErrors('email')}" th:errors="*{email}">email error</td>
            <td><button type="submit">Submit</button></td>


<a th:href="@{/login}">Login</a>
</html>, I’ve disabled csrf protection as @M. Deinum suggested, but that doesn’t work either.

public class SecurityConfig extends WebSecurityConfigurerAdapter {

    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
                .withUser(""). password("{noop}pass").roles("USER");

    public void configure(HttpSecurity http) throws Exception {
                    .antMatchers( "/register", "/login").permitAll()
                    .defaultSuccessUrl("/profile", true)





How to solve:
Try by adding consuming data type to headers. 

    value = "/register",
    method = RequestMethod.POST,
   public String registerSubmit(@Valid  User user, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return "register";

If this is not working you can find more details about your error by using Postman like a tool to send requests and find out what are the accepting request by debugging your server-side code. If there is an issue with spring security such as AuthenticationFailure the error should be logged in the console.


Ok, apparently the register method was stuck here

if (bindingResult.hasErrors()){
            return "register";

Leave a Reply

Your email address will not be published. Required fields are marked *