【问题标题】:Django Rest Framework JWTDjango Rest 框架 JWT
【发布时间】:2015-10-19 22:37:00
【问题描述】:

我正在使用 Django Rest Framework 为我的应用程序构建 API,并希望实现 DjangoRestFramework-JWT 以进行令牌身份验证。这些步骤看起来很简单,但是当我测试端点时,我得到一个 500 错误。终端输出是大量的 html,表示没有提供 csrf_token。代码和错误如下。非常感谢您的帮助。

curl -X POST -d "username=admin&password=123abc" http://127.0.0.1:8000/api/token/auth/

CSRF 错误

<!DOCTYPE html>
<html lang="en">
    <head>
      <meta http-equiv="content-type" content="text/html; charset=utf-8">
      <meta name="robots" content="NONE,NOARCHIVE">
      <title>403 Forbidden</title>
    </head>
    <body>
        <div id="summary">
            <h1>Forbidden <span>(403)</span></h1>
            <p>CSRF verification failed. Request aborted.</p>
            <p>You are seeing this message because this site requires a CSRF cookie when submitting forms. This cookie is required for security reasons, to ensure that your browser is not being hijacked by third parties.</p>
            <p>If you have configured your browser to disable cookies, please re-enable them, at least for this site, or for &#39;same-origin&#39; requests.</p>
        </div>

        <div id="info">
            <h2>Help</h2>
            <p>Reason given for failure:</p>
            <pre>CSRF cookie not set.</pre>
            <p>In general, this can occur when there is a genuine Cross Site Request Forgery, or when <a href="https://docs.djangoproject.com/en/1.8/ref/csrf/">Django's CSRF mechanism</a> has not been used correctly.  For POST forms, you need to ensure:</p>
            <ul>
                <li>Your browser is accepting cookies.</li>
                <li>The view function passes a <code>request</code> to the template's <a href="https://docs.djangoproject.com/en/dev/topics/templates/#django.template.backends.base.Template.render"><code>render</code></a> method.</li>
                <li>In the template, there is a <code>{% csrf_token %}</code> template tag inside each POST form that targets an internal URL.</li>
                <li>If you are not using <code>CsrfViewMiddleware</code>, then you must use <code>csrf_protect</code> on any views that use the <code>csrf_token</code> template tag, as well as those that accept the POST data.</li>
            </ul>

            <p>You're seeing the help section of this page because you have <code>DEBUG = True</code> in your Django settings file. Change that to <code>False</code>, and only the initial error message will be displayed.  </p>
            <p>You can customize this page using the CSRF_FAILURE_VIEW setting.</p>
        </div>
    </body>
</html>

Settings.py

INSTALLED_APPS = (
    ...
    'rest_framework',
    ...
)

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
    ),
}

JWT_AUTH = {
    'JWT_RESPONSE_PAYLOAD_HANDLER':
        'app.utils.jwt_response_payload_handler',
    'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=30000),
}

全局网址.py

urlpatterns = patterns(
    ...
    url(r'^api/auth/token/$', 'rest_framework_jwt.views.obtain_jwt_token'),
    url(r'^api/auth/', include('rest_framework.urls', namespace='rest_framework')),
    url(r'^api/', include(router.urls)),
)

【问题讨论】:

    标签: python django django-rest-framework jwt auth-token


    【解决方案1】:

    您收到此错误是因为 DRF 设置中定义了 SessionAuthentication 类。

    DRF 的SessionAuthentication 使用 Django 的会话框架进行身份验证,这需要检查 CSRF。 DRF 对经过身份验证的用户强制执行 CSRF 检查。 这意味着只有经过身份验证的请求才需要 CSRF 令牌,而匿名请求可以在没有 CSRF 令牌的情况下发送。

    现在您正在从经过身份验证的用户发出请求,并且按照settings.py 文件中DEFAULT_AUTHENTICATION_CLASSES 中定义的顺序,首先调用SessionAuthentication。它检查请求是否来自经过身份验证的用户,如果是,则必须在请求标头中设置CSRF 令牌。由于您没有在请求中发送CSRF Token,因此它返回错误。

    您在发出请求时需要在标头中传递 CSRF Token。你可以通过--header/-Hoption 做到这一点。

    --header "X-CSRFToken: {token_value}" # using '--header'
    -H "X-CSRFToken: {token_value}" # using '-H' 
    

    您可以将您的请求更改为:

    curl --header "X-CSRFToken: {token_value}" -X POST -d "username=admin&password=123abc" http://127.0.0.1:8000/api/token/auth/
    

    另一种方法:

    还有另一种选择,您可以创建一个自定义会话身份验证类,它不会对经过身份验证的用户强制执行此 CSRF 检查。

    SessionCsrfExemptAuthentication 下面的类不会对经过身份验证的用户强制执行 CSRF 令牌检查。

    from rest_framework.authentication import SessionAuthentication 
    
    class SessionCsrfExemptAuthentication(SessionAuthentication):
    
        def enforce_csrf(self, request):
            return  # To not perform the csrf check previously happening
    

    然后您可以在您的设置中定义此身份验证类,例如:

    'DEFAULT_AUTHENTICATION_CLASSES': (
            # Replace DRF session authentication class with our custom authentication class
            'rest_framework.authentication.SessionCsrfExemptAuthentication',
            'rest_framework.authentication.BasicAuthentication',
            'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
        ),
    

    【讨论】:

      猜你喜欢
      • 2015-08-13
      • 2019-02-25
      • 2018-11-19
      • 2019-03-01
      • 2021-12-21
      • 2020-10-21
      • 2019-07-11
      • 2016-06-27
      • 2018-10-17
      相关资源
      最近更新 更多