Spring security custom login page authentication succeeds but error 405 is reported

This article mainly records the errors and solutions I encountered in my study

1 environment

springboot + thymeleaf + spring security

2 errors

This page appears, but the authentication has indeed been successful

3 Related code

3.1 security configuration class

Insert code snippet here
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class NewSevurityConfig extends WebSecurityConfigurerAdapter {
	
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        
        // Custom login page
        http.formLogin()
				.loginPage("/toLogin")
                .usernameParameter("username")
                .passwordParameter("password")
				.loginProcessingUrl("/login")
                .successForwardUrl("/");

    }
	
	// Other configuration omitted
}

3.2 Main front-end code

<div>
    <form th:action="@{/login}" method="post">
        username<input type="text" name="username">
                 password<input type="password" name="password">
        <button type="submit" >log in</button>
    </form>
</div>

4 Solution

After checking, the error is in successForwardUrl(“/”) in 3.1
Replace with defaultSuccessUrl(“/”)

// Custom login page
        http.formLogin()
				.loginPage("/toLogin")
                .usernameParameter("username")
                .passwordParameter("password")
				.loginProcessingUrl("/login")
                .defaultSuccessUrl("/"); //Replace successForwardUrl here with defaultSuccessUrl
                

5 Cause of error

The cause of the error is temporarily unclear. Due to time, it is temporarily recorded. We will learn more about the cause of the problem later.

Để lại một bình luận

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

x