Python 我得到jwt.exceptions.DecodeError:无效的头填充
所以我有一个简单的烧瓶应用程序。 登录将创建一个令牌:Python 我得到jwt.exceptions.DecodeError:无效的头填充,python,flask,jwt,authorization,Python,Flask,Jwt,Authorization,所以我有一个简单的烧瓶应用程序。 登录将创建一个令牌: token = jwt.encode({'user': token_data}, app.config['SECRET_KEY']).decode('utf-8') 中间件如下所示: def token_required(f): @wraps(f) def decorated(*args, **kwargs): data = request.headers['Authorization'].encode('
token = jwt.encode({'user': token_data}, app.config['SECRET_KEY']).decode('utf-8')
中间件如下所示:
def token_required(f):
@wraps(f)
def decorated(*args, **kwargs):
data = request.headers['Authorization'].encode('ascii', 'ignore')
token = str.replace(str(data), 'Bearer ', '')
if not token:
return jsonify({'message': 'Token is missing'}), 401
data = jwt.decode(token, app.config['SECRET_KEY'], algorithms=['HS256'])['sub']
return f(*args, **kwargs)
return decorated
然后我运行一个受保护的路由,它是@token\u required
,并得到错误信息。
jwt.exceptions.DecodeError:无效的标题填充
无法将另一个utf-8添加到中间件令牌,因为我无法将其与str
我能做什么?所以我删除了.encode('ascii','ignore')
还有
['sub']
而且它似乎很有效你是不是在jwt编码的第一个arg上缺少一个结束括号?不,这是一个输入错误,对不起,我在我的原始代码中有它。为什么你的令牌在创建过程中被解码?需要用utf-8解码。它可以放在中间件中,也可以放在创建过程中。由于我的令牌在中间件中是str类型,我无法用utf-8对其进行解码