Flask-Login的request_loader与user_loader有什么关系? [英] How is Flask-Login's request_loader related to user_loader?

查看:959
本文介绍了Flask-Login的request_loader与user_loader有什么关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提出一个相当神秘的问题,我提前道歉。但是,尽管经历了很多的材料,我还是不明白。如果你能指出这一点,那将是非常好的。

在flask-login中request_loader的用途是什么?它如何与user_loader装饰器交互?如果我正在使用基于令牌的身份验证系统(我计划将令牌发送到我的angularJS前端,将令牌存储在那里并在授权中发送该令牌 - 令牌头),我会需要一个request_loader或将一个user_loader(我检查auth头,看看用户是否存在)足够?

解决方案

从Flask-Login 文档
$ b


有时,您希望不使用cookie来登录用户,例如使用
标题值或传递的api键作为查询参数。在这些情况下,
应该使用 request_loader 回调。这个回调应该
的行为与您的 user_loader 回调相同,除了它接受
Flask请求而不是user_id。

因此,要回答你的问题,他们都为Flask-Login提供相同的功能。它们都用来加载用户。但是, request_loader 适用于自定义登录。



下面是一个很好的教程,我发现利用 request_loader 来利用基于标记的身份验证(帖子不是我的自己,我只是分享链接): http://gouthamanbalaraman.com /blog/minimal-flask-login-example.html


I apologize in advance for asking a rather cryptic question. However, I did not understand it despite going through a lot of material. It would be great if you could shed some light on this.

What is the purpose of a request_loader in flask-login? How does it interact with the user_loader decorator?

If I am using a token based authentication system (I am planning on sending the token to my angularJS front end, storing the token there and sending that token in the authorization-token header), will I need a request_loader or will a user_loader (where I check the auth header and see if the user exists) suffice?

解决方案

From the Flask-Login documentation:

Sometimes you want to login users without using cookies, such as using header values or an api key passed as a query argument. In these cases, you should use the request_loader callback. This callback should behave the same as your user_loader callback, except that it accepts the Flask request instead of a user_id.

So, to answer your question, they both serve the same function for Flask-Login. They are both used to load the user. request_loader, however, is appropriate for custom logins.

Here's a great tutorial I found that utilizes request_loader to take advantage of token based authentication (The post is not my own, I'm merely sharing the link): http://gouthamanbalaraman.com/blog/minimal-flask-login-example.html

这篇关于Flask-Login的request_loader与user_loader有什么关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆