首页 > 解决方案 > 将身份验证装饰器添加到烧瓶 restx

问题描述

我有一个使用flask-restxand的 Flask 应用程序flask-login。我希望默认情况下所有路由都需要登录,并明确定义不需要身份验证的公共路由。我已经按照这个问题中给出的示例开始使用装饰器:

使 Flask-Login 的 login_required 成为默认值的最佳方法

它适用于函数端点,但不适用于restx资源端点。

我尝试将函数添加为装饰器并使用该method_decorators字段。例如:

def public_route(decorated_function):
    """
    This is a decorator to specify public endpoints in our flask routes
    :param decorated_function:
    :return:
    """
    decorated_function.is_public = True
    return decorated_function


class HelloWorld(ConfigurableResource):

    method_decorators = {"get": [public_route]}

    @public_route
    @api.doc('Welcome message')
    def get(self):
        return {'hello': 'world'}

这个测试通过了:

def test_hello_world_is_public():
    api = Namespace('health', description='Health related operations')
    hello = HelloWorld(api, config=None, logger=None)
    is_public_endpoint = getattr(hello.get, 'is_public', False)
    assert is_public_endpoint

我的挑战是我看不到如何在我的身份验证逻辑中访问此属性:


    @app.before_request
    def check_route_access():
        """
        This function decides whethere access should be granted to an endpoint.
        This function runs before all requests.
        :return:
        """
        is_public_endpoint = getattr(app.view_functions[request.endpoint], 'is_public', False)

        if person_authorized_for_path(current_user, request.path, is_public_endpoint):
            return
        # Otherwise access not granted
        return redirect(url_for("auth.index"))

这适用于普通函数端点,但不适用于 restx 资源。

我知道这restx是将我的资源类包装在一个函数中,以便烧瓶可以进行调度,但我不知道如何从这里访问装饰器。所以我有一些问题:

标签: pythonflaskflask-restplusflask-restx

解决方案


基于thisthismethod_decorators变量应该是一个函数列表,所以你应该像这样使用它:

def _perform_auth(method):
    is_public_endpoint = getattr(method, 'is_public', False)
    # place the validation here

class Resource(flask_restx.Resource):
    method_decorators = [_perform_auth]

推荐阅读