server_side

members:

class litestar.middleware.session.server_side.ServerSideSessionBackend

基类:BaseSessionBackend[ServerSideSessionConfig]

服务器端后端的基类。 实现了 BaseSessionBackend,并定义了一个接口,子类可以实现该接口以促进会话数据的存储。

__init__(config: ServerSideSessionConfig) None

初始化 ServerSideSessionBackend Args: config: ServerSideSessionConfig 的子类

async get(session_id: str, store: Store) bytes | None

获取与 session_id 关联的数据。 Args: session_id: 会话 ID store: 从中检索会话数据的存储区 Returns: 会话数据(如果存在),否则为 None

async set(session_id: str, data: bytes, store: Store) None

data 存储在 session_id 下以供后续检索。 如果 session_id 已有关联数据,则用 data 替换它,并重置其过期时间。 Args: session_id: 会话 ID data: 序列化的会话数据 store: 用于保存会话数据的存储后端 Returns: None

async delete(session_id: str, store: Store) None

删除与 session_id 关联的数据。如果不存在该 session-ID,则静默失败。 Args: session_id: session-ID store: 从中删除会话数据的存储 Returns: None

get_session_id(connection: ASGIConnection) str

尝试从连接中获取会话 ID。如果不存在,则生成一个。 如果 Cookie 中已存在会话 ID,则返回该 ID。 如果 Cookie 中没有 ID,但连接状态中存在 ID,则说明会话已存在但尚未返回给用户。 否则,必须创建新会话。 Args: connection: 包含 scope 的原始 ASGIConnection Returns: 会话 ID 字符串,如果会话 ID 的概念不适用则返回 None。

generate_session_id() str

生成一个新的会话 ID,包含 n = session_id_bytes 个随机字节。 Returns: 一个会话 ID

async store_in_message(scope_session: ScopeSession, message: Message, connection: ASGIConnection) None

通过在 outgoing Message 中设置包含 session-ID 的 cookie,来存储必要的信息。 如果 session 为空,将设置一个 null-cookie。否则,序列化后的数据将使用 set 存储在当前 session-id 下。如果不存在 session-ID,将使用 generate_session_id 生成一个新的 ID。 Args: scope_session: 要存储的当前 session message: outgoing 发送消息 connection: 包含 scope 的原始 ASGIConnection Returns: None

async load_from_connection(connection: ASGIConnection) dict[str, Any]

从连接中加载会话数据,并将其作为字典返回,以便在当前应用作用域中使用。 会话 ID 将从键名在 BaseBackendConfig.key 中设置的 Cookie 中获取。如果找到 Cookie,其值将用作会话 ID,并使用 get 加载与该 ID 关联的数据。 如果未找到 Cookie 或存储中未加载到任何数据,则返回一个空字典。 Args: connection: 一个 ASGIConnection 实例 Returns: 当前会话数据

class litestar.middleware.session.server_side.ServerSideSessionConfig

基类:BaseBackendConfig[ServerSideSessionBackend]

服务器端后端的基配置。

session_id_bytes: int = 32

Number of bytes used to generate a random session-ID.

renew_on_access: bool = False

Renew expiry times of sessions when they're being accessed

key: str = 'session'

Key to use for the cookie inside the header, e.g. session=<data> where session is the cookie key and <data> is the session data.

备注

  • If a session cookie exceeds 4KB in size it is split. In this case the key will be of the format session-{segment number}.

max_age: int = 1209600

Maximal age of the cookie before its invalidated.

path: str = '/'

Path fragment that must exist in the request url for the cookie to be valid.

Defaults to '/'.

domain: str | None = None

Domain for which the cookie is valid.

__init__(session_id_bytes: int = 32, renew_on_access: bool = False, key: str = 'session', max_age: int = 1209600, scopes: set[~typing.Literal[ScopeType.HTTP, ScopeType.WEBSOCKET]]=<factory>, path: str = '/', domain: str | None = None, secure: bool = False, httponly: bool = True, samesite: Literal['lax', 'strict', 'none']='lax', exclude: str | list[str] | None = None, exclude_opt_key: str = 'skip_session', store: str = 'sessions') None
secure: bool = False

Https is required for the cookie.

httponly: bool = True

Forbids javascript to access the cookie via 'Document.cookie'.

samesite: Literal['lax', 'strict', 'none'] = 'lax'

Controls whether or not a cookie is sent with cross-site requests. Defaults to lax.

exclude: str | list[str] | None = None

A pattern or list of patterns to skip in the session middleware.

exclude_opt_key: str = 'skip_session'

An identifier to use on routes to disable the session middleware for a particular route.

store: str = 'sessions'

Name of the Store to use

get_store_from_app(app: Litestar) Store

Litestar 实例中获取在 store 中定义的存储。