我有一个简单的Flask应用程序。 登录会创建一个令牌:
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_required
,但是出现了错误。
jwt.exceptions.DecodeError: Invalid header padding
我无法将另一个utf-8添加到中间件令牌中,因为不能与str
一起使用。我该怎么办?