簡體   English   中英

為什么在用戶提交注冊表時不執行 registerSubmit ? 春天

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

我不知道為什么在提交注冊表(register.html)時沒有運行registerSubmit函數,我的猜測是它與Spring Security的工作方式有關。 即使嘗試使用返回視圖的簡單控制器方法,但根本沒有執行,單擊提交時我看到的只是同一頁面。

注冊控制器.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";

    }

注冊.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 ,我以@M 的身份禁用了 csrf 保護。 Deinum 建議,但這也不起作用。

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

如果這不起作用,您可以使用 Postman 之類的工具發送請求並通過調試服務器端代碼找出接受請求的內容,從而找到有關錯誤的更多詳細信息。 如果 spring 安全性存在問題,例如 AuthenticationFailure,則應在控制台中記錄錯誤。

好的,顯然注冊方法卡在這里

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM