【发布时间】:2019-08-13 06:04:07
【问题描述】:
再次尝试制作我的第一个烧瓶应用程序,这一次,(在我创建了所有我需要的并且一切顺利之后)我试图用flask_jwt_extended 保护一些端点,但我找不到如何工作在我的页面中使用它们,文档主要是关于显示 JSON 消息,一些教程使用邮递员,而在我的情况下,我使用的是 HTML 模板。
例如,用户将他的凭据从登录页面发送到此端点:
@app.route('/login', methods=['POST'])
def UserLogin():
data = parser.parse_args()
current_user = UserModel.find_by_username(data['username'])
if not current_user:
return {'message': 'User {} doesn\'t exist'.format(data['username'])}
if UserModel.verify_hash(data['password'], current_user.password):
access_token = create_access_token(identity = data['username'])
refresh_token = create_refresh_token(identity = data['username'])
resp = jsonify({'login': True}) #I just added this line from the documentation
set_access_cookies(resp, access_token) # and this one
set_refresh_cookies(resp, refresh_token) # and this one
return redirect(url_for('results'))
else:
return {'message': 'Wrong credentials'}
当然,我添加了 @jwt_required 装饰器 results 端点:
@app.route('/result',methods = ['POST','GET'])
@jwt_required
def results():
temp={}
if request.method == 'POST':
# some code to fill temp with values
return render_template('result.html',data=temp)
所以我收到了{
"msg": "Missing cookie \"access_token_cookie\""
}
显然是因为我没有将 jwt 发回,但如果在 return 语句中发送它,我该如何将用户重定向到我想要的页面??
确实我用app.config['JWT_TOKEN_LOCATION'] = ['cookies']
【问题讨论】:
-
你能显示完整的请求吗(例如使用
curl)? -
我不明白你的意思,但是发送到登录端点的请求是从一个简单的基本表单(在 HTML 页面中)发送的。
标签: python cookies flask jwt flask-jwt-extended