- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为使用 Flask 的应用程序构建一个基于 token 的后端 (API),我正在尝试在其中使用 Flask_Security .因为我使用的是 Peewee ORM ,我关注了this guide构建基本设置,我现在必须构建应该让用户登录的 View ,然后构建一个实际提供一些有用数据的 View 。
所以我返回 token 的登录 View 如下所示:
@app.route('/api/login', methods=['POST'])
def api_login():
requestJson = request.get_json(force=True)
user = User.select().where(User.username == requestJson['username']).where(User.password == requestJson['password']).first()
if user:
return jsonify({'token': user.get_auth_token()})
else:
return jsonify({'error': 'LoginError'})
这很好用;我得到一个 token 作为回应。我现在想使用 auth_token_required
保护另一个 View ,我想使用 token 作为 header 。所以我尝试如下:
@app.route('/api/really-important-info')
@auth_token_required('SECURITY_TOKEN_AUTHENTICATION_HEADER')
def api_important_info():
return jsonify({'info': 'really important'})
但是启动 Flask 会导致 AttributeError: 'str' object has no attribute '__module__'
。 The documentation对它的使用也不是很有帮助。
有人知道我怎样才能让它工作吗?欢迎任何提示!
最佳答案
错误是因为装饰器不期望任何参数(除了它正在装饰的函数)。
@auth_token_required
def api_important_info():
pass
配置值 SECURITY_TOKEN_AUTHENTICATION_KEY
或 SECURITY_TOKEN_AUTHENTICATION_HEADER
分别表示传入请求的查询参数或 header 中的位置。
Flask-安全 automatically sends this token当向登录路由发出 JSON 请求时,发送给客户端以备将来使用。
您可能会对 Flask-Security 提供的多种身份验证方法感到困惑。授权 token 对于没有浏览器管理的 session cookie 的 api 很有用。 “正常”基于 session 的身份验证由 login_required
处理.
关于python - 如何让 Flask_Security 中的 auth_token_required 工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28727954/
我每次使用 Flask Security 时都会收到警告。 FlaskWTFDeprecationWarning: "flask_wtf.Form" has been renamed to "Flas
我正在使用 Flask 构建一个网站,我现在正尝试在其中使用 Flask_Security用于基于 token 的身份验证。我现在想从用户那里得到一个 auth_token,为此我使用 get_aut
我正在尝试为使用 Flask 的应用程序构建一个基于 token 的后端 (API),我正在尝试在其中使用 Flask_Security .因为我使用的是 Peewee ORM ,我关注了this g
我是一名优秀的程序员,十分优秀!