tornado安全应用之cookie

目前大多数服务器判断用户是否登录一般通过session机制,Tornado 通过 set_secure_cookie 和 get_secure_cookie 方法直接支持了这种功能。原理类似于session,只不过session是服务器自动生成一个sessionID存储在cookie里,而tornado需要我们手工设cookie。然后通过self.current_user的重载函数就可以实现用户的验证。

首先来看下set_secure_cookie和get_secure_cookie的使用方法:

Tornado的set_secure_cookie()和get_secure_cookie()函数发送和取得浏览器的cookies,以防范浏览器中的恶意修改。为了使用这些函数,你必须在应用的构造函数中指定cookie_secret参数。让我们来看一个简单的例子。这个例子将统计浏览器中页面被加载的次数。如果没有设置cookie或者cookie被篡改了,应用将设置一个值为1的新cookie,否则应用将从cookie中读到的值加1

class MainHandler(tornado.web.RequestHandler):

    def get(self, *args, **kwargs):

        cookie=self.get_secure_cookie("count")   

        print cookie

        count=int(cookie) + 1 if cookie else 1

        countstring="1 time" if count == 1 else "%d times" % count

        self.set_secure_cookie("count",str(count))

        content='you have viewed this page %s times' % countstring

        print content

        self.write(content)

def server_function():

    cookie='bZJc2sWbQLKos6GkHn/VB9oXwQt8S0R0kRvJ5/xJ89E='

    tornado.options.parse_command_line()

    app = tornado.web.Application(handlers=[(r"/", MainHandler),(r"/show",showHandler),(r"/index1",indexHandler_temp)],template_path=os.path.join(os.path.dirname(__file__),"template"),

                                  static_path=os.path.join(os.path.dirname(__file__),"static"),ui_modules={'Book':HelloModule},cookie_secret=cookie)

    http_server = tornado.httpserver.HTTPServer(app)

    http_server.listen(options.port,address='127.0.0.1')

tornado.ioloop.IOLoop.instance().start()

首先来运行看下结果:

在浏览器中输入url,可以看到显示的是浏览了一次网站

来看下服务器后端的打印。首先通过self.get_secure_cookie("count")得到的的cookie值是None,也就是空的。此时访问次数的赋值是count=int(cookie) + 1 if cookie else 1,在cookie为空的时候,count=1, 否则是int(cookie)+1. 最终通过self.set_secure_cookie("count",str(count))将更新好的count值以及cookie值再带给服务器。cookie值是通过在Application中设置的cookie='bZJc2sWbQLKos6GkHn/VB9oXwQt8S0R0kRvJ5/xJ89E='

/usr/bin/python2.7 /home/zhf/py_prj/tornada_try.py

None

you have viewed this page 1 time times

[I 180103 10:50:10 web:2063] 200 GET / (127.0.0.1) 0.92ms

不停的刷新浏览器,可以看到对应的count值也在不断的积累,通过这个我们就能监控到访问的网站的次数。

/usr/bin/python2.7 /home/zhf/py_prj/tornada_try.py

None

you have viewed this page 1 time times

[I 180103 10:50:10 web:2063] 200 GET / (127.0.0.1) 0.92ms

[I 180103 10:56:59 web:2063] 200 GET / (127.0.0.1) 0.62ms

1

you have viewed this page 2 times times

[I 180103 10:57:09 web:2063] 200 GET / (127.0.0.1) 0.57ms

2

you have viewed this page 3 times times

3

you have viewed this page 4 times times

[I 180103 10:57:11 web:2063] 200 GET / (127.0.0.1) 2.03ms

4

[I 180103 10:57:12 web:2063] 200 GET / (127.0.0.1) 2.08ms

you have viewed this page 5 times times

[I 180103 10:57:13 web:2063] 200 GET / (127.0.0.1) 2.06ms

5

you have viewed this page 6 times times

6

you have viewed this page 7 times times

[I 180103 10:57:14 web:2063] 200 GET / (127.0.0.1) 2.24ms

7

you have viewed this page 8 times times

[I 180103 10:57:14 web:2063] 200 GET / (127.0.0.1) 2.31ms

 

在浏览器也可以查看到服务器发下来的cookie值,其中携带了count

但是如果我们在浏览器上删除了这个cookie值,我们来看下运行的结果如何:

我们看到访问的次数又变成1了。

再看下服务器端的打印:之前访问的次数是11次,由于在浏览器上删除了cookie值,因此获得的count值为None,因此又重新计数。

10

you have viewed this page 11 times times

[I 180103 11:03:57 web:2063] 200 GET / (127.0.0.1) 0.59ms

[W 180103 11:03:57 web:2063] 404 GET /favicon.ico (127.0.0.1) 1.24ms

None

you have viewed this page 1 time times

[I 180103 11:04:19 web:2063] 200 GET / (127.0.0.1) 0.44ms

[W 180103 11:04:19 web:2063] 404 GET /favicon.ico (127.0.0.1) 0.50ms

 

同样的如果在MainHandler中添加清除cookie的命令self.clear_cookie("count")。那么浏览器的访问次数将永远是1

class MainHandler(tornado.web.RequestHandler):

    def get(self, *args, **kwargs):

        cookie=self.get_secure_cookie("count")

        print cookie

        count=int(cookie) + 1 if cookie else 1

        countstring="1 time" if count == 1 else "%d times" % count

        self.set_secure_cookie("count",str(count))

        content='you have viewed this page %s times' % countstring

        print content

        self.write(content)

        self.clear_cookie("count")

运行结果:

None

you have viewed this page 1 time times

[I 180103 11:08:50 web:2063] 200 GET / (127.0.0.1) 2.12ms

None

you have viewed this page 1 time times

[I 180103 11:08:51 web:2063] 304 GET / (127.0.0.1) 1.62ms

[I 180103 11:08:51 web:2063] 304 GET / (127.0.0.1) 1.94ms

None

you have viewed this page 1 time times

Tornado的cookie功能依附于Python内建的Cookie模块。因此,我们可以利用它所提供的一些安全功能。这些安全属性是HTTP cookie规范的一部分,并在它可能是如何暴露其值给它连接的服务器和它运行的脚本方面给予浏览器指导。比如,我们可以通过只允许SSL连接的方式减少cookie值在网络中被截获的可能性。我们也可以让浏览器对JavaScript隐藏cookie值。

为cookie设置secure属性来指示浏览器只通过SSL连接传递cookie。(这可能会产生一些困扰,但这不是Tornado的安全cookies,更精确的说那种方法应该被称为签名cookies。)从Python 2.6版本开始,Cookie对象还提供了一个httponly属性。包括这个属性指示浏览器对于JavaScript不可访问cookie,这可以防范来自读取cookie值的跨站脚本攻击。

为了开启这些功能,你可以向set_cookieset_secure_cookie方法传递关键字参数。比如,一个安全的HTTP-only cookie(不是Tornado的签名cookie)可以调用self.set_cookie('foo', 'bar', httponly=True, secure=True)发送。

有兴趣的可以搭建一个https网站去尝试下。

 

转载于:https://www.cnblogs.com/zhanghongfeng/p/8182909.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值