如何用jwt和flask restplus使用api-key。

问题描述 投票:0回答:1

我使用flask restplus来定义我的资源和jwt-extended来进行认证,但我不知道如何实现Api-keys来与sagger UI一起使用。

我试着用.NET Framework 2.0,并在rquire authentification的方法中加入@api.doc(security=apikey)。

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
1个回答
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)
© www.soinside.com 2019 - 2024. All rights reserved.