tornado.web.authenticated back button issue

谁说胖子不能爱 提交于 2019-12-06 12:34:06
A. Jesse Jiryu Davis

When you hit the back button after logout, your browser loads the previous page from cache. To prevent protected pages from being cached, you must set the following headers as described in this question

self.set_header('Cache-Control', 'no-cache, no-store, must-revalidate')
self.set_header('Pragma', 'no-cache')
self.set_header('Expires', '0')

You could put that in a decorator, something like:

def protected(method):
    @tornado.web.authenticated
    @functools.wraps(method)
    def wrapper(self, *args, **kwargs):
        self.set_header('Cache-Control', 'no-cache, no-store, must-revalidate')
        self.set_header('Pragma', 'no-cache')
        self.set_header('Expires', '0')
        return method(self, *args, **kwargs)
    return wrapper

Then decorate your protected pages with @protected instead of @tornado.web.authenticated.

Use the authenticated decorator on your method, that will make sure and redirect user to login page.

login_url should be configured part of the settings -

settings = dict({
    "login_url": "/#login",
    .....
 })

and decorator should be added like -

class Home(BaseHandler):
    @tornado.web.authenticated
    ...

Edit - User should be logged out, you can check by pressing F5, and it will redirect you to login page. If it shows you the content its just a cache issue and you might have to clear the cache explicitly.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!