I have implemented a custom csrf token repository which generates a new token for every http POST/DELETE req. I don't think token should be renewed for http GET, and if you look into source code of spring CsrfFilter class, it has a inner class DefaultRequiresCsrfFilter, which pass token checking for GET method.
The custom csrf token repository needs to implement interface CsrfTokenRepository. Actually I have reuse most of code of HttpSessionCsrfTokenRepository, which is spring default. The function that needs custom implementation is loadToken()
/*Customized loading token function, which invalidate the CSRF token once it is consumed. A new token is generated on next http req.*/
public CsrfToken loadToken(HttpServletRequest request) {
HttpSession session = request.getSession(false);
CsrfToken token = session == null ? null : (CsrfToken)session.getAttribute(this.sessionAttributeName);
if (/*HERE http request can be checked to see if it is a POST/DELETE */) {
if (session != null) {
//Remove the old token from session, and new token will be generated for next req
session.removeAttribute(DEFAULT_CSRF_TOKEN_ATTR_NAME);
}
}
return token;
}
And to get custom csrf token repository loaded, it needs to be configured in security.xml, as described in answers above.