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



我正在使用 django rest framwork 和 jwt。

在设置中我使用了RS256算法

我想在用户身份验证后发送令牌,这是我的函数,我正在尝试发送带有user_id令牌并is_user数据,它将产生令牌,但是当我将令牌传入请求到服务器服务器响应时:

detail
:
"Error decoding signature."

为什么?

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

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

登录功能:

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

智威汤逊设置:

JWT_AUTH = {
    'JWT_SECRET_KEY': SECRET_KEY,
    'JWT_ALGORITHM': 'RS256',
    'JWT_AUTH_HEADER_PREFIX': 'Bearer',
    'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=6600),
}

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

最新更新