tornado应用与安全

Cookie:

    设置cookie:self.setcookie(name,value,domain,expires,path,expires_days)

        name: cookie名

        value:cookie值

        domain: 提交cookie时匹配的域名

        path:提交cookie时匹配的路径

        expries:设置cookie的有效期,可以是时间戳整数/时间元组/datetime类型

        expires_days:设置cookie的有效天数,优先级低于exprires

    获取cookie:self.get_cookie(name,default=None)

        name:cookie值

        default:获取不到cookie值的时候返回的值

    清除名称为name的cookie:self.clear_cookie(name,path="/",domain=None)

    清除匹配path和domain的cookie:self.clear_all_cookies(path="/",domain=None)

    安全cookie:settings中添加:cookie_secret= base64.b64encode(uuid.uuid4().bytes)

                        设置安全cookie:set_secure_cookie(name,value,expire_days=30,version=None,**kwargs)

XSRF(跨域请求攻击):需要同源同域同协议

        设置XSCRF保护(4个方法设置): 1.settings 中添加:xsrf_cookie: True

                                    2.模板文件中添加 {% module xsrf_from_html() %}

                                    3.Ajax中添加 headers:{"X_XSRFToken": getCookie("_xsrf")}

                                    4.在路由中添加: 

                                            r("/(*+.)", index.StaticFileHandler, {"path":os.path.join(config.BASE_DIRS,"static/html"),

                                                                                                     default_filename:"index.html")})  

                                            类中继承tornado.web.StaticFileHandler,之后在__init__方法中添加self.xsrf_token

用户验证:

        *****@tornado.web.authentiicated装饰器

        第1步:重写get_current_user()方法用来编写验证逻辑,如果返回为True说明验证成功,否则验证失败。        

        第2步:在get/post方法中添加@tornado.web.authentiicated装饰器

        第3步:在settings中配置"login_url":"/login"用来验证失败时重定向的url

猜你喜欢

转载自blog.csdn.net/huolan__34/article/details/80791635