Django - csrf cookie not set



几个小时以来,我一直在尝试使用Axios从分离的VueJS前端向Django应用程序中的端点发送POST请求。我的代码的问题是,无论我尝试什么,我都会得到Forbidden (CSRF cookie not set.),而我不能使用@crsf_exempt

我尝试了我找到的所有可能的解决方案,从更改Axios请求中的标头名称到将CSRF_COOKIE_SECURE设置为False,似乎都无法解决这个问题。

这是我的请求:

function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie !== '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i].trim();
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
console.log(cookieValue)
return cookieValue;
}
function req(){
this.csrf_token = getCookie('csrftoken')
axios({
method: 'post',
url: 'http://127.0.0.1:8000/backend/testreq/',
data: {
//Some data here
},
headers: {'Content-Type': 'application/json', 'X-CSRFToken': this.csrf_ftoken }
}).then(function (response) {
console.log(response)
}).catch(function (error) {
console.log(error)
});
},

正在发送令牌,但结果始终相同。Django应用程序也在使用Django Rest Framework,我不知道这是否是问题所在。

以下是我的一些设置.py(用于开发(:

CORS_ORIGIN_ALLOW_ALL = True  
CORS_ALLOW_CREDENTIALS = True
CORS_ALLOW_HEADERS = list(default_headers) + [
'xsrfheadername',
'xsrfcookiename',
'content-type',
'csrftoken',
'x-csrftoken',
'X-CSRFTOKEN',
]
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_WHITELIST = [
"http://localhost:8080",
"http://127.0.0.1:8080",
"http://localhost:8000",
"http://127.0.0.1:8000",
]
CORS_ALLOWED_ORIGINS = [
"http://localhost:8080",
"http://127.0.0.1:8080",
"http://localhost:8000",
"http://127.0.0.1:8000",
]
CSRF_TRUSTED_ORIGINS = [
"http://localhost:8080",
"http://127.0.0.1:8080",
"http://localhost:8000",
"http://127.0.0.1:8000",
]
SESSION_COOKIE_SAMESITE = None
CSRF_COOKIE_SAMESITE = None
CSRF_COOKIE_SECURE = False
CSRF_COOKIE_HTTPONLY = False
SESSION_COOKIE_SECURE = False

我不知道我还能尝试什么来解决这个问题,任何建议都很感激

Django Rest Framework中的默认身份验证方案是
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
]
}

会话身份验证在发出POST请求时需要CSRF令牌,除非使用@csrf_exempt豁免

Django中的CSRF_COOKIE_SECURE仅确保通过HTTPS 发送CSRF令牌

要解决您的问题,您可以将DEFAULT_AUTHENTICATION_CLASSES从默认更改为

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.TokenAuthentication',
]
}

一旦您切换到TokenAuthentication,您就必须将用户的凭据交换为Auth令牌,并将该令牌用于后续请求

Django Rest Framework代币认证指南

您也可以在这里查看这个SO答案,以便在基于类的视图上使用@csrf_exempt

我想这是由"跨域"引起的问题,您无法设置cookie或将其存储在由后端localhost:8000通过前端localhost:8080生成的浏览器中。如果要存储或修改cookie,则只能访问localhost:8000。

您可以使用Nginx作为反向代理来解决问题,以下是视频了解详细信息https://youtube.com/watch?v=VeDms9GPaLw

最新更新