【发布时间】:2019-11-24 02:30:19
【问题描述】:
我正在使用 flask restplus 来定义我的资源并使用 jwt-extended 进行身份验证,但我不知道如何实现 Api-keys 以与 sagger UI 一起使用
我试过用:
authorizations = {
'apikey': {
'type': 'apiKey',
'in': 'header',
'name': 'X-API-KEY'
}
}
并将@api.doc(security='apikey') 添加到需要身份验证的方法中。
#app configuration in config.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_jwt_extended import JWTManager
from flask_restplus import Api
app = Flask(__name__)
api = Api(app=app)
ns = api.namespace('auth', description='Manage users')
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECRET_KEY'] = 'some-secret-string'
db = SQLAlchemy(app)
app.config['JWT_SECRET_KEY'] = 'some-secret-string'
jwt = JWTManager(app)
app.config['JWT_BLACKLIST_ENABLED'] = True
app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = ['access', 'refresh']
#resources.py
from flask_restplus import Resource, reqparse , fields ,Api
from flask_jwt_extended import ( create_access_token,
create_refresh_token, jwt_required, jwt_refresh_token_required,
get_jwt_identity, get_raw_jwt
)
from config import api , ns ,app
from user_models import UserModel ,RevokedTokenModel
@ns.route('/users')
class AllUsers(Resource):
@jwt_required
def get(self):
"""Returns a list of all users."""
return UserModel.return_all()
【问题讨论】:
标签: rest flask jwt api-key flask-restplus