From 9a31d14b41e570947c5c5e04ab5e35f67907f66d Mon Sep 17 00:00:00 2001 From: chenchun Date: Thu, 14 Aug 2025 17:04:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96CORS=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=94=AF=E6=8C=81=E9=80=9A=E9=85=8D=E7=AC=A6=E5=92=8C?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 支持CORS配置使用通配符"*"允许所有来源访问 - 优化CORS策略配置逻辑,区分通配符和具体域名处理 - 格式化代码,移除多余空行和统一代码风格 - 修复Hangfire配置中的变量赋值格式 - 更新默认CORS配置为通配符模式 --- Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs | 44 +++++++++++--------- Yi.Abp.Net8/src/Yi.Abp.Web/appsettings.json | 2 +- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs b/Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs index be0fe985..9e08520e 100644 --- a/Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs +++ b/Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs @@ -56,14 +56,12 @@ namespace Yi.Abp.Web typeof(YiAbpApplicationModule), typeof(AbpAspNetCoreMultiTenancyModule), typeof(AbpAspNetCoreMvcModule), - typeof(AbpSwashbuckleModule), typeof(AbpAspNetCoreSerilogModule), typeof(AbpAuditingModule), typeof(AbpAspNetCoreAuthenticationJwtBearerModule), typeof(YiFrameworkAspNetCoreModule), typeof(YiFrameworkAspNetCoreAuthenticationOAuthModule), - typeof(YiFrameworkBackgroundWorkersHangfireModule), typeof(AbpAutofacModule) )] @@ -108,12 +106,9 @@ namespace Yi.Abp.Web //本地开发环境,可以禁用作业执行 if (host.IsDevelopment()) { - Configure (options => - { - options.IsEnabled = false; - }); + Configure(options => { options.IsEnabled = false; }); } - + //请求日志 Configure(options => { @@ -172,18 +167,27 @@ namespace Yi.Abp.Web { options.AddPolicy(DefaultCorsPolicyName, builder => { + var corsOrigins = configuration["App:CorsOrigins"]!; + builder - .WithOrigins( - configuration["App:CorsOrigins"]! + .WithAbpExposedHeaders() + .AllowAnyHeader() + .AllowAnyMethod(); + + if (corsOrigins == "*") + { + builder.AllowAnyOrigin(); + } + else + { + builder + .WithOrigins(corsOrigins .Split(";", StringSplitOptions.RemoveEmptyEntries) .Select(o => o.RemovePostFix("/")) - .ToArray() - ) - .WithAbpExposedHeaders() - .SetIsOriginAllowedToAllowWildcardSubdomains() - .AllowAnyHeader() - .AllowAnyMethod() - .AllowCredentials(); + .ToArray()) + .SetIsOriginAllowedToAllowWildcardSubdomains() + .AllowCredentials(); + } }); }); @@ -200,17 +204,17 @@ namespace Yi.Abp.Web //配置Hangfire定时任务存储,开启redis后,优先使用redis var redisConfiguration = configuration["Redis:Configuration"]; - context.Services.AddHangfire(config=> + context.Services.AddHangfire(config => { - var redisEnabled=configuration.GetSection("Redis").GetValue("IsEnabled"); + var redisEnabled = configuration.GetSection("Redis").GetValue("IsEnabled"); if (redisEnabled) { - var jobDb=configuration.GetSection("Redis").GetValue("JobDb"); + var jobDb = configuration.GetSection("Redis").GetValue("JobDb"); config.UseRedisStorage( ConnectionMultiplexer.Connect(redisConfiguration), new RedisStorageOptions() { - Db =jobDb, + Db = jobDb, InvisibilityTimeout = TimeSpan.FromHours(1), //JOB允许执行1小时 Prefix = "Yi:HangfireJob:" }).WithJobExpirationTimeout(TimeSpan.FromHours(1)); diff --git a/Yi.Abp.Net8/src/Yi.Abp.Web/appsettings.json b/Yi.Abp.Net8/src/Yi.Abp.Web/appsettings.json index 744b05ba..95351b34 100644 --- a/Yi.Abp.Net8/src/Yi.Abp.Web/appsettings.json +++ b/Yi.Abp.Net8/src/Yi.Abp.Web/appsettings.json @@ -21,7 +21,7 @@ //应用启动 "App": { "SelfUrl": "http://*:19001", - "CorsOrigins": "http://localhost:19001;http://localhost:18000;vscode-file://vscode-app;https://web.chatboxai.app;capacitor://localhost;http://codegeex;https://yxai.chat" + "CorsOrigins": "*" }, //配置 "Settings": {