Flask 通过扩展来实现登录验证
1. flask扩展
说明:
flask的扩展类似于python中的装饰器,和Django中的process_request的方法也类似
测试代码
from flask import Flask,session,Session,flash,get_flashed_messages,redirect,render_template,request
app = Flask(__name__)
app.secret_key ='sdfsdfsdf' @app.before_request
def process_request1():
print('process_request1') @app.after_request
def process_response1(response):
print('process_response1')
return response @app.before_request
def process_request2():
print('process_request2') @app.after_request
def process_response2(response):
print('process_response2')
return response @app.route('/index')
def index():
print('index')
return 'Index' @app.route('/order')
def order():
print('order')
return 'order' @app.route('/test')
def test():
print('test')
return 'test' if __name__ == '__main__':
app.run()
在浏览器中访问index结果:
process_request1
process_request2
127.0.0.1 - - [04/Jan/2018 15:48:53] "GET / HTTP/1.1" 404 -
process_response2
process_response1
127.0.0.1 - - [04/Jan/2018 15:48:58] "GET /index HTTP/1.1" 200 -
process_request1
process_request2
index
process_response2
process_response1
实际使用案例:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:supery from flask import Flask,render_template,request,redirect,session app = Flask(__name__)
# 使用session需要配置secret_key
app.secret_key ='sdfsdfsdf' @app.route('/login',endpoint='login',methods=['POST','GET'])
def login():
if request.method == "GET":
return render_template('login.html')
else:
user = request.form.get('username')
pwd = request.form.get('password')
if user == 'alex' and pwd =='':
# 创建session信息
session['user_info'] = {'user':user,'userID':1}
return redirect('index')
return render_template('login.html') @app.before_request
def process_request():
if request.path == '/login':
return None
if not session.get('user_info'):
return redirect('login') @app.after_request
def process_response(response):
print('写扩展程序')
return response @app.route('/index',endpoint='index',methods=['GET','POST'])
def index():
return 'index' @app.route('/query',endpoint='query',methods=['GET','POST'])
def query():
return 'query' @app.route('/student',endpoint='student',methods=['GET','POST'])
def student():
return 'student' if __name__ == '__main__':
app.run()
扩展登录验证
Flask 通过扩展来实现登录验证的更多相关文章
- python装饰器 & flask 通过装饰器 实现 单点登录验证
首先介绍装饰器,以下是一段标注了特殊输出的代码.用于帮助理解装饰器的调用过程. import time def Decorator_one(arg1): info = "\033[1;31; ...
- Flask【第4篇】:用Flask的扩展实现的简单的页面登录
用flask的扩展实现的简单的页面登录 from flask import Flask,render_template,request,redirect,session app = Flask(__n ...
- Flask常用扩展(Extentions)
Flask常用扩展(Extentions) 官网;http://flask.pocoo.org/extensions/ 1.Flask-Script 说明: 一个flask终端运行的解析器 安装: ...
- nodeJS---express4+passport实现用户注册登录验证
网上有很多关于passport模块使用方法的介绍,不过基本上都是基于express3的,本文介绍在express4框架中使用passport模块. 前言 passport是一个功能单一,但非常强大的一 ...
- php 连接redis,并登录验证
环境: centos7 上安装了redis, 同时安装了php的redis扩展 yum install redis yum install php-pecl-redis redis服务端设置了登录密码 ...
- 搭建开发框架Express,实现Web网站登录验证
NodeJS学习笔记(一)——搭建开发框架Express,实现Web网站登录验证 JS是脚本语言,脚本语言都需要一个解析器才能运行.对于写在HTML页面里的JS,浏览器充当了解析器的角色.而对于需 ...
- PHP连接LDAP进行登录验证
基于安全性考虑,准备把PHP做的自动化平台加入ldap登录验证,具体做法如下: 了解背景: LDAP 的全称是"轻量级目录访问协议(Lightweight Directory Access ...
- djangorestframework-jwt自带的认证视图进行用户登录验证源代码学习
Django REST framework JWT djangorestframework-jwt自带的认证视图进行用户登录验证源代码学习 SECRET_KEY = '1)q(f8jrz^edwtr2 ...
- DJango简单的后台定义登录验证
第一步创建一个新的项目 APPLICATIONNAME : 表示创建子项目 第二步:找到主项目的url 进行 include 分发式url 简单的说,就是将app里边的url放在这里. 这里也可以找到 ...
随机推荐
- BZOJ4897 [Thu Summer Camp2016]成绩单 【dp】
题目链接 BZOJ4897 题解 发现我们付出的代价与区间长度无关,而与区间权值范围有关 离散化一下权值 我们设\(f[l][r][x][y]\)表示区间\([l,r]\)消到只剩权值在\([x,y] ...
- 学习操作Mac OS 之 使用brew安装软件
安装brew软件 安装方法: 在Mac中打开Termal: 输入命令: /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercont ...
- Vue语法笔记
Vue.js 的核心是一个允许采用简洁的模板语法来声明式的将数据渲染进 DOM: 事件监听:v-on 指令绑定一个事件监听器 缩写[@] v-on:click 用户输入,绑定数据:v-model ...
- GYM 101875 2018 USP-ICMC
3月自训 (1):10/12 A: 题意:每个人可以连边,最远连到第(i+k)%n个人,边权为这个人与另一个人连边距离,求生成一颗最大生成树的权值和是多少 题解:可以证明的是,我们每一个人都向接下来的 ...
- stout代码分析之四:Try类
stout的在异常捕获上遵循于谷歌类似的原则,不适用try...catch...,而是从函数返回值判断异常.Try类正是实现了这样的一个功能. 同Option一样,Try是一个模板类,每个类对象都有两 ...
- bzoj 4900 [CTSC2017]密钥 模拟+乱搞
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4900 #include<cstring> #include<cmath&g ...
- C语言中两个!!的作用
两个!是为了把非0值转换成1,而0值还是0. 因为C语言中,所有非0值都表示真. 所以!非0值 = 0,而!0 = 1.所以!!非0值 = 1,而!!0 = 0.例如:i=123 !i=0 !!i=1 ...
- git merge与git rebase
文章源:https://blog.csdn.net/wh_19910525/article/details/7554489 git merge是用来合并两个分支的. git merge b # 将b分 ...
- uva 10683 Fill
https://vjudge.net/problem/UVA-10603 题意: 倒水问题,输出最少的倒水量和目标水量 如果无解,目标水量就是尽可能接近给定点的目标水量,但不得大于给定的目标水量 推推 ...
- 51Nod 1344 走格子 | 贪心
Input示例 5 1 -2 -1 3 4 Output示例 2 贪心 #include <bits/stdc++.h> using namespace std; typedef long ...