我正在使用带有 jwt 的 django rest 框架。
在设置中我使用了 RS256 ALGORITHM
我想在用户身份验证后发送令牌,这是我的功能我正在尝试使用 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设置:
JWT_AUTH = {
'JWT_SECRET_KEY': SECRET_KEY,
'JWT_ALGORITHM': 'RS256',
'JWT_AUTH_HEADER_PREFIX': 'Bearer',
'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=6600),
}