如文档中所述,默认情况下启用CSRF保护。这意味着,
/logaout仅可
post应要求访问。发布请求必须包含CSRF令牌。因此,您可以执行以下操作:
<form action="/logout" method="post"> <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/> <input type="submit" value="Log out" /></form>此表格只是示例。当然,您可以通过单击链接删除按钮并使用javascript提交。
作为另一种解决方案,可以禁用CSRF保护(就安全性而言,这不是一个好主意):
@Configuration@EnableWebSecuritypublic class SecurityConfig extends WebSecurityConfigurerAdapter { .... @Override protected void configure(HttpSecurity http) throws Exception { http. .csrf().disable() .authorizeRequests() ....; }}


