Java Spring will return a 403 Forbidden
if any request besides a GET
request is missing a
Cross Site Request Forgery Token (CSRF Token) in the X-XSRF-TOKEN
Header. Here is how to fix that issue
when using Postman. I have seen people online suggest that you disable CSRF Tokens but please don't do that. That is
silly. Those people are sily.
We need to create an environment in which to store our CSRF Token
In the top right of Postman, click the cog.
In the Pop Up window, Click Add
Environment Name
xsrf-token
in the first column.Add
in the bottom right cornerEnsure your environment is selected in the drop-down in the top right.
GET
requests do not require a CSRF Token to be allowed through our SpringSecurityConfig
GET
requestTests
tabpm.environment.set("xsrf-token", decodeURIComponent(pm.cookies.get("XSRF-TOKEN")));
Now when you call this endpoint in Postman, your CSRF Token will be stored in your environment variables.
Headers
tabX-XSRF-TOKEN
and a value of {{xsrf-token}}
, the
{{xsrf-token}}
value will be populated from our Environment we created earlier.Your request should now be from from CSRF errors
GET
request again to populate the value in case it has become invalid or has
expired.