一、说明
用户验证是指在收到用户请求后进行处理前先判断用户的认证状态(如登陆状态),若通过验证则正常处理,否则强制用户跳转至认证页面(如登陆页面)
二、authenticated装饰器
说明
为了使用Tornado的认证功能,我们需要对登录用户标记具体的处理函数。我们可以使用@tornado.web.authenticated装饰器完成它。当我们使用这个装饰器包裹一个处理方法时,Tornado将确保这个方法的主体只有在合法的用户被发现时才会调用
示例
代码语言:javascript复制<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ProfileHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
<span class="hljs-meta"> @tornado.web.authenticated</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
self.write(<span class="hljs-string">"这是我的个人主页。"</span>)
三、get_current_user()方法
说明:
装饰器@tornado.web.authenticated的判断执行依赖于请求处理类中的self.current_user属性,如果current_user值为假(None、False、0、""等),任何GET或HEAD请求都将把访客重定向到应用设置中login_url指定的URL,而非法用户的POST请求将返回一个带有403(Forbidden)状态的HTTP响应
在获取self.current_user属性的时候,tornado会调用get_current_user()方法来返回current_user的值。也就是说,我们验证用户的逻辑应写在get_current_user()方法中,若该方法返回非假值则验证通过,否则验证失败
示例
代码语言:javascript复制<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ProfileHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_current_user</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-string">"""在此完成用户的认证逻辑"""</span>
user_name = self.get_argument(<span class="hljs-string">"name"</span>, <span class="hljs-keyword">None</span>)
<span class="hljs-keyword">return</span> user_name
<span class="hljs-meta"> @tornado.web.authenticated</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
self.write(<span class="hljs-string">"这是我的个人主页。"</span>)
四、login_url 设置
说明
当用户验证失败时,将用户重定向到login_url上,所以我们还需要在Application中配置login_url
示例
代码语言:javascript复制<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">LoginHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-string">"""在此返回登陆页面"""</span>
self.write(<span class="hljs-string">"登陆页面"</span>)
app = tornado.web.Application(
[
(<span class="hljs-string">r"/"</span>, IndexHandler),
(<span class="hljs-string">r"/profile"</span>, ProfileHandler),
(<span class="hljs-string">r"/login"</span>, LoginHandler),
],
<span class="hljs-string">"login_url"</span>:<span class="hljs-string">"/login"</span>
)
结果:
next 参数跳转
在login_url后面补充的next参数就是记录的跳转至登录页面前的所在位置,所以我们可以使用next参数来完成登陆后的跳转
示例
修改登陆逻辑
代码语言:javascript复制<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">LoginHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-string">"""登陆处理,完成登陆后跳转回前一页面"""</span>
next = self.get_argument(<span class="hljs-string">"next"</span>, <span class="hljs-string">"/"</span>)
self.redirect(next <span class="hljs-string">"?name=logined"</span>)
用户访问的网址顺序:
完整代码
代码语言:javascript复制<span class="hljs-keyword">import</span> tornado.web,os
<span class="hljs-keyword">import</span> tornado.ioloop
<span class="hljs-keyword">import</span> tornado.httpserver
<span class="hljs-keyword">from</span> tornado.web <span class="hljs-keyword">import</span> RequestHandler
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ProfileHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_current_user</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-string">"""在此完成用户的认证逻辑"""</span>
user_name = self.get_argument(<span class="hljs-string">"name"</span>, <span class="hljs-keyword">None</span>)
<span class="hljs-keyword">return</span> user_name
<span class="hljs-meta"> @tornado.web.authenticated</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-comment"># 获取当前用户</span>
print(self.current_user)
self.write(<span class="hljs-string">"这是我的个人主页。"</span>)
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">LoginHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-string">"""在此返回登陆页面"""</span>
self.write(<span class="hljs-string">"登陆页面"</span>)
<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">"__main__"</span>:
app = tornado.web.Application([
(<span class="hljs-string">r"/profile"</span>, ProfileHandler),
(<span class="hljs-string">r"/login"</span>, LoginHandler),
],
login_url=<span class="hljs-string">"/login"</span>,
debug=<span class="hljs-keyword">True</span>,
autoreload=<span class="hljs-keyword">True</span>,
template_path=os.path.join(os.path.dirname(__file__),<span class="hljs-string">'templates'</span>),
)
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(<span class="hljs-number">8000</span>)
tornado.ioloop.IOLoop.current().start()