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(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<AbpBackgroundWorkerOptions> (options =>
{
options.IsEnabled = false;
});
Configure<AbpBackgroundWorkerOptions>(options => { options.IsEnabled = false; });
}
//请求日志
Configure<AbpAuditingOptions>(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<bool>("IsEnabled");
var redisEnabled = configuration.GetSection("Redis").GetValue<bool>("IsEnabled");
if (redisEnabled)
{
var jobDb=configuration.GetSection("Redis").GetValue<int>("JobDb");
var jobDb = configuration.GetSection("Redis").GetValue<int>("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));

View File

@@ -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": {