Rust 如何在Actix 2.0中从请求中获取Cookie
我想从请求中获取cookie的值。我发现在Actix 0.x.x中,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); ) } 这就叫它什
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()
}