当前文章收录状态:
未收录
解决方案
因为 spring security 在开启 csrf
防护的情况下,/logout 必须是以 POST 方法提交才行,<a> 标签请求是 GET 方法,所以报 404
1.很有可能开启了CSRF防护,可以关闭【不建议】
http.csrf().disable();
2.以 form 表单的形式请求 /logout 接口
<form th:action="@{/logout}" method="post"><input type="hidden" name="${_csrf.parameterName }" value="${_csrf.token }"/><input type="submit" value="logout"></form><form th:action="@{/logout}" method="post"> <input type="hidden" name="${_csrf.parameterName }" value="${_csrf.token }"/> <input type="submit" value="logout"> </form><form th:action="@{/logout}" method="post"> <input type="hidden" name="${_csrf.parameterName }" value="${_csrf.token }"/> <input type="submit" value="logout"> </form>
3.在 spring security 的配置中,添加 /logout 能够以 GET 请求的配置
@Overrideprotected void configure(Http<a href="https://www.zym88.cn/tag/security" title="更多关于 Security 的文章" target="_blank">Security</a> http) throws Exception {http.logout().<a href="https://www.zym88.cn/tag/logouturl" title="更多关于 logoutUrl 的文章" target="_blank">logoutUrl</a>("/logout").<a href="https://www.zym88.cn/tag/logoutsuccessurl" title="更多关于 logoutSuccessUrl 的文章" target="_blank">logoutSuccessUrl</a>("/home").logoutRequestMatcher(new AntPathRequestMatcher("/logout", "GET")).deleteCookies("JSESSIONID").invalidateHttpSession(true).and();}@Override protected void configure(Http<a href="https://www.zym88.cn/tag/security" title="更多关于 Security 的文章" target="_blank">Security</a> http) throws Exception { http.logout() .<a href="https://www.zym88.cn/tag/logouturl" title="更多关于 logoutUrl 的文章" target="_blank">logoutUrl</a>("/logout") .<a href="https://www.zym88.cn/tag/logoutsuccessurl" title="更多关于 logoutSuccessUrl 的文章" target="_blank">logoutSuccessUrl</a>("/home") .logoutRequestMatcher(new AntPathRequestMatcher("/logout", "GET")) .deleteCookies("JSESSIONID") .invalidateHttpSession(true) .and(); }@Override protected void configure(HttpSecurity http) throws Exception { http.logout() .logoutUrl("/logout") .logoutSuccessUrl("/home") .logoutRequestMatcher(new AntPathRequestMatcher("/logout", "GET")) .deleteCookies("JSESSIONID") .invalidateHttpSession(true) .and(); }
© 版权声明
1
本站网络名称:
明恒博客
2
本站永久网址:
www.zym88.cn
3 本站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行删除处理。
4 本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
5 本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报。
6 本站资源大多存储在云盘,如发现链接失效,请联系我们我们会第一时间更新。
4 本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
5 本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报。
6 本站资源大多存储在云盘,如发现链接失效,请联系我们我们会第一时间更新。
THE END
暂无评论内容