Flask 烧瓶-Restplus+;招摇过市,如何限制身份验证用户+;/角色

Flask 烧瓶-Restplus+;招摇过市,如何限制身份验证用户+;/角色,flask,flask-restplus,Flask,Flask Restplus,我有一个这样的端点: @api.route('/apps/<uid>') @api.doc(params={'uid': 'UUID of the app agent.'}) class AppAgentAPI(Resource): @login_required @api.marshal_with(app_agent) def get(self, uid): ... @api.route(“/apps/”) @doc(参数={'uid

我有一个这样的端点:

@api.route('/apps/<uid>')
@api.doc(params={'uid': 'UUID of the app agent.'})
class AppAgentAPI(Resource):

    @login_required
    @api.marshal_with(app_agent)
    def get(self, uid):
          ...
@api.route(“/apps/”)
@doc(参数={'uid':'UUID of the app agent.'})
AppAgentAPI类(资源):
@需要登录
@api.封送(应用程序代理)
def get(自我,uid):
...
目前,该端点的招摇过市文档是公开的

我想限制只有经过身份验证的用户才能访问swagger文档。(我所说的认证是指关于我的烧瓶应用程序)

如果我可以插入一些自定义逻辑,使其基于角色,那就更好了(例如:只有我的应用程序中的管理员才能看到此端点的文档,等等)


我该怎么做?

这是一个使用@app.before\u请求的黑客攻击:

DOC_URL = api_bp.url_prefix + '/doc'

@app.before_request
def before_request():
    if request.path[0: len(DOC_URL)] == DOC_URL and not (current_user and current_user.is_authenticated):
        print("Unauthenticated user trying to access swagger docs. Redirecting")
        return redirect(url_for('user.login'))
其中“api_bp”是Swagger正在记录的flask restplus api蓝图

它可以扩展以检查用户角色。但是,这适用于整个文档页面,更细粒度(逐端点)的规则不能以这种方式实现

flask restplus固有的解决方案,即:直接使用flask restplus API和更细粒度的规则,仍然是首选