django rest framwork使用jwt RS256解码签名时出错

时间:2018-03-17 11:22:21

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

我正在使用django rest framwork with jwt。

在设置中我使用了RS256 ALGORITHM

我希望在用户身份验证后发送令牌,这是我的功能我正在尝试使用user_id和is_user数据发送令牌,但是当我将请求中的令牌传递给服务器服务器响应时:

!cd %:p:h && latexmk -xelatex % && pdfviewer %:p:r.pdf

为什么?

这是我的http://localhost:8000/login/服务器响应:

detail
:
"Error decoding signature."

登录功能:

{
    "username": "admin",
    "email": "",
    "token": "b'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJpc191c2VyIjp0cnVlfQ.kIz9TBYqVJVgFV5siM3QfNWHxBN28BlZRY_t8TFADmg'",
    "is_staff": false
}

jwt setting:

JWT_SECRET = 'secret'
JWT_ALGORITHM = 'HS256'
JWT_EXP_DELTA_SECONDS = 20

   def validate(self, data):
        user_obj = None
        email = data.get('email', None)
        username = data.get('username', None)
        password = data.get('password')
        if not email and not username:
            raise ValidationError("email or username is required!")
        if '@' in username:
            email = username
        user = User.objects.filter(
            Q(email=email) |
            Q(username=username)
        ).distinct()

        # user = user.exclude(email__isnull=True).exclude(email__iexact='')
        if user.exists() and user.count() == 1:
            user_obj = user.first()
        else:
            raise ValidationError("this username/email is not valid")
        if user_obj:
            if not user_obj.check_password(password):
                raise ValidationError("password is incorrect")

        # payload_handler(user)
        # payload = payload_handler(user_obj)

        payload = {
            'user_id': user_obj.id,
            'is_user': True,
        }
        jwt_token = jwt.encode(payload, JWT_SECRET, JWT_ALGORITHM)
        # code = json_response({'token': jwt_token.decode('utf-8')})

        data['token'] = jwt_token
        return data

1 个答案:

答案 0 :(得分:0)

对于你的"逻辑功能"你使用HS256算法并在你的" jwt设置"你使用RS256。我认为这一定是个问题。