我有一个具有以下配置的 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
我该如何解决?
如何重现
- 查看 this repository 中的代码.
gradle bootRun
- 转到
http://localhost:8080
,分别输入usr@provider.com
和test
作为用户名和密码。 - 按注销按钮。
更新 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/