简体   繁体   English

Django Rest 框架 JWT 身份验证要求 CSRF 令牌

[英]Django Rest Framework JWT authentication asking for CSRF token

I am using django rest framework simplejwt for authentication.我正在使用 django rest 框架 simplejwt 进行身份验证。 Here is the rest framework settings:下面是 rest 框架设置:

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES' : [
        'rest_framework.permissions.IsAuthenticated',
    ],
    'DEFAULT_AUTHENTICATION_CLASSES' : [
        'rest_framework_simplejwt.authentication.JWTAuthentication'
    ],
    'DEFAULT_PAGINATION_CLASS' : 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE' : 3
}

As much as I am aware, if no DEFAULT_AUTHENTICATION_CLASSES are mention, django will default to the session based authentication which requires csrf token.据我所知,如果没有提及 DEFAULT_AUTHENTICATION_CLASSES,则 django 将默认为基于 session 的身份验证,该身份验证需要 csrf 令牌。 I have not included the session authentication and have used simplejwt as shown above.我没有包括 session 身份验证,并且使用了 simplejwt,如上所示。 Yet I receive the error of CSRF verification failed when handling POST requests, which is not supposed to be required.然而,我在处理 POST 请求时收到 CSRF 验证失败的错误,这不应该是必需的。 What am I doing wrong?我究竟做错了什么?

Did you try this:你试过这个:

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES' : [
        'rest_framework.permissions.IsAuthenticated',
    ],
    'DEFAULT_AUTHENTICATION_CLASSES' : [
        'rest_framework.rest_framework_simplejwt.authentication.JWTAuthentication'
    ],
    'DEFAULT_PAGINATION_CLASS' : 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE' : 3
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM