Skip to content

Commit d0dd912

Browse files
committed
refactor(middleware/cors): 优化跨域中间件配置
将允许的方法、头部和源从`Any`替换为具体的值,并启用凭据支持,以提高安全性和明确性
1 parent 3e4ff47 commit d0dd912

1 file changed

Lines changed: 27 additions & 4 deletions

File tree

backend/src/middleware/cors.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
1-
use tower_http::cors::{Any, CorsLayer};
1+
use axum::http;
2+
use http::Method;
3+
use http::header::{AUTHORIZATION, CONTENT_TYPE};
4+
use tower_http::cors::CorsLayer;
25

36
/// 创建跨域中间件
47
pub fn create_layer() -> CorsLayer {
8+
// 定义允许的源
9+
let origins = [
10+
"http://localhost:3000".parse().unwrap(),
11+
"https://exquisitecore.xyz".parse().unwrap(),
12+
"https://www.exquisitecore.xyz".parse().unwrap(),
13+
];
14+
15+
// 定义允许的方法
16+
let methods = [
17+
Method::GET,
18+
Method::POST,
19+
Method::PUT,
20+
Method::DELETE,
21+
Method::OPTIONS,
22+
];
23+
24+
// 定义允许的头部
25+
let headers = [AUTHORIZATION, CONTENT_TYPE];
26+
527
CorsLayer::new()
6-
.allow_methods(Any)
7-
.allow_headers(Any)
8-
.allow_origin(Any)
28+
.allow_methods(methods)
29+
.allow_headers(headers)
30+
.allow_origin(origins)
31+
.allow_credentials(true)
932
}

0 commit comments

Comments
 (0)