【问题标题】:JWT Token Authentication with Django Rest Framework使用 Django Rest 框架进行 JWT 令牌身份验证
【发布时间】:2018-12-15 13:12:03
【问题描述】:

我在我的 Django-Rest 应用程序上使用 react native 前端进行令牌身份验证时遇到了一些问题。我一直使用会话身份验证,这是我第一次设置具有这些要求的项目。

我已经安装了 pip -

djangorestframework_simplejwt

我知道当我点击端点api/token时正在生成令牌

我可以在前端检索它们。当我尝试在后端点击列表路由时出现我的问题,我得到的错误如下。

{
    "detail": "Authentication credentials were not provided."
}

我认为这可能是一个 cors 问题,或者是我的 axios 请求的问题,但我相当确定它们设置正确。另一个问题是我的视图集中的身份验证和权限类,这是我的直觉告诉我这个问题的来源。

相关settings.py信息--

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'social_django.middleware.SocialAuthExceptionMiddleware',
]

CORS_ORIGIN_ALLOW_ALL = True

CORS_ALLOW_METHODS = (
    'DELETE',
    'GET',
    'OPTIONS',
    'PATCH',
    'POST',
    'PUT',
)

CORS_ALLOW_HEADERS = (
    'accept',
    'accept-encoding',
    'authorization',
    'content-type',
    'dnt',
    'origin',
    'user-agent',
    'x-csrftoken',
    'x-requested-with',
)

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework.permissions.IsAuthenticated',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_simplejwt.authentication.JWTAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    )
}

视图集/序列化器/Url

class WorkoutViewSet(ModelViewSet):
    model = apps.get_model('backend', 'Workout')
    queryset = model.objects.all()
    serializer_class = serializers.WorkoutSerializer
    authentication_classes = (authentication.TokenAuthentication,)
    permission_classes = (permissions.IsAuthenticated,)

class WorkoutSerializer(serializers.ModelSerializer):
    class Meta:
        model = apps.get_model('backend', 'Workout')
        fields = ('name', 'is_complete', 'allow_copy', 'workout_goal', 'user')

router.register(r'workouts', views.WorkoutViewSet, base_name='workouts')

Axios 请求

export const workouts = (token) => {
  return axios({
    method: 'get',
    url: 'http://localhost:8000/workouts',
      headers: { 'authorization': `Bearer ${token}`}
  })
} 

感谢任何帮助/指导。

【问题讨论】:

  • 对于 djangorest 框架你必须使用这种格式curl -X GET http://localhost:8000/workouts/ -H 'Authorization: Token xxxxxxxxxx'
  • 试试Authorization(即headers: { 'Authorization': `Bearer ${token}`}
  • 如果你在apache上运行,那么你必须在/etc/apache2/apache2.conf中添加WSGIPassAuthorization On
  • 欣赏它,两者都不起作用。我更新了WSGIPassAuthorization On 并使用了大写字母。

标签: django django-rest-framework jwt axios


【解决方案1】:

在您的视图集中,您已经指定了您设置 TokenAuthentication 的 authentication_classes。

这意味着,对于您的 WorkoutViewSet,您指定仅使用 TokenAuthentication - 它在标头中使用 Token 前缀,因此您会得到 'Credentials not provided'

如果您想使用 JWT 令牌身份验证 - 您应该在此处明确设置或删除它并让默认选择的类处理身份验证

【讨论】:

  • 这成功了,并从不应该存在的默认权限中删除了rest_framework.authentication.TokenAuthentication
猜你喜欢
  • 2019-05-02
  • 2023-03-16
  • 2019-03-01
  • 2021-12-21
  • 1970-01-01
  • 2021-10-27
  • 2015-06-21
  • 2021-07-06
  • 2019-02-27
相关资源
最近更新 更多