【问题标题】:DRF How to use a Token to prove authenticationDRF 如何使用 Token 来证明身份验证
【发布时间】:2017-07-13 17:03:40
【问题描述】:

我正在使用 Django Rest Framework 来构建 API。我正在使用令牌身份验证。请求时我得到一个有效的令牌。然后我调用另一个 API 点来请求数据并发送我的令牌。我似乎没有进行身份验证。当我调试打印到服务器时,我的用户是匿名的,我的 request.auth 是无。

我在 VueJS 中使用 Javascript 进行调用:

    fetchHabits: function() {
      console.log('Token '+this.authToken.token);
      fetch('http://127.0.0.1:8000/api/habit/?format=json',{
         method: 'GET',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'Authorization': 'Token '+this.authToken.token
      },

  })
    .then(response => response.json())
    .then(json => this.habits = json)
  }

谁能告诉我如何将有效令牌传递给 DRF 以使其被识别。 如果我必须在一个类中对其进行编码,那么有效令牌存储在哪里?

谢谢

这是我的身份验证类。注意这两个被注释掉了,除非他们被注释掉了发送用户名/密码的api不起作用:

 REST_FRAMEWORK = {
     'DEFAULT_AUTHENTICATION_CLASSES': (
         'rest_framework.authentication.BasicAuthentication',
 #        'rest_framework.authentication.SessionAuthentication',
 #        'rest_framework.authentication.TokenAuthentication',

     ), }

效果: 目标是发送用户名/密码获取令牌,然后使用该令牌发出请求。

    'DEFAULT_AUTHENTICATION_CLASSES': (
#        'rest_framework.authentication.BasicAuthentication',
#        'rest_framework.authentication.SessionAuthentication',
#        'rest_framework.authentication.TokenAuthentication',

获取令牌,匿名用户 500 错误(第二部分失败)

    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.BasicAuthentication',
#        'rest_framework.authentication.SessionAuthentication',
#        'rest_framework.authentication.TokenAuthentication',

获取令牌,500错误匿名用户

    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
#        'rest_framework.authentication.TokenAuthentication',

获取令牌,500错误匿名用户

'DEFAULT_AUTHENTICATION_CLASSES': (
    'rest_framework.authentication.BasicAuthentication',
    'rest_framework.authentication.SessionAuthentication',
    'rest_framework.authentication.TokenAuthentication',

401 未经授权

在@Rajan 的帮助下,我发现 Header 有一个未知令牌(不在我的数据库中)并且正在返回匿名用户,如果我强制令牌内联它可以工作。但是当前的标头身份验证似乎不是使用我在标头中传递的令牌,而是另一个。过程中会不会被修改?

【问题讨论】:

  • 您是否在 settings.py 中为 DRF 配置了 DEFAULT_AUTHENTICATION_CLASSES
  • @rajan 更新以向您展示我现在在我的设置中。我还安装了应用程序:`'rest_framework.authtoken'、'corsheaders'、'rest_framework'、`
  • 请取消注释 TokenAuthentication 行。
  • @rajan 当我这样做时,我的代码获取令牌失败。我得到一个未授权的 401。
  • 那么你需要在get_token请求上应用AllowAny

标签: javascript django django-rest-framework


【解决方案1】:

解决方案是 HTTP 标头“损坏”,我需要将凭据包含在内。最终解决方案是:

  fetch('http://127.0.0.1:8000/api/habit/?format=json',{
     method: 'GET',
     headers: {
       'Accept': '*/*',
       'Content-Type': 'application/json',
       'Authorization': 'Token '+this.authToken.token
     },
     credentials: 'include'
  })

【讨论】:

    猜你喜欢
    • 2019-10-07
    • 2017-06-05
    • 2018-04-21
    • 2021-09-05
    • 2014-12-26
    • 2011-10-06
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    相关资源
    最近更新 更多