我正在尝试使用 Flask-JWT 目前在基础级别扩展。 我从用户表单中获得了经过身份验证的用户和密码。我创建了一个访问令牌,将其包含在响应中并路由到另一个受保护的路由。请找到以下代码的较短版本...
from flask import Flask, jsonify, request
from flask_jwt_extended import,JWTManager, jwt_required, create_access_token,get_jwt_identity)
app.config['JWT_SECRET_KEY'] = 'super-secret'
jwt = JWTManager(app)
app.config['JWT_TOKEN_LOCATION'] = ['headers']
app.config['JWT_BLACKLIST_ENABLED'] = True
jwt = JWTManager(app)
app.config['PROPAGATE_EXCEPTIONS'] = True
@log_blueprint.route('/', methods=['GET', 'POST'])
def login():
form = LoginForm()
if request.method == 'POST':
if error is None and username = form.request['user'] and pwd = form.request['pwd'] :
access_token = create_access_token(identity=user)
resp = redirect(url_for('log_blueprint.protected'),access_token)
resp.headers = {'Authorization': 'Bearer {}'.format(access_token)}
return resp
@log_blueprint.route('/protected', methods=["POST","GET"])
@jwt_required
def protected():
current_user = get_jwt_identity()
return jsonify(logged_in_as=current_user), 200
它给我如下错误...
{"msg":"Missing Authorization Header"}
我尝试了此页面上的答案...https://stackoverflow.com/questions/52087743/flask-restful-noauthorizationerror-missing-authorization-header 但无法变得更好。 请让我知道此问题的任何解决方案。 对不起,如果有任何错字。
谢谢和问候, 阿比奈 J K
根据您使用的版本,根据最新稳定版本的更改日志,您应该使用如下符号:
@log_blueprint.route('/protected', methods=["POST","GET"])
@jwt_required()
def protected():
current_user = get_jwt_identity()
return jsonify(logged_in_as=current_user), 200
如果您使用邮递员发送请求,请确保检查“密钥”。
如果它在您的本地计算机上工作但在服务器上不工作,请确保您的服务器接受授权标头。在这里查看答案:Apache 剥离“授权”标头