base

class litestar.middleware.session.base.BaseBackendConfig

基类:ABC, Generic[BaseSessionBackendT]

会话中间件后端的配置。

key: str

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

Maximal age of the cookie before its invalidated.

scopes: Scopes = {ScopeType.HTTP, ScopeType.WEBSOCKET}

Scopes for the middleware - options are http and websocket with the default being both

path: str

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

Defaults to '/'.

domain: str | None

Domain for which the cookie is valid.

secure: bool

Https is required for the cookie.

httponly: bool

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

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

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

Defaults to lax.

exclude: str | list[str] | None

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

exclude_opt_key: str

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

property middleware: DefineMiddleware

使用此属性将配置插入到应用层之一的中间件列表中。 示例: `python from os import urandom from litestar import Litestar, Request, get from litestar.middleware.sessions.cookie_backend import CookieBackendConfig session_config = CookieBackendConfig(secret=urandom(16)) @get("/") def my_handler(request: Request) -> None: ... app = Litestar(route_handlers=[my_handler], middleware=[session_config.middleware]) ` Returns: 一个 DefineMiddleware 实例,其中包含以 self 作为 config 关键字参数的值。

class litestar.middleware.session.base.BaseSessionBackend

基类:ABC, Generic[ConfigT]

定义存储机制与 SessionMiddleware 应用程序之间接口的抽象会话后端。 此类是所有客户端和服务器端后端的基础类。

__init__(config: ConfigT) None

初始化 BaseSessionBackend Args: config: BaseBackendConfig 子类的实例

static serialize_data(data: ScopeSession, scope: Scope | None = None) bytes

将数据序列化为字节,以便在后端存储。 Args: data: 当前作用域的会话数据。 scope: 一个作用域(如果适用),用于从中提取序列化器。 Notes: - 序列化器将从 scope 中提取,如果无法提取,则回退到 default_serializer Returns: 序列化为字节的 data

static deserialize_data(data: Any) dict[str, Any]

将数据反序列化为字典,以便在应用作用域中使用。 Args: data: 待反序列化的数据 Returns: 反序列化后的字典数据

abstractmethod get_session_id(connection: ASGIConnection) str | None

尝试从连接 ScopeState 中获取会话 ID。如果不存在,则生成一个。 Args: connection: 包含作用域的原始 ASGIConnection Returns: 会话 ID 字符串,如果会话 ID 的概念不适用则返回 None。

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

将必要信息存储到发出的 Message 中 Args: scope_session: 当前要存储的会话 message: 发出的发送消息 connection: 包含作用域的原始 ASGIConnection Returns: None

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

从连接中加载会话数据,并将其作为字典返回,以便在当前应用作用域中使用。 Args: connection: 一个 ASGIConnection 实例 Returns: 会话数据 Notes: - 此方法不应修改连接的作用域。此方法返回的数据将由中间件存储到应用作用域中。

class litestar.middleware.session.base.SessionMiddleware

基类:AbstractMiddleware, Generic[BaseSessionBackendT]

用于存储会话数据的 Litestar 会话中间件。

__init__(app: ASGIApp, backend: BaseSessionBackendT) None

初始化 SessionMiddleware Args: app: 一个 ASGI 应用 backend: 一个用于存储和检索会话数据的 BaseSessionBackend 实例

create_send_wrapper(connection: ASGIConnection) Callable[[Message], Awaitable[None]]

为 ASGI send 函数创建一个包装器,用于处理在 outgoing 响应中设置 cookies。 Args: connection: ASGIConnection Returns: None