【问题标题】:How to use api-key with jwt and flask restplus如何将 api-key 与 jwt 和烧瓶 restplus 一起使用
【发布时间】: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


    【解决方案1】:
    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)
    

    【讨论】:

    • 如果还是不行,你可以在输入框中添加'Bearer XXXXXX'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    相关资源
    最近更新 更多