不满足特征界限 `actix_identity::Identity: FromRequest`
the trait bound `actix_identity::Identity: FromRequest` is not satisfied
我正在关注 this tutorial 使用 actix-web、tera 和 diesel 创建一个简单的网站。
我目前使用 actix-identity 为登录用户添加会话。据我了解,我需要将中间件附加到应用程序。但是出现了一些错误。
其中一个说 the trait bound 'actix_identity::Identity: FromRequest' is not satisfied
。
我试图阅读 actix-identity 文档,但不知道我的代码有什么问题。有人说我可能忘记了 return
,但找不到。
我才学了几天rust,希望有人能给我个线索。
这是代码
main.rs
use actix_web::{web, web::Data, App, HttpResponse, HttpServer, Responder};
use tera::{Tera, Context};
use actix_identity::{CookieIdentityPolicy, Identity, IdentityService};
async fn login(tera: Data<Tera>, id: Identity) -> impl Responder {
let mut data = Context::new();
data.insert("head_title", "Login");
if let Some(id) = id.identity() {
return HttpResponse::Ok().body("Already logged in")
}
let rendered = tera.render("login.html", &data).unwrap();
return HttpResponse::Ok().body(rendered)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(move || {
let tera = Tera::new("templates/**/*").expect("Failed to parse template file");
App::new()
.wrap(IdentityService::new(
CookieIdentityPolicy::new(&[0;32])
.name("auth-cookie")
.secure(false)
))
.app_data(web::Data::new(tera))
.route("/login", web::get().to(login))
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}
Cargo.toml
--- snip ---
[dependencies]
actix-web = "4"
tera = "1.15"
dotenv = "0.15"
serde = { version = "1.0", features = ["derive"] }
diesel = { version = "1.4", features = ["postgres", "chrono"] }
chrono = { version = "0.4", features = ["serde"] }
actix-identity = "0.3"
这是完整的错误信息
error[E0277]: the trait bound `IdentityService<CookieIdentityPolicy>: Transform<actix_web::app_service::AppRouting, ServiceRequest>` is not satisfied
--> src/main.rs:26:19
|
26 | .wrap(IdentityService::new(
| ______________----_^
| | |
| | required by a bound introduced by this call
27 | | CookieIdentityPolicy::new(&[0;32])
28 | | .name("auth-cookie")
29 | | .secure(false)
30 | | ))
| |_____________^ the trait `Transform<actix_web::app_service::AppRouting, ServiceRequest>` is not implemented for `IdentityService<CookieIdentityPolicy>`
|
note: required by a bound in `App::<T>::wrap`
--> /home/ikraduya/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-web-4.0.1/src/app.rs:358:12
|
358 | M: Transform<
| ____________^
359 | | T::Service,
360 | | ServiceRequest,
361 | | Response = ServiceResponse<B>,
362 | | Error = Error,
363 | | InitError = (),
364 | | > + 'static,
| |_____________^ required by this bound in `App::<T>::wrap`
error[E0277]: the trait bound `actix_identity::Identity: FromRequest` is not satisfied
--> src/main.rs:32:41
|
32 | .route("/login", web::get().to(login))
| ^^ the trait `FromRequest` is not implemented for `actix_identity::Identity`
|
= note: required because of the requirements on the impl of `FromRequest` for `(Data<Tera>, actix_identity::Identity)`
note: required by a bound in `Route::to`
--> /home/ikraduya/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-web-4.0.1/src/route.rs:185:15
|
185 | Args: FromRequest + 'static,
| ^^^^^^^^^^^ required by this bound in `Route::to`
For more information about this error, try `rustc --explain E0277`.
我克隆了 this repo 来与我的代码进行比较,它有效,但我找不到任何显着差异。
错误消息没有帮助(有 open issue for this)。
您正在使用 actix-identity 0.3,这取决于 actix-web 3,而您的主要应用程序功能正在使用 actix-web 4,这导致 Rust 包含 both actix-web 3 和 4 进入您的应用程序。 actix_identity::Identity
从 actix-web 3 实现 FromRequest
,而不是 actix-web 4,这就是错误消息试图说明的内容。
要修复,您需要确保两个箱子的版本兼容。在这种情况下,您可以简单地将 actix-identity 升级到 0.4.0,这取决于 actix-web 4.
[dependencies]
actix-web = "4"
actix-identity = "0.4"
我正在关注 this tutorial 使用 actix-web、tera 和 diesel 创建一个简单的网站。 我目前使用 actix-identity 为登录用户添加会话。据我了解,我需要将中间件附加到应用程序。但是出现了一些错误。
其中一个说 the trait bound 'actix_identity::Identity: FromRequest' is not satisfied
。
我试图阅读 actix-identity 文档,但不知道我的代码有什么问题。有人说我可能忘记了 return
,但找不到。
我才学了几天rust,希望有人能给我个线索。
这是代码
main.rs
use actix_web::{web, web::Data, App, HttpResponse, HttpServer, Responder};
use tera::{Tera, Context};
use actix_identity::{CookieIdentityPolicy, Identity, IdentityService};
async fn login(tera: Data<Tera>, id: Identity) -> impl Responder {
let mut data = Context::new();
data.insert("head_title", "Login");
if let Some(id) = id.identity() {
return HttpResponse::Ok().body("Already logged in")
}
let rendered = tera.render("login.html", &data).unwrap();
return HttpResponse::Ok().body(rendered)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(move || {
let tera = Tera::new("templates/**/*").expect("Failed to parse template file");
App::new()
.wrap(IdentityService::new(
CookieIdentityPolicy::new(&[0;32])
.name("auth-cookie")
.secure(false)
))
.app_data(web::Data::new(tera))
.route("/login", web::get().to(login))
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}
Cargo.toml
--- snip ---
[dependencies]
actix-web = "4"
tera = "1.15"
dotenv = "0.15"
serde = { version = "1.0", features = ["derive"] }
diesel = { version = "1.4", features = ["postgres", "chrono"] }
chrono = { version = "0.4", features = ["serde"] }
actix-identity = "0.3"
这是完整的错误信息
error[E0277]: the trait bound `IdentityService<CookieIdentityPolicy>: Transform<actix_web::app_service::AppRouting, ServiceRequest>` is not satisfied
--> src/main.rs:26:19
|
26 | .wrap(IdentityService::new(
| ______________----_^
| | |
| | required by a bound introduced by this call
27 | | CookieIdentityPolicy::new(&[0;32])
28 | | .name("auth-cookie")
29 | | .secure(false)
30 | | ))
| |_____________^ the trait `Transform<actix_web::app_service::AppRouting, ServiceRequest>` is not implemented for `IdentityService<CookieIdentityPolicy>`
|
note: required by a bound in `App::<T>::wrap`
--> /home/ikraduya/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-web-4.0.1/src/app.rs:358:12
|
358 | M: Transform<
| ____________^
359 | | T::Service,
360 | | ServiceRequest,
361 | | Response = ServiceResponse<B>,
362 | | Error = Error,
363 | | InitError = (),
364 | | > + 'static,
| |_____________^ required by this bound in `App::<T>::wrap`
error[E0277]: the trait bound `actix_identity::Identity: FromRequest` is not satisfied
--> src/main.rs:32:41
|
32 | .route("/login", web::get().to(login))
| ^^ the trait `FromRequest` is not implemented for `actix_identity::Identity`
|
= note: required because of the requirements on the impl of `FromRequest` for `(Data<Tera>, actix_identity::Identity)`
note: required by a bound in `Route::to`
--> /home/ikraduya/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-web-4.0.1/src/route.rs:185:15
|
185 | Args: FromRequest + 'static,
| ^^^^^^^^^^^ required by this bound in `Route::to`
For more information about this error, try `rustc --explain E0277`.
我克隆了 this repo 来与我的代码进行比较,它有效,但我找不到任何显着差异。
错误消息没有帮助(有 open issue for this)。
您正在使用 actix-identity 0.3,这取决于 actix-web 3,而您的主要应用程序功能正在使用 actix-web 4,这导致 Rust 包含 both actix-web 3 和 4 进入您的应用程序。 actix_identity::Identity
从 actix-web 3 实现 FromRequest
,而不是 actix-web 4,这就是错误消息试图说明的内容。
要修复,您需要确保两个箱子的版本兼容。在这种情况下,您可以简单地将 actix-identity 升级到 0.4.0,这取决于 actix-web 4.
[dependencies]
actix-web = "4"
actix-identity = "0.4"