【问题标题】:Log out doesn't work in Spring Boot application (POST method not supported)注销在 Spring Boot 应用程序中不起作用(不支持 POST 方法)
【发布时间】:2016-12-22 11:39:17
【问题描述】:

我有一个具有以下配置的 Spring Boot 应用程序

@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("usr@provider.com").password("test").roles("USER")
    }
}

当我尝试注销时,我得到了错误

出现意外错误(类型=不允许的方法,状态=405)。 不支持请求方法“POST”

我该如何解决?

如何重现

  1. 查看this repository的代码。
  2. gradle bootRun
  3. 转到http://localhost:8080,分别输入usr@provider.comtest 作为用户名和密码。
  4. 按注销按钮。

更新 1:这也不起作用。

http
    .authorizeRequests()
        .antMatchers("/css/**", "/js/**", "/fonts/**", "/logout‌​")
        .permitAll().and()
    .formLogin()
        .loginPage("/login")
        .permitAll()
        .and()
    .authorizeRequests()
        .anyRequest().authenticated()
        .and()
    .csrf().disable()

【问题讨论】:

    标签: java spring authentication spring-boot kotlin


    【解决方案1】:

    您是否尝试过指定注销路径?

    .logout() 
        .logoutSuccessUrl("/login")
        .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
        .permitAll()
    

    【讨论】:

    • 当我在文件中添加import org.springframework.security.web.util.AntPathRequestMatcher 时,编译器说它无法解析引用。
    • 抱歉,改用.logoutUrl("/logout")
    【解决方案2】:

    您可以将"/logout" 添加到允许路径的第一部分。

    但是你为什么要使用 a 和 a 来注销呢?您可以使用将发出 GET 请求的简单链接 ()。

    【讨论】:

    • 我尝试了您的方法,请参阅“更新 1”。这就是你的意思吗?
    【解决方案3】:

    我不熟悉 Thymeleaf,但至少这会给你一些想法。

    问题不在于您的 SecurityConfig,而在于

    th:action="@{/logout}"

    (不重定向到 /logout,检查 Chrome 或 Firefox 中的网络选项卡)。

    如果我用

    替换它

    action="/logout"

    然后它就完美地工作了。

    【讨论】:

      猜你喜欢
      • 2015-12-05
      • 1970-01-01
      • 2016-06-23
      • 2017-01-13
      • 2018-06-03
      • 2020-03-31
      • 2016-02-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多