Your server has CSRF enabled. The @EnableWebSecurity
annotation will enable CSRF by default as stated in the documentation.
CSRF protection is enabled by default with Java configuration.
There are two ways to "fix" this, either disable CSRF or submit the CSRF-token when doing PATCH
, POST
, PUT
, and DELETE
actions.
To disable CSRF do it in the Spring Security configuration
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
}
}
To submit the CSRF-token you must include it in the request to the server (in this example a JSP with sending a POST
request)
<c:url var="logoutUrl" value="/logout"/>
<form action="${logoutUrl}" method="post">
<input type="submit" value="Log out" />
<input type="hidden" name="${_csrf.parameterName}"
value="${_csrf.token}"/>
</form>
All examples taken from the Spring Cross Site Request Forgery (CSRF) documentation
Please consider the recommendation from Spring when considering whether to disable CSRF
[...] use CSRF protection for any request that could be processed by a
browser by normal users. If you are only creating a service that is
used by non-browser clients, you will likely want to disable CSRF
protection.