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>

ajax的post请求CSRF验证

当html页面{% raw xsrf_form_html()%}生成密串时,会自动将密串写入浏览器的一个cookie

用jquery.cookie插件获取CSRF验证的cookie

在用jquery的ajax请求将cookie传输到路由映射,就会自动通过验证

框架引擎

#!/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):
self.write("Hello, world") 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>
<script type="text/javascript" src='{{static_url("jquery.min.js")}}' charset="UTF-8"></script>
<script type="text/javascript" src='{{static_url("jquery.cookie.js")}}' charset="UTF-8"></script>
</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="button" value="提交" onclick="csrf();"/>
</form>
<script type="text/javascript">
function csrf() {
var hacsrf = $.cookie('_xsrf'); //jquery.cookie插件获取CSRF验证的cookie
$.ajax({ //jquery的ajax请求
type: 'POST',
url: '/dlu',
data: {'_xsrf':hacsrf}, //将CSRF验证的cookie值提交到路由映射,就会自动通过验证
success: function (response, status, xhr) {
//请求完成后自动执行
}
});
}
</script>
</body>
</html>

最新文章

  1. jsp_包含指令
  2. YTU 3020: 对称矩阵(数组)
  3. iOS 关于使用xib创建cell的两种初始化方式
  4. struct和class
  5. C++ Name Mangling 为什么不编码返回值参数
  6. linux中fork()函数
  7. RemoteViews的内部机制
  8. 半小时C语言题目
  9. Flex/AS3 base64指定字符编码
  10. springboot之集成mybatis mongo shiro druid redis jsp
  11. 打造自己的 JavaScript 武器库(转自SegmentFault公众号)
  12. .NET Core微服务之基于Steeltoe使用Hystrix熔断保护与监控
  13. python网络爬虫笔记(八)
  14. sql日期格式小应用 记录一下
  15. 「BZOJ 3645」小朋友与二叉树
  16. SNMP学习笔记之SNMPv3的报文格式以及基于USM的认证和加密过程
  17. 冷知识:excel 2013有多少行列
  18. Python学习---IO的异步[twisted模块]
  19. Redis 主从分离
  20. php获取经纬度

热门文章

  1. MySQL联合多表更新和删除
  2. sql中同一个Trigger里同时包含Insert,Update,Delete
  3. 无源码情况下直接修改jar里内容思路
  4. OSSSME - 开源软件助力中小企业发展
  5. Linux命令-压缩解压命令:gzip、gunzip
  6. php简单混淆类加密文件如何解密?
  7. ItelliJ基于Gradle创建及发布Web项目(三)
  8. POJ 2677 旅行商问题 双调dp或者费用流
  9. AngularJS中的$apply
  10. atitit.激活一个窗口总结 swing java .net php