简体   繁体   中英

Why isn't registerSubmit executed when user submits registration form? spring

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.

RegisterController.java

 @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";

    }

register.html

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

    <table>
        <tr>
           <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>
        </tr>
        <tr>
            <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>
        </tr>
        <tr>
            <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>
        </tr>
        <tr>
            <td><button type="submit">Submit</button></td>
        </tr>

    </table>


</form>
<a th:href="@{/login}">Login</a>
</body>
</html>

SecurityConfig.java , I've disabled csrf protection as @M. Deinum suggested, but that doesn't work either.

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .inMemoryAuthentication()
                .withUser("user@mail.com"). password("{noop}pass").roles("USER");
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .authorizeRequests()
                    .antMatchers( "/register", "/login").permitAll()
                    .antMatchers("/result").permitAll()
                .and()
                .formLogin()
                .loginPage("/login")
                    .usernameParameter("email")
                    .defaultSuccessUrl("/profile", true)
                    .permitAll()

                    .and()

                .logout()
                        .permitAll();

    }





}

Try by adding consuming data type to headers. 

@RequestMapping(
    value = "/register",
    method = RequestMethod.POST,
    headers="Accept=application/x-www-form-urlencoded")
   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";
        } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM