使用 vuejs 和 django restframework 和 restauth 的 django-webpack-loader 的身份验证问题

Authentication issues with django-webpack-loader with vuejs and django restframework and restauth

我使用 vue cli 和 webpack 创建了 vue.js 前端,使用 django restframework 创建了后端,我还使用 restauth 为 google 实现了社交身份验证。在我使用 django webpack 之前,登录和注销可以正常工作并且符合预期。 (我应该提到我正在使用 tokenauthentication)在我的 settings.py 文件中,我在 restframework 身份验证可能性中同时启用了 sessionauthentication 和 tokenauthentication。启用的两个设置从未造成任何麻烦。但是,在使用 django webpack 加载器使用 django 模板渲染前端 vue 文件后,我会不断收到一个错误,说我的 csrf 令牌不存在。但是,如果我从 settings.py 文件中删除 sessionauthentication 选项,在这种情况下登录将正常工作。

有人知道为什么会这样吗?

https://medium.com/@rodrigosmaniotto/integrating-django-and-vuejs-with-vue-cli-3-and-webpack-loader-145c3b98501a

我用上面的博客实现了webpack loader的功能

我在使用 django-webpack-loader 后从 settings.py 中删除了 'rest_framework.authentication.SessionAuthentication' 行,问题得到解决

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
    'rest_framework.authtoken',
    'rest_framework.authentication.BasicAuthentication',
    'rest_framework.authentication.SessionAuthentication',
)
}

我基本上改成了下面这样:

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
    'rest_framework.authtoken'
    'rest_framework.authentication.BasicAuthentication',

)
}

因为您可能正在使用 axios,所以您需要在 main.js

中包含这两行
/* configure axios to forward the csrftoken back to Django on POST requests */
axios.defaults.xsrfHeaderName = 'X-CSRFToken';
axios.defaults.xsrfCookieName = 'csrftoken';