我正在使用 POST 方法请求密钥,如下所示,
var session_id; // to use for token based authentication
// prep
$(document).ready(function(){
// sending a csrftoken with every ajax request
function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
if (!chrome.cookies) {
chrome.cookies = chrome.experimental.cookies;
}
const csrf_from_cookies = {'url': 'https://dummy-site-xyz.com/', 'name': 'csrftoken'};
chrome.cookies.get(csrf_from_cookies, function(res){
csrftoken = res.value;
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.url)) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}
});
});
const sessionid = {'url': 'https://dummy-site-xyz.com/', 'name': 'sessionid'};
chrome.cookies.get(sessionid, function(res) …Run Code Online (Sandbox Code Playgroud) 我写了一个 Django 应用程序,它有一个外部身份验证系统,可以在某个 URL 上访问(比如,https://.../myproject/login)。这运行良好。
但是,当会话过期时,用户会被重定向到默认登录 url,即https://.../myproject/admin)。我想更改应用程序的行为,因此如果会话过期,则应将用户重定向到https://.../myproject/login并仅/admin在显式打开时使用登录名。
在 Django 中是否有内置的方法来做到这一点?