跨站请求伪造CSRF

开启xsrf(就是叫法不一样和csrf一样),'xsrf_cookies':True 

settings = {
    'template_path':'template',
    'static_path':'static',
    'static_path_prefix':'/static/',
    'xsrf_cookies':True,
}

在post表单中增加csrf认证

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="{{static_url("commons.css")}}" rel="stylesheet" />
</head>
<body>
<h1>index.html</h1>
<h1>{{ name }}</h1>
<form action="/index" method="post">
{% module xsrf_form_html() %} <p>user:<input type="text"/></p> <p>password:<input type="password" /> </p> <input type="submit" value="submit" /> </form> </body> </html>

网站请求效果(表单中没有增加认证token):

 Tornado(二)

 

加上token

Tornado(二)

 

Tornado(二)

 

AJAX方法

官方提供:

获取cookie的token信息  args._xsrf = getCookie("_xsrf");
function getCookie(name) {
    var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
    return r ? r[1] : undefined;
}

jQuery.postJSON = function(url, args, callback) {
    args._xsrf = getCookie("_xsrf");
    $.ajax({url: url, data: $.param(args), dataType: "text", type: "POST",
        success: function(response) {
        callback(eval("(" + response + ")"));
    }});
};

 

 

 

UI(自定义标签)

自定义有两种方式:uimethods(方法)和uimodule(模块)

创建玩一下uimethods,新创建一个py文件

#_*_coding:utf-8_*_


def abcd(self):
    return '自定义uimethod方法'
uimethods

相关文章:

  • 2021-04-26
  • 2021-09-20
  • 2021-11-20
  • 2022-12-23
  • 2022-12-23
  • 2021-04-06
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-12
  • 2021-11-07
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案