flask_login模块实现过程
2017-06-08 14:11
495 查看
1、先创建用户表,该表继承flask_login 模块的UserMixin类
vi model.py
其中id字段是必须要有的,登录成功之后,每次请求界面,UserMixin类定义的获取该登录用户id的字段就是id
vi views.py
前端验证成功后,通过login_user()函数记录登录状态
该过程会将user_id,及前端请求header存入session
3、添加回调函数
根据user_id获取用户对象
@login_manager.user_loader
def load_user(user_id):
return User.query.get(user_id)
user_loader会将load_user函数复于LoginManager()类的self.user_callback属性
4、为其他函数加上@login_required装饰器
当用户访问index时,会判断该用户的登录状态,会通过reload_user属性重新加载用户
self.user_callback此时等于步骤3中的reload_user函数
vi model.py
其中id字段是必须要有的,登录成功之后,每次请求界面,UserMixin类定义的获取该登录用户id的字段就是id
class UserMixin(object): ''' This provides default implementations for the methods that Flask-Login expects user objects to have. ''' if not PY2: # pragma: no cover # Python 3 implicitly set __hash__ to None if we override __eq__ # We set it back to its default implementation __hash__ = object.__hash__ @property def is_active(self): return True @property def is_authenticated(self): return True @property def is_anonymous(self): return False def get_id(self): try: print '99222', self.id return text_type(self.id) except AttributeError: raise NotImplementedError('No `id` attribute - override `get_id`') def __eq__(self, other): ''' Checks the equality of two `UserMixin` objects using `get_id`. ''' if isinstance(other, UserMixin): return self.get_id() == other.get_id() return NotImplemented def __ne__(self, other): ''' Checks the inequality of two `UserMixin` objects using `get_id`. ''' equal = self.__eq__(other) if equal is NotImplemented: return NotImplemented return not equal2、登录接口
vi views.py
前端验证成功后,通过login_user()函数记录登录状态
该过程会将user_id,及前端请求header存入session
3、添加回调函数
根据user_id获取用户对象
@login_manager.user_loader
def load_user(user_id):
return User.query.get(user_id)
user_loader会将load_user函数复于LoginManager()类的self.user_callback属性
4、为其他函数加上@login_required装饰器
当用户访问index时,会判断该用户的登录状态,会通过reload_user属性重新加载用户
self.user_callback此时等于步骤3中的reload_user函数
相关文章推荐
- Yate开源软件Jabberclient模块实现注册openfire过程分析
- 为go smtp模块增加smtp.Auth的LOGIN模式实现, 以及修复smtp.go中的一...
- 分析一个通用的rtsp server实现过程基础模块
- Flask入门之一:简单实现login功能
- 实现Register&Login过程中遇到的问题
- 家庭视频监控系统Ⅱ--C#登录模块实现过程
- Java Web开发过程中登陆模块的验证码的实现方式总结
- Java Web开发过程中登陆模块的验证码的实现方式总结
- Python任务调度模块 – APScheduler,Flask-APScheduler实现定时任务
- 问卷调查模块实现的过程中的历程
- 用户登录模块的实现过程
- 使用异步过程调用(APC)实现模块注入
- Flask-login 简单 Demo, 最简单的实现登录方法
- Linux文件系统启动过程及login的实现
- 使用异步过程调用(APC)实现模块注入
- 使用异步过程调用(APC)实现模块注入
- Linux文件系统启动过程及login的实现
- 分析一个通用的rtsp server实现过程发送模块
- Flask框架通过Flask_login实现用户登录功能示例
- Linux文件系统启动过程及login的实现 .