GAE 中的记住我选项 python

remember me option in GAE python

我正在从事一个项目,其中我正在研究 signup/login 模块。我已经成功地在 webapp2 python 中实现了会话。现在我想在登录时实现记住我的功能。我找不到任何可以帮助我的东西。我知道我必须设置会话年龄。但是我不知道怎么做。这是我的会话代码。

def dispatch(self):

    # Get a session store for this request.
    self.session_store = sessions.get_store(request=self.request)

    try:
        # Dispatch the request.
        webapp2.RequestHandler.dispatch(self)
    finally:
        # Save all sessions.
        self.session_store.save_sessions(self.response)

@webapp2.cached_property
def session(self):
    # Returns a session using the default cookie key.
    return self.session_store.get_session()

配置:

config = {}
config['webapp2_extras.sessions'] = {
    'secret_key': 'my-super-secret-key',
}

请帮助我。

首先以防您不知道会话和 cookie 之间的区别

What is a Cookie? A cookie is a small piece of text stored on a user's computer by their browser. Common uses for cookies are authentication, storing of site preferences, shopping cart items, and server session identification.

Each time the users' web browser interacts with a web server it will pass the cookie information to the web server. Only the cookies stored by the browser that relate to the domain in the requested URL will be sent to the server. This means that cookies that relate to www.example.com will not be sent to www.exampledomain.com.

In essence, a cookie is a great way of linking one page to the next for a user's interaction with a web site or web application.

.

What is a Session? A session can be defined as a server-side storage of information that is desired to persist throughout the user's interaction with the web site or web application.

Instead of storing large and constantly changing information via cookies in the user's browser, only a unique identifier is stored on the client side (called a "session id"). This session id is passed to the web server every time the browser makes an HTTP request (ie a page link or AJAX request). The web application pairs this session id with it's internal database and retrieves the stored variables for use by the requested page.

如果您想实现类似 "remember me" 的功能,您应该使用 cookie,因为存储在会话中的数据不是持久的。

用于在 webapp2 中设置和获取 cookie:

response.headers.add_header('Set-Cookie', 'remember_me=%s' % some_hash)

request.cookies.get('remember_me', '')

我强烈建议您阅读这篇article,它已经彻底解释了这些东西。