The spring documentation also recommends that you do not use the csrf token in GET requests for security reasons.
"The ability to capture requests that receive a token helps against CSRF token leakage to a third party."
Thus, you can filter to transmit the token only for POST requests as follows:
$(function() {
var token = $("meta[name='_csrf']").attr("content");
var header = $("meta[name='_csrf_header']").attr("content");
$(document).ajaxSend(function(e, xhr, options) {
if (options.type == "POST") {
xhr.setRequestHeader(header, token);
}
});
});
The meta tags in the element <head>will be the same as in the previous answers:
<meta th:name="_csrf" th:content="${_csrf.token}"/>
<meta th:name="_csrf_header" th:content="${_csrf.headerName}"/>
source
share