Automatically retry AJAX request with new CSRF token if token error is returned

다른 탭에서 로그아웃했다가 다시 로그인한 후 exec_json을 시도하는 등,
사용자가 보기에는 정상적인 상황인데도 보안정책(CSRF) 오류가 발생할 수 있음.
이 때 자연스럽게 토큰을 갱신한 후 요청을 다시 전송하도록 개선함.
This commit is contained in:
Kijin Sung 2022-10-20 16:56:47 +09:00
parent ed649fb58c
commit de7bf0a0a6
2 changed files with 21 additions and 5 deletions

View file

@ -53,12 +53,19 @@
};
/**
* @brief Get CSRF token for the document
* Get CSRF token for the document
*/
window.getCSRFToken = function() {
return $("meta[name='csrf-token']").attr("content");
};
/**
* Set CSRF token for the document
*/
window.setCSRFToken = function(token) {
$("meta[name='csrf-token']").attr("content", token);
};
/* Intercept jQuery AJAX calls to add CSRF headers */
$.ajaxPrefilter(function(options) {
if (!isSameOrigin(location.href, options.url)) return;