java - 注销在 Spring Boot 应用程序中不起作用(不支持 POST 方法)

标签 java spring authentication spring-boot kotlin

我有一个具有以下配置的 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")
    }
}

当我尝试注销时,出现错误

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

我该如何解决?

如何重现

  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()

最佳答案

我对 Thymeleaf 不熟悉,但至少这会让您有所了解。

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

th:action="@{/logout}"

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

如果我把它换成

action="/logout"

然后它就完美地工作了。

关于java - 注销在 Spring Boot 应用程序中不起作用(不支持 POST 方法),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38969088/

相关文章:

java - 反序列化操作请求消息正文时出现 JAX-WS 错误

java - Jersey 与 Spring 的融合

java - Spring 中 @Transactional 相对于 ProxyFactoryBean 的性能

java - 将spring boot mvc部署到远程tomcat 7

android - 如何检查用户是否使用 FB SDK 4.0 for Android 登录?

java - 在 java web 应用程序中添加用户身份验证

java - Android 服务 ("startService & stopService") 和播放/停止按钮合二为一

java - 动态 JTextArea 等待用户输入,如 Netbeans Console

javascript - 导入登录用户disqus评论

java - 需要帮助编码刽子手