##########################################################################################
#########Python script: 创建web app. 包含3个页面的处理:主页面,申请页面,审批页面########
##########################################################################################
from flask import Flask
from flask_wtf import FlaskForm
from wtforms import TextField, SubmitField,IntegerField, BooleanField,SelectField,DecimalField,TextAreaField,PasswordField
f_instance=Flask(__name__)
@f_instance.route('/')
def index():
return render_template("home.html" )
@f_instance.route('/apply',methods=['GET','POST'])
def apply_input():
form=applyform()
return render_template("apply_input.html",form=form)
@f_instance.route('/approve',methods=['GET','POST'])
def approve_view():
form=approveform()
if request.method=='GET':
apply=request.args
else
apply=request.form
return render_template("approve_view.html", form=form,apply=apply)
f_instance.config['SECRET_KEY']='flasksecret'
if __name__=="__main__":
f_instance.run(debug=True)
####################################################################
##########htlm page: 包含3个页面:主页面,申请页面,审批页面########
####################################################################
########## 主页面 ########
<!DOCTYPE html>
<html lang="en">
<body>
<h1>Welcome!</h1>
<a href={{url_for("apply_input")}}>Submit your appllication please.</a>
</body>
</html>
########## 申请页面 ########
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<form action={{url_for('approve_view')}} method="GET">
{{form.hidden_tag()}}
{{form.apply.label}}{{form.apply}}
{{form.submit}}
</form>
</body>
</html>
########## 审批页面 ########
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
{% for key, value in apply.items() %}
{{key}}:{{value}}<br>
{% endfor %}
<form action={{url_for('index')}}>
{{form.submit}}
</form>
</body>
</html>
上面显示出Flask通过在request中添加参数csrf_token来预防csrf攻击。GET方法中该参数出现在url中,POST方法中该参数出现在form中。
CSRF介绍:https://blog.csdn.net/ru_li/article/details/53301708
CSRF攻击只是盗用用户的身份访问服务器执行非法更新操作,比如冒充用户对银行账户转账,该攻击无法知道用户的身份信息,对于攻击返回的信息也无法查看,所以对于只读操作无效。