简体   繁体   English

Flask Sessions 是如何工作的?

[英]How does Flask Sessions work?

I am very curious of how Flask sessions works, especially how it stores the information between server restarts (quote me if I am wrong).我很好奇 Flask 会话是如何工作的,尤其是它如何存储服务器重启之间的信息(如果我错了,请引用我)。 I understand that you have to set a unique app.secret_key so people cannot decrypt the session and modify the cookie in any way.我知道您必须设置一个唯一的app.secret_key以便人们无法以任何方式解密会话和修改 cookie。 Because the cookie for the session is just random generated letter and numbers, would this mean that the id is paired up with the id from the server side, and that the server stores the sessions?因为会话的 cookie 只是随机生成的字母和数字,这是否意味着 id 与服务器端的 id 配对,并且服务器存储会话? If that is so, how would Flask remember the sessions between restarts?如果是这样,Flask 如何记住重启之间的会话? If not, how does Flask know to decrypt the session cookie?如果没有,Flask 如何知道解密会话 cookie?

The default session is implemented using secure cookies.默认会话是使用安全 cookie 实现的。 Cookies are persisted by the client's browser, Flask doesn't do anything in that regard. Cookie 由客户端的浏览器保存,Flask 在这方面不做任何事情。 Each client has a unique session cookie, which it sends to the Flask server with each request.每个客户端都有一个唯一的会话 cookie,它会随每个请求发送到 Flask 服务器。

The cookie is secure not encrypted , it does not prevent anyone with the cookie from viewing the data, only from modifying it. cookie 是安全的,没有加密,它不会阻止任何拥有 cookie 的人查看数据,只会阻止修改它。 Flask signs the data with the app's secret key when sending it, and unsigns it with the same key when reading it. Flask 在发送数据时使用应用程序的密钥对数据进行签名,并在读取时使用相同的密钥取消签名。

Flask does not add anything to the session. Flask 不会向会话添加任何内容。 There is no session id, the browser just sends the session cookie during each request, and Flask reads it.没有 session id,浏览器只是在每次请求期间发送 session cookie,Flask 读取它。

You can write your own session interface to change how the session works.您可以编写自己的会话界面来更改会话的工作方式。 See extensions such as Flask-Session查看扩展,例如Flask-Session

Flask generates the session cookie using its sister project, It's Dangerous . Flask 使用其姊妹项目It's Dangerous生成会话 cookie。 The project page has a great overview of how It's Dangerous works, but at a high level:项目页面对 It's Dangerous 的工作原理有一个很好的概述,但在高层次上:

  • the data in your session (set by session["username"] = "EndenDragon" ) is serialized into a JSON string ( {"username":"EndenDragon"} )会话中的数据(由session["username"] = "EndenDragon" )被序列化为 JSON 字符串( {"username":"EndenDragon"}
  • that string is encoded using base64 encoding ( eyJ1c2VybmFtZSI6IkVuZGVuRHJhZ29uIn0= ).该字符串使用 base64 编码( eyJ1c2VybmFtZSI6IkVuZGVuRHJhZ29uIn0= )进行编码。 This makes it safe for use cases like an email verification link, where it might be appended at the end of the link.这使得它对于电子邮件验证链接等用例是安全的,它可能会附加在链接的末尾。
  • the base64 encoded data has a "." base64 编码的数据有一个“.” appended to it.附加到它。 The timestamp when the session was created is base64 encoded and appended to it.创建会话时的时间戳是 base64 编码的并附加到它上面。
  • A cryptographic signature is generated for the session + timestamp, using your secret key.使用您的密钥为会话 + 时间戳生成加密签名。 The signature to the session value after a "." “.”之后的会话值签名。 as well.以及。

The value is then sent to the browser as a Cookie in the response.然后将该值作为响应中的 Cookie 发送到浏览器

The values in the session can be read by end users (and over insecure connections).最终用户(以及通过不安全的连接)可以读取会话中的值。 The server can verify cookies it receives hasn't been tampered with, without storing anything on its end.服务器可以验证它收到的 cookie 未被篡改,而无需在其端存储任何内容。 It just recomputes the signature from the session + timestamp part of the session value, and makes sure it matches the signature at the end of the session value.它只是从会话值的会话 + 时间戳部分重新计算签名,并确保它与会话值末尾的签名匹配。

The inclusion of the timestamp enables Flask to enforce the expiration date of permanent sessions on the server side, in addition to setting an expiration date on the client side .除了在客户端设置到期日期之外,时间戳的包含使 Flask 能够在服务器端强制执行permanent会话的到期日期

Addendum附录

Users can easily read the values in the session by decoding the first part of the session value.用户可以通过解码会话值的第一部分来轻松读取会话中的值。 Go to the "Storage" or "Application" tab in developer tools, look for the "session" cookie, copy the value up to the first period, and run btoa(<session-part>) in the Console.转到开发人员工具中的“存储”或“应用程序”选项卡,查找"session" cookie,将值复制到第一个句点,然后在控制台中运行btoa(<session-part>)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM