如何在登录过程中生成rest_framework_jwt标记

时间:2017-11-15 09:35:28

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

我正在尝试使用django-rest-framework-jwt进行身份验证。我做了jwt教程所说的所有内容(添加了设置,url和curl测试)。我在可浏览的api中使用127.0.0.1:8000/api/auth/token/成功获取了令牌。

但我没有得到的是如何在登录过程中生成令牌。我的猜测是在LoginSerializer中生成它可能吗? (或者我误解了这个概念!)

这就是我所拥有的:

# settings.py
REST_FRAMEWORK = {
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    ),
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
}

# project/urls.py
url(r'^api/auth/token/', obtain_jwt_token),

登录序列化程序并查看:

class UserLoginSerializer(ModelSerializer):
    token = CharField(allow_blank=True, read_only=True)
    class Meta:
        model = User
        fields = ('username', 'password', 'token')

    def validate(self, data):
        username = data.get('username',None)
        password = data['password']

        user = authenticate(username=username, password=password)
        if not user or not user.is_active:
            raise ValidationError("Invalid username/password.")

        # generate jwt token ??
        # data['token'] = generated jwt token??

        return data


class UserLoginAPIView(APIView):
    permission_classes = [AllowAny]
    serializer_class = UserLoginSerializer

    def post(self, request, *args, **kwargs):
        data = request.data
        serializer = UserLoginSerializer(data=data)
        if serializer.is_valid(raise_exception=True):
            new_data = serializer.data
            return Response(new_data, status=HTTP_200_OK)
        return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)

那我该怎么办?

1 个答案:

答案 0 :(得分:2)

这就是我所做的。

from rest_framework_jwt.settings import api_settings


class LoginView(APIView):
    '''
    Post call for user login.
    '''

    def post(self, request, format=None):
        serializer = LoginSerializer(data=request.data)

        if serializer.is_valid():

            # Check if user has valid credentials and return user instance else None
            user = authenticate(username=serializer.validated_data['username'],
                                password=serializer.validated_data['password'])

            if user is not None:

                jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
                jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER

                payload = jwt_payload_handler(user)
                token = jwt_encode_handler(payload)

                return Response({'msg':'Login successful', 'token': token, 'is_login_success': True}, status=status.HTTP_200_OK)
            else:
                return Response({'msg': 'Credentials are not valid!'}, status=status.HTTP_400_BAD_REQUEST)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

但你不必这样做,因为你的127.0.0.1:8000/api/auth/token/做得非常相同,除非你想在登录前记录一些东西。

如果您想了解obtain_jwt_token视图的工作原理,JSONWebTokenSerializer会为您提供帮助。