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

标签 python flask auth0 flask-restful

我需要为我的 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_resourceResource 类一起使用,而不是将 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 called method_decorators. You can subclass the Resource 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/

相关文章:

jwt - 为什么 auth0 建议不要将 token 存储在 localStorage 中?

node.js - Auth0 getProfile 不返回 user_id

python - Python 中的列表理解

python - 导入错误 : No module named "utils" when using pickle. 加载

python - Pandas ffill/bfill 用于特定数量的观察

python - 如何使用 Flask SQLAlchemy 从 mysql 获取有限数量的行?

python - Flask Upload 如何配置文件存储路径

node.js - 未找到授权 token - Express-JWT 和 Auth0

python - 打开文件时lxml中的密码错误

python - 从 flask 蓝图中导入应用程序设置