小编Mam*_*ezo的帖子

如何在 actix-web 中创建受保护的路由

我需要验证用户是否有权访问某些路线。我已经创建了 3 个“范围”(访客、身份验证用户、管理员),现在我不知道如何检查用户是否可以访问这些路由。

我正在尝试实现 auth-middleware,这个中间件应该检查​​用户是否有正确的 cookie 或令牌。(我可以从请求标头中打印出一个 cookie),但我不知道如何导入、使用 actix_identity 以及如何访问此中间件中的 id 参数。

我相信我的问题不仅与 Actix-identity 有关,而且我无法在中间件内部传递参数。

    #[actix_rt::main]
    async fn main() -> std::io::Result<()> {

        let cookie_key = conf.server.key;
    
        // Register http routes
        let mut server = HttpServer::new(move || {
            App::new()
                // Enable logger
                .wrap(Logger::default())
                .wrap(IdentityService::new(
                    CookieIdentityPolicy::new(cookie_key.as_bytes())
                        .name("auth-cookie")
                        .path("/")
                        .secure(false),
                ))
                //limit the maximum amount of data that server will accept
                .data(web::JsonConfig::default().limit(4096))
                //normal routes
                .service(web::resource("/").route(web::get().to(status)))
                // .configure(routes)
                .service(
                    web::scope("/api")
                        // guest endpoints
                        .service(web::resource("/user_login").route(web::post().to(login)))
                        .service(web::resource("/user_logout").route(web::post().to(logout)))
                        // admin endpoints
                        .service(
                            web::scope("/admin")
                                // .wrap(AdminAuthMiddleware)
                                .service(
                                    web::resource("/create_admin").route(web::post().to(create_admin)), …
Run Code Online (Sandbox Code Playgroud)

authentication rust-actix actix-web

14
推荐指数
3
解决办法
5688
查看次数

标签 统计

actix-web ×1

authentication ×1

rust-actix ×1