我需要为我的 Flask-RESTful 应用程序使用 Auth0。 Auth0 有一个 example在 View 函数上使用 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"
对于 Flask-RESTful,我将 add_resource
与 Resource
类一起使用,而不是将 app.route
与 View 函数一起使用。如何将 requires_auth
应用于 Version
?
app = Flask(__name__)
API = Api(app)
CORS = CORS(app, resources={r'/api/*': {'origins': '*'}})
API.add_resource(Version, '/api/v1')
最佳答案
Flask-Restful 文档描述了如何 specify decorators for a resource .
There is a property on the
Resource
class calledmethod_decorators
. You can subclass theResource
and add your own decorators that will be added to all method functions in resource.
class AuthResource(Resource):
method_decorators = [requires_auth]
# inherit AuthResource instead of Resource to define Version
关于python - 将 Auth0 装饰器与 Flask-RESTful 资源一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44657159/