I have three spring boot applications running on the same server with different paths. All three of them expose API endpoints and one of them also serve web resources such as HTML
, JavaScript
and CSS
.
Application 1:
- Serves UI files
- Serves API endpoints
Application 2
- Serves API Endpoints
Application 3
- Serves API Endpoints
So far we only enabled CSRF validation for application 1. Which worked well with org.springframework.security.web.csrf.CookieCsrfTokenRepository
. We send XSRF-TOKEN
as cookie and angularJs sends back X-XSRF-TOKEN
in header in each request.
Now we are planning to introduce XSRF
to other two applications the same way we did for Application 1.
But we are stuck with a problem. AngularJs sends the XSRF-TOKEN from application 1 and uses the same token for all three applications while each application has its own TOKEN cookies per (application path).
This causes CSRF
validation to fail for other two services.
Here are the configurations that I use.
Spring-boot version : 1.5.3
Angular version : 1.3.18
<beans:bean id="csrfTokenRepository" class="org.springframework.security.web.csrf.CookieCsrfTokenRepository">
<beans:property name="cookieHttpOnly" value="false" />
</beans:bean>
The Error that I get
{
"timestamp": 1509437659613,
"status": 403,
"error": "Forbidden",
"message": "Invalid CSRF Token '2fa60cb2-803f-4b2b-a1d6-7e10e56ca649' was found on the request parameter '_csrf' or header 'X-XSRF-TOKEN'.",
"path": "/application2/posturl/path"
}
Here the token 2fa60cb2-803f-4b2b-a1d6-7e10e56ca649
is from application1 with cookie path /application1.
My observations so far:
- I checked and made sure all three applications are setting cookies
to be
httpOnly=false
. - I can see that all three applications have
their own
XSRF-TOKEN
cookies in the chrome developer console with their own paths. - I did not write a single line at the angular end to change its default behaviour.
- All three applications are running as WAR files on the same IP and Port.
What I suspect here is that Angular is not honouring the path attribute of the cookies and it goes with the first cookie with a name
XSRF-TOKEN
.
Is there a way to go around this issue?