feat: 优化CORS配置支持通配符和代码格式化

- 支持CORS配置使用通配符"*"允许所有来源访问
- 优化CORS策略配置逻辑,区分通配符和具体域名处理
- 格式化代码,移除多余空行和统一代码风格
- 修复Hangfire配置中的变量赋值格式
- 更新默认CORS配置为通配符模式
This commit is contained in:
chenchun
2025-08-14 17:04:27 +08:00
parent 2fd7f88f04
commit 9a31d14b41
2 changed files with 25 additions and 21 deletions

View File

@@ -56,14 +56,12 @@ namespace Yi.Abp.Web
typeof(YiAbpApplicationModule), typeof(YiAbpApplicationModule),
typeof(AbpAspNetCoreMultiTenancyModule), typeof(AbpAspNetCoreMultiTenancyModule),
typeof(AbpAspNetCoreMvcModule), typeof(AbpAspNetCoreMvcModule),
typeof(AbpSwashbuckleModule), typeof(AbpSwashbuckleModule),
typeof(AbpAspNetCoreSerilogModule), typeof(AbpAspNetCoreSerilogModule),
typeof(AbpAuditingModule), typeof(AbpAuditingModule),
typeof(AbpAspNetCoreAuthenticationJwtBearerModule), typeof(AbpAspNetCoreAuthenticationJwtBearerModule),
typeof(YiFrameworkAspNetCoreModule), typeof(YiFrameworkAspNetCoreModule),
typeof(YiFrameworkAspNetCoreAuthenticationOAuthModule), typeof(YiFrameworkAspNetCoreAuthenticationOAuthModule),
typeof(YiFrameworkBackgroundWorkersHangfireModule), typeof(YiFrameworkBackgroundWorkersHangfireModule),
typeof(AbpAutofacModule) typeof(AbpAutofacModule)
)] )]
@@ -108,10 +106,7 @@ namespace Yi.Abp.Web
//本地开发环境,可以禁用作业执行 //本地开发环境,可以禁用作业执行
if (host.IsDevelopment()) if (host.IsDevelopment())
{ {
Configure<AbpBackgroundWorkerOptions> (options => Configure<AbpBackgroundWorkerOptions>(options => { options.IsEnabled = false; });
{
options.IsEnabled = false;
});
} }
//请求日志 //请求日志
@@ -172,18 +167,27 @@ namespace Yi.Abp.Web
{ {
options.AddPolicy(DefaultCorsPolicyName, builder => options.AddPolicy(DefaultCorsPolicyName, builder =>
{ {
var corsOrigins = configuration["App:CorsOrigins"]!;
builder builder
.WithOrigins( .WithAbpExposedHeaders()
configuration["App:CorsOrigins"]! .AllowAnyHeader()
.AllowAnyMethod();
if (corsOrigins == "*")
{
builder.AllowAnyOrigin();
}
else
{
builder
.WithOrigins(corsOrigins
.Split(";", StringSplitOptions.RemoveEmptyEntries) .Split(";", StringSplitOptions.RemoveEmptyEntries)
.Select(o => o.RemovePostFix("/")) .Select(o => o.RemovePostFix("/"))
.ToArray() .ToArray())
) .SetIsOriginAllowedToAllowWildcardSubdomains()
.WithAbpExposedHeaders() .AllowCredentials();
.SetIsOriginAllowedToAllowWildcardSubdomains() }
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
}); });
}); });
@@ -200,17 +204,17 @@ namespace Yi.Abp.Web
//配置Hangfire定时任务存储开启redis后优先使用redis //配置Hangfire定时任务存储开启redis后优先使用redis
var redisConfiguration = configuration["Redis:Configuration"]; var redisConfiguration = configuration["Redis:Configuration"];
context.Services.AddHangfire(config=> context.Services.AddHangfire(config =>
{ {
var redisEnabled=configuration.GetSection("Redis").GetValue<bool>("IsEnabled"); var redisEnabled = configuration.GetSection("Redis").GetValue<bool>("IsEnabled");
if (redisEnabled) if (redisEnabled)
{ {
var jobDb=configuration.GetSection("Redis").GetValue<int>("JobDb"); var jobDb = configuration.GetSection("Redis").GetValue<int>("JobDb");
config.UseRedisStorage( config.UseRedisStorage(
ConnectionMultiplexer.Connect(redisConfiguration), ConnectionMultiplexer.Connect(redisConfiguration),
new RedisStorageOptions() new RedisStorageOptions()
{ {
Db =jobDb, Db = jobDb,
InvisibilityTimeout = TimeSpan.FromHours(1), //JOB允许执行1小时 InvisibilityTimeout = TimeSpan.FromHours(1), //JOB允许执行1小时
Prefix = "Yi:HangfireJob:" Prefix = "Yi:HangfireJob:"
}).WithJobExpirationTimeout(TimeSpan.FromHours(1)); }).WithJobExpirationTimeout(TimeSpan.FromHours(1));

View File

@@ -21,7 +21,7 @@
//应用启动 //应用启动
"App": { "App": {
"SelfUrl": "http://*:19001", "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": { "Settings": {