RequestHandler 类的 get_current_user() 方法覆写无效 - V2EX
roychan

RequestHandler 类的 get_current_user() 方法覆写无效

  •  
  •   roychan Jul 27, 2015 4992 views
    This topic created in 3968 days ago, the information mentioned may be changed or developed.

    刚刚开始用 Tornado。

    在用户认证时,我覆写了 BaseHandler,以便使用内置的 current_user() 方法与 is_authenticated 修饰器。

    class BaseHandler(tornado.web.RequestHandler): def get_current_user(self): return self.get_secure_cookie('user') 

    在某步中,我加了判断 if self.current_user then xxx,此时即便登录了也无法执行 xxx,如果我把判断写成 if self.get_current_user then xxx,便能成功确认登录,xxx 就执行了。

    请问这可能是什么问题?

    Supplement 1    Jul 29, 2015
    谢谢各位,问题解决了,下次我会多测试再来提问。
    12 replies    205-07-29 00:39:00 +08:00
    gx
        1
    gx  
       Jul 28, 2015   1
    `self.current_user`可以当作是`get_current_user`的一个缓存,如果你复写的`get_current_user`在第一次返回None的话,之后使用`self.current_user`是直接读取缓存的结果。

    解决方案:
    当你做登陆操作时,除了`set_secure_cookie`之外,可以给current_user赋值:
    ```
    user = {'id': "v2ext208772", 'name': "roychan"}
    self.current_user = user
    ```
    roychan
        2
    roychan  
    OP
       Jul 28, 2015   1
    @gx
    那么是否退出的时候要再将这个值设置为 None?
    这样是不是没有用到 Tornado 内置的 auth 功能了?
    mulog
        3
    mulog  
       Jul 28, 2015   1
    你可以看看你说的那两个东西的实现
    https://github.com/tornadoweb/tornado/blob/master/tornado/web.py

    如果你的 get_current_user() 真的就只是读 cookie 的话 我觉得这两的返回会是一致的呀
    roychan
        4
    roychan  
    OP
       Jul 28, 2015
    @mulog

    正如一楼所说,get_current_user 方法在调用 current_user 方法时会被调用一次,然后将自身的 _current_user 属性设置为当前用户,以后直接调用自身属性,此为「缓存」。

    假设:我现在要求访问登录页面也需要登录。那么在访问登录页面之前,需要调用一次 current_user 这个方法(也是属性)来验证是否登录。如果用户未登录,此时 current_user 值便成为了 None,即便登录后,使用 current_user 属性来验证的话,也依旧是 NoneType。

    我想在不覆写 Tornado 源代码的情况下最大化实现其内置功能。如果还要自己加一个属性,那么我感觉设置安全 cookie 的验证步骤可以省去了(除了用来保持登录)。
    roychan
        5
    roychan  
    OP
       Jul 28, 2015
    @mulog

    按照官方文档来看的话,只需要覆写 get_current_user 方法就可以了,不知道为什么不行。
    siteshen
        6
    siteshen  
       Jul 28, 2015 via iPhone
    POST /login 后应该返回302,而不是继续停留在 /login 页面。
    siteshen
        7
    siteshen  
       Jul 28, 2015 via iPhone
    @siteshen logout 同理,这样就能保证登录状态在整个请求中一致。
    roychan
        8
    roychan  
    OP
       Jul 28, 2015
    @siteshen 假设我手动访问登录页面呢?
    mulog
        9
    mulog  
       Jul 28, 2015
    @roychan
    可是缓存也只是在同一个请求之内啊 你访问另一个页面/刷新之后哪来的缓存。。
    roychan
        10
    roychan  
    OP
       Jul 28, 2015
    那如何解决手动访问登录页的问题呢…
    gx
        11
    gx  
       Jul 28, 2015
    @roychan 这个_current_user存在于内存当中,准确的说是在一个http_request生命周期内有效。

    只要你重置了cookie,再次开始一个request都是current_user都是None;

    一般情况下,登入和登出都会301/302,当然如果是单页面应用可以再次请求状态。
    siteshen
        12
    siteshen  
       Jul 29, 2015
    @roychan 登录页面的作用仅仅是用于浏览器告诉服务器自己是谁,“已经登录”的意思是服务器知道你是谁了,登录过的用户服务器认为他应该到哪就返回 302 跳转到哪。

    def login(request, next_url=None):
    if self.current_user:
    redirect(next_url or '/dashboard')
    About     Help     Advertise     Blog     API     FAQ     Solana     5469 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 66ms UTC 08:25 PVG 16:25 LAX 01:25 JFK 04:25
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86