Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust 如何在Actix 2.0中从请求中获取Cookie_Rust_Rust Actix_Actix Web - Fatal编程技术网

Rust 如何在Actix 2.0中从请求中获取Cookie

Rust 如何在Actix 2.0中从请求中获取Cookie,rust,rust-actix,actix-web,Rust,Rust Actix,Actix Web,我想从请求中获取cookie的值。我发现在Actix 0.x.x中,cookie的值可以通过调用 fn get_cookie(req: HttpRequest) { let cookie = req.cookie("name") <-- Here return HttpResponse::Ok() .body( format!("{}", cookie); ) } 这就叫它什

我想从请求中获取cookie的值。我发现在Actix 0.x.x中,cookie的值可以通过调用

fn get_cookie(req: HttpRequest) {
    let cookie = req.cookie("name") <-- Here

    return HttpResponse::Ok()
        .body(
            format!("{}", cookie);
        )
}

这就叫它什么?

对于身份验证,您可能只需要使用中间件。提供了一个
IdentityService
,您可以使用
CookieIdentityPolicy
配置该服务,然后使用
identity
提取器在处理程序中获取标识。
pub fn get_cookie(req: HttpRequest, name: &str) -> String {
    let cookie: Vec<&str> = req
        .headers()
        .get("cookie")
        .unwrap()
        .to_str()
        .unwrap()
        .split("&")
        .collect();

    let auth_token: Vec<&str> = cookie
        .into_iter()
        .filter(|each| {
            let body: Vec<&str> = each.split("=").collect();

            body[0] == name
        })
        .collect();

    let cookie_part: Vec<&str> = auth_token[0].split("=").collect();

    cookie_part[1].to_owned() 
}