@@ -135,19 +135,20 @@ cookie是好的,被普遍使用。在tornado中,也提供对cookie的读写
135135` set_cookie() ` 和` get_cookie() ` 是默认提供的两个方法,但是它是明文不加密传输的。
136136
137137在index.py文件的IndexHandler类的post()方法中,当用户登录,验证用户名和密码后,将用户名和密码存入cookie,代码如下:
138- def post(self):
139- username = self.get_argument("username")
140- password = self.get_argument("password")
141- user_infos = mrd.select_table(table="users",column="* ",condition="username",value=username)
142- if user_infos:
143- db_pwd = user_infos[ 0] [ 2 ]
144- if db_pwd == password:
145- self.set_cookie(username,db_pwd) #设置cookie
146- self.write(username)
147- else:
148- self.write("your password was not right.")
149- else:
150- self.write("There is no thi user.")
138+
139+ def post(self):
140+ username = self.get_argument("username")
141+ password = self.get_argument("password")
142+ user_infos = mrd.select_table(table="users",column="*",condition="username",value=username)
143+ if user_infos:
144+ db_pwd = user_infos[0][2]
145+ if db_pwd == password:
146+ self.set_cookie(username,db_pwd) #设置cookie
147+ self.write(username)
148+ else:
149+ self.write("your password was not right.")
150+ else:
151+ self.write("There is no thi user.")
151152
152153上面代码中,较以前只增加了一句` self.set_cookie(username,db_pwd) ` ,在回到登录页面,等候之后就成为:
153154
@@ -193,7 +194,7 @@ tornado提供另外一种安全的方法:set_secure_cookie()和get_secure_cook
193194
194195 self.set_secure_cookie(username, db_pwd, httponly=True, secure=True)
195196
196- 要获取cookie,可以使用` self.set_secure_cookie (username) ` 方法,将这句放在user.py中某个适合的位置,并且可以用print语句打印出结果,就能看到变量username对应的cookie了。这时候已经不是那个“密”过的,是明文显示。
197+ 要获取cookie,可以使用` self.get_secure_cookie (username) ` 方法,将这句放在user.py中某个适合的位置,并且可以用print语句打印出结果,就能看到变量username对应的cookie了。这时候已经不是那个“密”过的,是明文显示。
197198
198199用这样的方法,浏览器通过SSL连接传递cookie,能够在一定程度上防范跨站脚本攻击。
199200
@@ -282,4 +283,4 @@ XSRF的含义是Cross-site request forgery,即跨站请求伪造,也称之
282283
283284[ 总目录] ( ./index.md )   ;  ;  ; |  ;  ;  ; [ 上节:用tornado做网站(4)] ( ./306.md )   ;  ;  ; |  ;  ;  ; [ 下节:用tornado做网站(6)] ( ./308.md )
284285
285- 如果你认为有必要打赏我,请通过支付宝:
** [email protected] ** ,不胜感激。
286+ 如果你认为有必要打赏我,请通过支付宝:
** [email protected] ** ,不胜感激。
0 commit comments