我正在将Flask_jwt_extended与jsonrpc后端一起用于Flask。
App Factory:
import os
from flask import Flask, jsonify, make_response, request
from flaskr.db import db
from flask_migrate import Migrate
from jsonrpc.backend.flask import api as apibackend
import flaskr.models
from flask_jwt_extended import JWTManager
from flask_jwt_extended import (
create_access_token, get_jwt_identity, jwt_required
)
def create_app(tests=None):
app = Flask(__name__, instance_relative_config=True)
if tests is None:
if os.environ.get("FLASK_ENV") == "production":
app.config.from_object('flaskr.config.ProdConfig')
else:
app.config.from_object('flaskr.config.DevConfig')
else:
app.config.from_object('flaskr.config.TestingConfig')
# DB & Migrations
db.init_app(app)
app.db = db
migrate = Migrate(app, db)
# RPC
jwt = JWTManager(app)
app.add_url_rule('/api', 'api', apibackend.as_view(), methods=['POST'])
# Instance
try:
os.makedirs(app.instance_path)
except OSError:
pass
# errors as json
@app.errorhandler(404)
@app.errorhandler(403)
@app.errorhandler(410)
@app.errorhandler(405)
def page_not_found(e):
return make_response(jsonify({
'error': e.name,
'description': e.description,
}), e.code)
# generic rout for debug
@app.route('/generic', methods=['GET', 'POST'])
def hello():
return request.headers.get('Authorization')
from jsonrpc.backend.flask import api as api_back
@api_back.dispatcher.add_method(name="auth.refresh")
@jwt_required
def refresh(**kwargs):
current_user = get_jwt_identity()
current_user = User.query.filter_by(username=current_user).first()
if current_user:
return create_access_token(identity=current_user.getIdentity())
return False
return app
当我调用“ auth.refresh”时,引发了 ExpiredSignatureError 。
我尝试使用 @jwt_required_optional 装饰器, get_jwt_identity 始终返回null。但是当我发布到 / generic 时,我可以看到我的Bearer令牌。这表示我的标头正常,JWTManager无法“看到”请求或未正确加载。
烧瓶== 1.0.2 flask-jwt-extended == 3.17.0