简体   繁体   English

需要 Django Rest Framework JWT 登录

[英]Django Rest Framework JWT login required

I am using django rest framework jwt authentication.我正在使用 django rest 框架 jwt 身份验证。 I get token successfully and can add token to cookie.我成功获得令牌并且可以将令牌添加到 cookie。 But when i try to reach views that requires login, JWT authentication is not working.但是当我尝试访问需要登录的视图时,JWT 身份验证不起作用。 Always redirects to login.html.总是重定向到 login.html。

Http request header: Http请求头:

Authorization: JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjozLCJ1c2VybmFtZSI6ImFkbWluIiwiZXhwIjoxNTA1NjU3NDgwLCJlbWFpbCI6ImFkbWluQGdtYWlsLmNvbSJ9.Ro507cIEisRle_iKgH4dm3-tSbrrsaCUYtP2CIK9jLM授权:JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjozLCJ1c2VybmFtZSI6ImFkbWluIiwiZXhwIjoxNTA1NjU3NDgwLCJlbWFpbCI6ImFkbWluQGdtYWlsLmNvbSJ9.Ro507cIEisRle_iKgH4dm3-tSbrrsaCUYtP2CIK9jLM

Cookie: token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjozLCJ1c2VybmFtZSI6ImFkbWluIiwiZXhwIjoxNTA1NjU3NDgwLCJlbWFpbCI6ImFkbWluQGdtYWlsLmNvbSJ9.Ro507cIEisRle_iKgH4dm3-tSbrrsaCUYtP2CIK9jLM饼干:标记= eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjozLCJ1c2VybmFtZSI6ImFkbWluIiwiZXhwIjoxNTA1NjU3NDgwLCJlbWFpbCI6ImFkbWluQGdtYWlsLmNvbSJ9.Ro507cIEisRle_iKgH4dm3-tSbrrsaCUYtP2CIK9jLM

class SystemUserView(View):
    @method_decorator(login_required)
    def get(self, request, user_id):
        users = list(User.objects.all().values('email', 'id', 'username'))
        return HttpResponse(HttpResponse(json.dumps(users), content_type="application/json"))

urls:网址:

from django.conf.urls import url
from . import views
from .views import SystemUserView, UserAuthenticationView
from rest_framework_jwt.views import obtain_jwt_token, refresh_jwt_token, verify_jwt_token
urlpatterns = [
    url(r'^$', views.index, name="index"),
    url(r'^login/?$', UserAuthenticationView.login, name="index"),
    url(r'^user/(?P<user_id>[0-9]+)/$', SystemUserView.as_view(), name='user'),
    url(r'^api-token-auth/', obtain_jwt_token),
    url(r'^api-token-refresh/', refresh_jwt_token),
    url(r'^api-token-verify/', verify_jwt_token),
]

Django version :(1, 11, 5, 'final', 0) Django 版本 :(1, 11, 5, 'final', 0)

Python 3.6.2蟒蛇 3.6.2

https://jpadilla.github.io/django-rest-framework-jwt https://jpadilla.github.io/django-rest-framework-jwt

 class SystemUserView(View):

Looks like you are importing a Django view, not a DRF APIView .看起来您正在导入 Django 视图,而不是 DRF APIView DRF and Django use different authentication systems. DRF 和 Django 使用不同的身份验证系统。 If you are authenticated with JWT towards DRF, Django Views will still redirect you to the login page.如果您通过 JWT 向 DRF 进行身份验证,Django Views 仍会将您重定向到登录页面。

Here is an example of a DRF view using normal Token Authentication.以下是使用普通令牌身份验证的 DRF 视图示例。 I haven't tested it and you will have to adapt it for JWT, but it should lead you onto the right path.我还没有测试过它,你必须为 JWT 调整它,但它应该会引导你走上正确的道路。

from rest_framework import authentication, permissions
from django.contrib.auth.models import User

class ListUsers(APIView):
    """
    View to list all users in the system.

    * Requires token authentication.
    """
    authentication_classes = (authentication.TokenAuthentication,)
    permission_classes = (permissions.IsAuthenticated,)

    def get(self, request, user_id):
        """
        Return a list of all users.
        """
        users = list(User.objects.all().values('email', 'id', 'username'))
        return Response(users)

Also, please consider using a DRF serializer for converting the user object to json.另外,请考虑使用DRF 序列化程序将用户对象转换为 json。

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

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