Python flask实战订餐系统微信小程序-22实现管理员退出功能
2021/7/31 17:36:18
本文主要是介绍Python flask实战订餐系统微信小程序-22实现管理员退出功能,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
B站配套视频教程观看
攔截器检测登录状态决定是否登录成功
創建一個攔截器:
from application import app from flask import request,redirect from common.models.User import User from common.libs.user.UserService import UserService from common.libs.UrlManager import UrlManager @app.before_request def before_request(): path = request.path user_info =check_login() if not user_info: return redirect(UrlManager.buildUrl("/user/login")) return ''' 判断用户是否登录 ''' def check_login(): cookies = request.cookies auth_cookie = cookies[app.config['AUTH_COOKIE_NAME']] if app.config['AUTH_COOKIE_NAME'] in cookies else None #app.logger.info(auth_cookie) if auth_cookie is None: return False auth_info = auth_cookie.split("#") if len(auth_info) != 2: return False try: user_info = User.query.filter_by(uid=auth_info[1]).first() except Exception: return False if user_info is None: return False if auth_info[0] != UserService.geneAuthCode(user_info): return False return auth_info
运行后发现重定向很多次:
base_setting.py
配置過濾的url:
SERVER_PORT=8999 DEBUG = False SQLALCHEMY_ECHO = False AUTH_COOKIE_NAME = "mooc_food" # 過濾url IGNORE_URLS = [ r"^/user/login" ] IGNORE_CHECK_LOGIN_URLS = [ r"^/static", r"^/favicon.ico" ]
拦截器过滤掉不判断的url
from application import app from flask import request,redirect from common.models.User import User from common.libs.user.UserService import UserService from common.libs.UrlManager import UrlManager import re @app.before_request def before_request(): ignore_urls = app.config['IGNORE_URLS'] ignore_check_login_urls = app.config['IGNORE_CHECK_LOGIN_URLS'] path = request.path patten = re.compile(r'%s' % r"|".join(ignore_check_login_urls)) if patten.match(path): return user_info =check_login() patten = re.compile(r'%s' % r"|".join(ignore_urls)) if patten.match(path): return if not user_info: return redirect(UrlManager.buildUrl("/user/login")) return ''' 判断用户是否登录 ''' def check_login(): cookies = request.cookies auth_cookie = cookies[app.config['AUTH_COOKIE_NAME']] if app.config['AUTH_COOKIE_NAME'] in cookies else None #app.logger.info(auth_cookie) if auth_cookie is None: return False auth_info = auth_cookie.split("#") if len(auth_info) != 2: return False try: user_info = User.query.filter_by(uid=auth_info[1]).first() except Exception: return False if user_info is None: return False if auth_info[0] != UserService.geneAuthCode(user_info): return False return auth_info
此时运行 可以正确的跳转
登出功能实现
user.py
from flask import Blueprint,render_template,request,jsonify,make_response,redirect from common.libs.UrlManager import UrlManager @route_user.route( "/logout") def logout(): responce = make_response( redirect( UrlManager.buildUrl("/user/login") ) ) responce.delete_cookie(app.config["AUTH_COOKIE_NAME"]) return responce
运行时 退出账户时 可以看到删除了cookie
这篇关于Python flask实战订餐系统微信小程序-22实现管理员退出功能的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门
- 2024-11-14Python编程入门指南
- 2024-11-13Python基础教程
- 2024-11-12Python编程基础指南
- 2024-11-12Python基础编程教程
- 2024-11-08Python编程基础与实践示例
- 2024-11-07Python编程基础指南
- 2024-11-06Python编程基础入门指南
- 2024-11-06怎么使用python 计算两个GPS的距离功能-icode9专业技术文章分享