为什么用户提交注册表格时不执行registerSubmit?春天

问题描述 投票:0回答:2

我不知道为什么registerSubmit函数在提交注册表单(register.html)时未运行,我想这与Spring Security的工作方式有关。甚至尝试过使用一个简单的控制器方法返回一个视图,但是根本没有执行,单击提交时我看到的全部是同一页。

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,我已将csrf保护禁用为@M。 Deinum建议,但这也不起作用。

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .inMemoryAuthentication()
                .withUser("[email protected]"). 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();

    }





}

java html spring security
2个回答
1
投票
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";
        }.....

如果此方法不起作用,则可以通过使用Postman之类的工具(例如发送请求的工具,并通过调试服务器端代码找出可接受的请求)来找到有关错误的更多详细信息。如果Spring Security出现问题,例如AuthenticationFailure,则错误应记录在控制台中。


0
投票

好吧,显然注册方法卡在这里

if (bindingResult.hasErrors()){
            return "register";
        } 
© www.soinside.com 2019 - 2024. All rights reserved.