Sessions
3 分钟阅读
原文:https://gobuffalo.io/documentation/request_handling/sessions/
Sessions 会话
An HTTP session is a non-persistent data storage, which is destroyed on browser shutdown (in the default browser configuration). It can be used to store flash messages, or any temporary user-specific data. Use cookies instead if you need a more persistent client side storage.
HTTP 会话是一种非持久性数据存储,在浏览器关闭时销毁(在默认浏览器配置中)。它可用于存储闪存消息或任何临时用户特定数据。如果您需要更持久性客户端存储,请改用 Cookie。
The session is available directly from the buffalo.Context
inside of a handler.
会话可直接从处理程序内部的 buffalo.Context
获得。
|
|
The Session Type 会话类型
The buffalo.Session
type has everything needed to work with a session during a request. Under the covers Buffalo uses the github.com/gorilla/sessions package for managing the session.
buffalo.Session
类型具有在请求期间使用会话所需的一切。在后台,Buffalo 使用 github.com/gorilla/sessions 包来管理会话。
|
|
Session Store 会话存储
By default Buffalo will setup a session store using sessions.CookieStore
.
默认情况下,Buffalo 将使用 sessions.CookieStore
设置会话存储。
This can be changed when setting up a new Buffalo application using the SessionStore
option:
这可以在使用 SessionStore
选项设置新 Buffalo 应用程序时更改:
|
|
The ENV variable SESSION_SECRET
should be set before running the application. If this is not set, you will see a warning in your logs that your session is not secured.
在运行应用程序之前,应设置 ENV 变量 SESSION_SECRET
。如果未设置此变量,您将在日志中看到警告,指出您的会话不安全。
For more information on this see the docs for buffalo.Options
.
有关此内容的更多信息,请参阅 buffalo.Options
的文档。
Storing Complex Types 存储复杂类型
It is generally considered not good practice to store complex types in a session. There are lots of reasons for this, but it is recommended to store the ID of a type, instead of the “whole” value.
通常认为在会话中存储复杂类型不是一个好习惯。原因有很多,但建议存储类型的 ID,而不是“整个”值。
Should you need to store a complex type, like a struct
you will first need to register the type with the encoding/gob
package.
如果您需要存储复杂类型,例如 struct
,则首先需要使用 encoding/gob
包注册该类型。
|
|
Saving a Session 保存会话
Buffalo automatically saves your session for you, so you don’t have to. If there is an error when saving the session, Buffalo will return an error through the normal error handling process.
Buffalo 会自动为您保存会话,因此您不必这样做。如果在保存会话时出错,Buffalo 将通过正常的错误处理过程返回错误。
Null Sessions for APIs API 的空会话
When building API servers the default cookie session store is undesirable. The sessions.Null
type is the recommended replacement for the default session store.
在构建 API 服务器时,默认的 cookie 会话存储是不可取的。 sessions.Null
类型是默认会话存储的推荐替代类型。
|
|
When running buffalo new
with the --api
flag the default session will be set to sessions.Null
.
使用 --api
标志运行 buffalo new
时,默认会话将设置为 sessions.Null
。