From 78389519e546d5b366cea83f67df4016a3f6a290 Mon Sep 17 00:00:00 2001
From: chenhaozhe <gaozhechen26@gmail.com>
Date: 星期二, 10 十一月 2026 21:28:29 +0800
Subject: [PATCH] 网关添加 跨域配置

---
 Program.cs |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)

diff --git a/Program.cs b/Program.cs
index e16352d..3db8134 100644
--- a/Program.cs
+++ b/Program.cs
@@ -10,6 +10,14 @@
 
 builder.Services.AddWindowsService(options => options.ServiceName = "APIGetway");
 
+builder.Services.AddCors(options =>
+{
+    options.AddPolicy("frontend-dev", policy => policy
+        .WithOrigins("http://192.168.0.102:32976")   // 鎹㈡垚鍓嶇鐪熷疄鍦板潃+绔彛
+        .WithMethods("GET", "POST", "PUT", "DELETE")
+        .WithHeaders("Content-Type", "X-Username", "Authorization"));
+});
+
 // YARP 鍙嶅悜浠g悊
 builder.Services.AddReverseProxy()
     .LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));
@@ -24,6 +32,10 @@
 app.UseDefaultFiles();
 app.UseStaticFiles();
 
+// 璺ㄥ煙涓棿浠讹細YARP 璺敱甯� CorsPolicy 鏃朵細寰�绔偣鍐� CORS 鍏冩暟鎹紝
+// 缂轰簡 UseCors 灏变細鍦ㄨ姹傛椂鎶� "contains CORS metadata, but a middleware was not found"
+app.UseCors();
+
 // YARP 杞彂锛氭寜 appsettings.json 涓殑 Routes 瑙勫垯杞彂鍒板悗绔�
 app.MapReverseProxy();
 

--
Gitblit v1.9.1