You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flask-CORS与Flask-JWT-Extended兼容问题:添加@jwt_required后报500错误

问题原因及解决

问题出在@jwt_required的使用方式上:

flask-jwt-extended库中的jwt_required是一个需要调用的装饰器(哪怕不需要传入自定义参数),你当前直接写@jwt_required,相当于把装饰器的定义函数直接当作装饰器来使用,而它本身需要接收被装饰的视图函数作为参数,这就导致了调用时出现TypeError: wrapper() missing 1 required positional argument: 'fn'的错误。

修改后的代码

@app.route('/user_authentication/protected', methods=['POST'])
@cross_origin(headers=['Content-Type','Authorization'])
@jwt_required()  # 这里加上括号
def protected():
    # username = get_jwt_identity()
    username = "sana ne "
    # response send to Vue page
    response = {}
    response['user'] = username
    print(response)
    return jsonify(response), 200

当你移除@jwt_required后,装饰器链恢复正常,所以接口能正常工作;添加错误的@jwt_required时,装饰器的调用逻辑被破坏,触发500错误。

内容的提问来源于stack exchange,提问作者adam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 04:10:36