将 Auth0 装饰器与 Flask-RESTful 资源一起使用

use*_*968 4 python flask flask-restful auth0

我需要将 Auth0 用于我的 Flask-RESTful 应用程序。Auth0 有一个在视图函数上使用装饰器的示例requires_auth

@app.route('/secured/ping')
@cross_origin(headers=['Content-Type', 'Authorization'])
@requires_auth
def securedPing():
    return "All good. You only get this message if you're authenticated"
Run Code Online (Sandbox Code Playgroud)

对于 Flask-RESTful,我add_resourceResourceapp.route一起使用,而不是与视图函数一起使用。我该如何申请requires_authVersion

app = Flask(__name__)
API = Api(app)
CORS = CORS(app, resources={r'/api/*': {'origins': '*'}})
API.add_resource(Version, '/api/v1')
Run Code Online (Sandbox Code Playgroud)

dav*_*ism 8

Flask-Restful 文档描述了如何为资源指定装饰器

类上有一个Resource名为的属性method_decorators。您可以子类化Resource并添加您自己的装饰器,这些装饰器将添加到资源中的所有方法函数中。

class AuthResource(Resource):
    method_decorators = [requires_auth]

# inherit AuthResource instead of Resource to define Version
Run Code Online (Sandbox Code Playgroud)