我遇到了jwt.exceptions.DecodeError: Invalid header padding错误

10

我有一个简单的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一起使用。我该怎么办?


你第一个参数的jwt编码没有缺少闭合括号吗? - JBallin
不,那是个打错了的错误,抱歉,我在我的原始代码中有它。 - Ani Amirjanyan
为什么在创建过程中要解码您的令牌? - JBallin
需要使用utf-8进行解码。它可以放在中间件或创建过程中。由于我的令牌在中间件中是str类型,因此无法使用utf-8进行解码。 - Ani Amirjanyan
1个回答

3

因此,我删除了.encode('ascii','ignore')以及['sub'],看起来它可以工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接