I use org.springframework.security.web.csrf.CookieCsrfTokenRepository
to secure my spring based web application from CSRF attacks. This enables all the Controller methods to be called only by passing a X-XSRF-TOKEN
header or a _csrf
request parameter.
So in order for me get the response of a GET
URL in browser, I will have to write something like the below in browser address bar.
http://localhost:8080/api/someresource?_csrf=99e3b824-d0c9-409d-91ee-c7ccbdce313f&filter1=value1&filter2=value2&so=on
However, Some of these urls have filter mechanism based on the request parameters and unfortunately this extra _csrf
parameter breaks that logic.
As I see it, this request parameter should be needed if the request had passed the csrf validation. But I couldn't do anything in the documentation to remove the _csrf
request parameter on the application level.
At the moment, I do something like below.
@ResponseStatus(OK)
@RequestMapping(method = RequestMethod.GET, value = "/search/advanced")
@ResponseBody
public ResponseVO advancedSearch( @RequestParam MultiValueMap<String, String> queryParameters, Pageable pageable) {
queryParameters.remove(MyApplicatonConstants.CSRF_PARAM_NAME); //this line is the hack that I wrote
return doStuffAndGetFilteredData(queryParameters);
}
This implementation has its drawbacks.
- I will have to change all 143 controller methods to have this one line on the top.
- I have to remember to add this for new methods in future.
- it's a cheap hack and there should be some better and cleaner way of doing it.
Is there a way I can acheive this without rewriting that one line again and again?
Note:
- I fully understand that I can use CURL or Postman so I can pass
X-XSRF-TOKEN
in header. But it's not as quick as opening the URL in a new tab.