logo
#[non_exhaustive]
pub struct CookieSessionStore;
Available on crate feature cookie-session only.
Expand description

Use the session key, stored in the session cookie, as storage backend for the session state.

use actix_web::{cookie::Key, web, App, HttpServer, HttpResponse, Error};
use actix_session::{SessionMiddleware, storage::CookieSessionStore};

// The secret key would usually be read from a configuration file/environment variables.
fn get_secret_key() -> Key {
    // [...]
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let secret_key = get_secret_key();
    HttpServer::new(move ||
            App::new()
            .wrap(SessionMiddleware::new(CookieSessionStore::default(), secret_key.clone()))
            .default_service(web::to(|| HttpResponse::Ok())))
        .bind(("127.0.0.1", 8080))?
        .run()
        .await
}

Limitations

Cookies are subject to size limits so we require session keys to be shorter than 4096 bytes. This translates into a limit on the maximum size of the session state when using cookies as storage backend.

The session cookie can always be inspected by end users via the developer tools exposed by their browsers. We strongly recommend setting the policy to CookieContentSecurity::Private when using cookies as storage backend.

There is no way to invalidate a session before its natural expiry when using cookies as the storage backend.

Trait Implementations

Returns the “default value” for a type. Read more

Loads the session state associated to a session key.

Persist the session state for a newly created session. Read more

Updates the session state associated to a pre-existing session key.

Updates the TTL of the session state associated to a pre-existing session key.

Deletes a session from the store.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more