I have UI written with Angular 2 and Java based backend that uses OpenID Connect authentication on top of Spring Security.
The authentication works fine but only for GET requests. I'm getting HTTP 403 every time I perform POST, PUT or DELETE methods on a resource:
{
"status": 403,
"message": "Invalid CSRF Token 'null' was found on the request parameter '_csrf' or header 'X-CSRF-TOKEN'.",
}
I use HttpClient like this:
http.post(
'/api/my-resource',
JSON.stringify(myObject),
new RequestOptions({
headers: new Headers({'Content-Type': 'application/json'})
})
)
When I add withCredentials: true
to the RequestOptions as proposed here I still get HTTP 403 but with different message:
{
"status": 403,
"message": "Could not verify the provided CSRF token because your session was not found.",
}
What do I need to do to make HttpClient
work with CSRF?
Note: I also took a look at similar questions, especially angular-2-spring-security-csrf-implementation-problems, but the solutions proposed there do not solve my problem. At the same time I don't want to disable CSRF.