码迷,mamicode.com
首页 > 其他好文 > 详细

第二百七十一节,Tornado框架-CSRF防止跨站post请求伪造

时间:2017-05-26 00:50:26      阅读:343      评论:0      收藏:0      [点我收藏+]

标签:eth   验证码   创建   logs   raw   char   参数   允许   表单   

Tornado框架-CSRF防止跨站post请求伪造

CSRF是什么

CSRF是用来在post请求时做请求验证的,防止跨站post请求伪造

当用户访问一个表单页面时,会自动在表单添加一个隐藏的input标签,name="_xsrf",value="等于一个密串"

当用户post请求提交数据时,会将_xsrf的密串提交到后台,后台会判断这个密串存在就允许提交数据,否则不允许提交

进行CSRF验证只需要两步

 

1、在框架配置字典里开启CSRF验证,开启后会自动接收post传来的_xsrf密串判断是否合法

"xsrf_cookies":True 开启CSRF验证,开启后会自动接收post传来的_xsrf密串判断是否合法

2、在HTML页面,用模板语言接收添加CSRF密串

{% raw xsrf_form_html()%}当用户访问一个表单页面时,会自动在表单添加一个隐藏的input标签,name="_xsrf",value="等于一个密串"

框架引擎

#!/usr/bin/env python
#coding:utf-8

import tornado.ioloop
import tornado.web                                              #导入tornado模块下的web文件
import session_lei                                              #导入session模块

class dluHandler(tornado.web.RequestHandler):
    def get(self):
        self.render("dlu.html")
    def post(self):
        pass


settings = {                                            #html文件归类配置,设置一个字典
    "template_path":"views",                            #键为template_path固定的,值为要存放HTML的文件夹名称
    "static_path":"statics",                            #键为static_path固定的,值为要存放js和css的文件夹名称
    "xsrf_cookies":True
}

#路由映射
application = tornado.web.Application([                 #创建一个变量等于tornado.web下的Application方法
    (r"/dlu", dluHandler),
],**settings)                                           #将html文件归类配置字典,写在路由映射的第二个参数里

if __name__ == "__main__":
    #内部socket运行起来
    application.listen(8888)                            #设置端口
    tornado.ioloop.IOLoop.instance().start()

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>请登录</h1>
    <form method="post" action="/dlu">
        {% raw xsrf_form_html()%}
        用户名<input type="text" name="yhm"/><br/><br/>
        密码<input type="text" name="mim"/><br/><br/>
        验证码<input type="text" name="yanzhma"/><br/><br/>
        <input type="submit" value="提交"/>
    </form>
</body>
</html>

技术分享

 

第二百七十一节,Tornado框架-CSRF防止跨站post请求伪造

标签:eth   验证码   创建   logs   raw   char   参数   允许   表单   

原文地址:http://www.cnblogs.com/adc8868/p/6906342.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!