如何配置 actix-web 以接受来自任何来源的 CORS 请求?
How do I configure actix-web to accept CORS requests from any origin?
我正在使用 actix-web 构建 REST API。如何配置 CORS 以接受来自任何来源的请求?
Cors::new() // <- Construct CORS middleware builder
.allowed_origin("localhost:8081")
.allowed_methods(vec!["GET", "POST"])
.allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT])
.allowed_header(http::header::CONTENT_TYPE)
.max_age(3600)
以上代码在 localhost:8081
的网络上有效,但不适用于 0.0.0.0:8081
或 127.0.0.1:8081
。我尝试 "*"
允许全部,但它不起作用。我如何允许所有或至少允许特定来源然后传递多个 URL?
默认 All
来源是 allowed
这是我的简单 CORS 设置(允许所有来源和方法 + 允许发送凭据)
Cors::new().supports_credentials()
你可以从它开始,禁止方法、来源和headers step-by-step。
从actix-cors = "0.5.0"
开始,您可以使用:
Cors::permissive()
但是,他们建议不要在生产中使用它:https://docs.rs/actix-cors/latest/actix_cors/struct.Cors.html#method.permissive
我正在使用 actix-web 构建 REST API。如何配置 CORS 以接受来自任何来源的请求?
Cors::new() // <- Construct CORS middleware builder
.allowed_origin("localhost:8081")
.allowed_methods(vec!["GET", "POST"])
.allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT])
.allowed_header(http::header::CONTENT_TYPE)
.max_age(3600)
以上代码在 localhost:8081
的网络上有效,但不适用于 0.0.0.0:8081
或 127.0.0.1:8081
。我尝试 "*"
允许全部,但它不起作用。我如何允许所有或至少允许特定来源然后传递多个 URL?
默认 All
来源是 allowed
这是我的简单 CORS 设置(允许所有来源和方法 + 允许发送凭据)
Cors::new().supports_credentials()
你可以从它开始,禁止方法、来源和headers step-by-step。
从actix-cors = "0.5.0"
开始,您可以使用:
Cors::permissive()
但是,他们建议不要在生产中使用它:https://docs.rs/actix-cors/latest/actix_cors/struct.Cors.html#method.permissive