问题描述
我正在使用 actix-web 构建 REST API.如何配置 CORS 以接受来自任何来源的请求?
I am building a REST API with actix-web. How do I configure CORS to accept requests from any origin?
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?
The above code works from the web at localhost:8081
, but not from 0.0.0.0:8081
or 127.0.0.1:8081
. I tried "*"
to allow all, but it's not working. How do I allow all, or at least allow a specific origin and then pass multiple URLs?
推荐答案
默认 All
origins is 允许
By default All
origins is allowed
这是我的简单 CORS 设置(允许所有来源和方法 + 允许发送凭据)
This is my simple CORS setup (allow all origins and methods + allow send credentials)
Cors::new().supports_credentials()
您可以从它开始,逐步禁止方法、来源和标题.
You can start with it, and disallow methods, origins and headers step-by-step.
这篇关于如何配置 actix-web 以接受来自任何来源的 CORS 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!