如何使用try和except块在Python中捕获jwt ExpiredSignatureError异常

时间:2019-01-16 09:44:56

标签: python jwt

代替打印jwt.exceptions.ExpiredSignatureError jwt.exceptions.ExpiredSignatureError:签名已过期

我想在他们单击发送到邮件的URL时打印一条消息print(“令牌已过期”)

我已经尝试了即使页面过期也可以返回页面的代码

@mod.route('/forgot/<token>', methods=['GET', 'POST'])
    def get_resetpassword(token):
result = ''
errorMsg = None
try:
    token_decode = jwt.decode(token, app.config['SECRET KEY'], 'exp', algorithm='HS256')
    print(str(token_decode))
except jwt.ExpiredSignature:
    print("Invalid Token")
    if request.method == 'POST':
        if request.form['password'] != '':
            return redirect('/company/admin')
        else:
            errorMsg= 'Please Enter Password '
    return render_template('reset_token.html',error=errorMsg)

下面的代码在没有try和Except块的情况下可以正确执行

@mod.route('/forgot/<token>', methods=['GET', 'POST'])
def get_resetpassword(token):
result = ''
errorMsg = None
token_decode = jwt.decode(token, app.config['SECRET KEY'], 'exp', algorithm='HS256')
print(str(token_decode))

if request.method == 'POST':
    if request.form['password'] != '':
        return redirect('/company/admin')
    else:
        errorMsg= 'Please Enter Password '
return render_template('reset_token.html',error=errorMsg)

我想在代码中插入try和except块

1 个答案:

答案 0 :(得分:0)

@mod.route('/forgot/<token>', methods=['GET', 'POST'])
def get_resetpassword(token):
result = ''
errorMsg = None
try:
token_decode = jwt.decode(token, app.config['SECRET KEY'], 'exp', algorithm='HS256')
print(str(token_decode))
    if request.method == 'POST':
    if request.form['password'] != '':
        return redirect('/company/admin')
    else:
        errorMsg= 'Please Enter Password '
return render_template('reset_token.html',error=errorMsg)
except jwt.ExpiredSignature:
print("Invalid Token")

除了障碍物,就在正确的地方,谢谢