Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log out doesn't work in Spring Boot application (POST method not supported)

I have a Spring Boot application with the following configuration

@Configuration
@EnableWebSecurity
open class WebSecurityConfig : WebSecurityConfigurerAdapter() {
    override fun configure(http:HttpSecurity) {
        http
            .authorizeRequests()
                .antMatchers("/css/**", "/js/**", "/fonts/**")
                .permitAll().and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .authorizeRequests()
                .anyRequest().authenticated()
                .and()
            .logout() 
                .logoutSuccessUrl("/login") 
                .permitAll()
            .and().csrf().disable()
    }
    @Autowired
    fun configureGlobal(auth:AuthenticationManagerBuilder) {
        auth
            .inMemoryAuthentication()
                .withUser("[email protected]").password("test").roles("USER")
    }
}

When I try to log out, I get the error

There was an unexpected error (type=Method Not Allowed, status=405). Request method 'POST' not supported

How can I fix it?

How to reproduce

  1. Check out the code from this repository.
  2. gradle bootRun
  3. Go to http://localhost:8080, enter [email protected] and test as user name and password, respectively.
  4. Press the logout button.

Update 1: This doesn't work, either.

http
    .authorizeRequests()
        .antMatchers("/css/**", "/js/**", "/fonts/**", "/logout‌​")
        .permitAll().and()
    .formLogin()
        .loginPage("/login")
        .permitAll()
        .and()
    .authorizeRequests()
        .anyRequest().authenticated()
        .and()
    .csrf().disable()
like image 547
Dmitrii Pisarenko Avatar asked Sep 14 '26 07:09

Dmitrii Pisarenko


1 Answers

I'm not familiar with Thymeleaf, but at least this will give you some idea.

The problem is not your SecurityConfig but the

th:action="@{/logout}"

(not redirect to /logout, check Network tab in Chrome or Firefox).

If I replace it with

action="/logout"

Then it work perfectly.

like image 120
Duc D. Nguyen Avatar answered Sep 15 '26 20:09

Duc D. Nguyen