如何在jwt和flask restplus中使用api-key

时间:2019-07-15 11:35:29

标签: rest flask jwt api-key flask-restplus

我正在使用flask restplus来定义我的资源,并进行jwt-extended进行身份验证,但是我无法弄清楚如何实现与sagger UI一起使用的Api键

我尝试使用:

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()

1 个答案:

答案 0 :(得分:0)

authorizations = {
    'apikey': {
        'type': 'apiKey',
        'in': 'header',
        'name': 'Authorization',
        'description': "Type in the *'Value'* input box below: **'Bearer <JWT>'**, where JWT is the token"
    }
}

api = Api(version='1.0', title='My MVC API',
          description='A simple Flask RestPlus powered API', authorizations=authorizations)