SpringBoot+SpringSecurity注銷時報錯404的兩種解決方法
解決方案
方法一: 關閉csrf保護
在SpringSecurity配置內中,關閉csrf保護
...
@Override
protected void configure(HttpSecurity http) throws Exception {
//關閉跨域訪問,解決注銷404問題
http.csrf().disable();
}
方法二:以post方式提交“注銷請求”
SpringSecurity的注銷配置
//注銷
http.logout().logoutUrl("/toLogout").logoutSuccessUrl("/toLogin").invalidateHttpSession(true);
所以,這里將/toLogout請求改為用表單以post方式提交。在此方法下,不用關閉SpringSecurity的csrf保護
<form th:action="@{/toLogout}" method="post">
<input type="submit" value="注銷"/>
</form>