23个版本

0.7.1 2024年3月2日
0.6.0 2023年9月16日
0.5.2 2022年7月19日
0.4.0 2022年3月1日
0.1.0 2019年6月12日

#538 in HTTP服务器

Download history 4134/week @ 2024-04-23 4497/week @ 2024-04-30 3759/week @ 2024-05-07 3789/week @ 2024-05-14 4225/week @ 2024-05-21 3819/week @ 2024-05-28 4336/week @ 2024-06-04 4497/week @ 2024-06-11 4269/week @ 2024-06-18 4170/week @ 2024-06-25 3774/week @ 2024-07-02 4107/week @ 2024-07-09 3783/week @ 2024-07-16 5291/week @ 2024-07-23 3866/week @ 2024-07-30 4127/week @ 2024-08-06

每月下载量17,735
用于 12 个 crate(11个直接使用)

MIT/Apache

160KB
2.5K SLoC

actix-identity

Actix Web的身份管理。

crates.io Documentation Apache 2.0 or MIT licensed Dependency Status

Actix Web的身份管理。

actix-identity可用于跟踪用户在多个请求中的身份。它基于HTTP会话,通过actix-session构建。

入门

要在您的Actix Web应用程序中使用身份管理,您必须在您的App上注册IdentityMiddlewareSessionMiddleware作为中间件。

use actix_web::{cookie::Key, App, HttpServer, HttpResponse};
use actix_identity::IdentityMiddleware;
use actix_session::{storage::RedisSessionStore, SessionMiddleware};

#[actix_web::main]
async fn main() {
    // When using `Key::generate()` it is important to initialize outside of the
    // `HttpServer::new` closure. When deployed the secret key should be read from a
    // configuration file or environment variables.
    let secret_key = Key::generate();

    let redis_store = RedisSessionStore::new("redis://127.0.0.1:6379")
        .await
        .unwrap();

    HttpServer::new(move || {
        App::new()
            // Install the identity framework first.
            .wrap(IdentityMiddleware::default())
            // The identity system is built on top of sessions. You must install the session
            // middleware to leverage `actix-identity`. The session middleware must be mounted
            // AFTER the identity middleware: `actix-web` invokes middleware in the OPPOSITE
            // order of registration when it receives an incoming request.
            .wrap(SessionMiddleware::new(
                 redis_store.clone(),
                 secret_key.clone(),
            ))
            // Your request handlers [...]
    })
}

您可以使用请求处理程序中的Identity提取器创建、访问和销毁用户身份。

use actix_web::{get, post, HttpResponse, Responder, HttpRequest, HttpMessage};
use actix_identity::Identity;
use actix_session::storage::RedisSessionStore;

#[get("/")]
async fn index(user: Option<Identity>) -> impl Responder {
    if let Some(user) = user {
        format!("Welcome! {}", user.id().unwrap())
    } else {
        "Welcome Anonymous!".to_owned()
    }
}

#[post("/login")]
async fn login(request: HttpRequest) -> impl Responder {
    // Some kind of authentication should happen here
    // e.g. password-based, biometric, etc.
    // [...]

    // attach a verified user identity to the active session
    Identity::login(&request.extensions(), "User1".into()).unwrap();

    HttpResponse::Ok()
}

#[post("/logout")]
async fn logout(user: Identity) -> impl Responder {
    user.logout();
    HttpResponse::Ok()
}

高级配置

默认情况下,actix-identity不会自动注销用户。您可以通过自定义IdentityMiddleware的配置来更改此行为,通过IdentityMiddleware::builder

特别是,您可以自动注销以下用户:

依赖关系

~15–26MB
~454K SLoC