chore 优化代码

This commit is contained in:
2025-10-16 10:30:51 +08:00
parent f1c609b4be
commit 1f5bc3e971
367 changed files with 2705 additions and 3083 deletions

View File

@ -1,4 +1,6 @@
using KonSoft.Shared.Hosting.AspNetCore;
using System;
using System.Linq;
using KonSoft.Shared.Hosting.AspNetCore;
using Medallion.Threading;
using Medallion.Threading.Redis;
using Microsoft.AspNetCore.Authentication.JwtBearer;
@ -7,8 +9,6 @@ using Microsoft.AspNetCore.DataProtection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using StackExchange.Redis;
using System;
using System.Linq;
using Volo.Abp.AspNetCore.Authentication.JwtBearer;
using Volo.Abp.AspNetCore.Mvc.UI.MultiTenancy;
using Volo.Abp.BackgroundJobs.RabbitMQ;
@ -21,84 +21,83 @@ using Volo.Abp.Modularity;
using Volo.Abp.Security.Claims;
using Volo.Abp.Swashbuckle;
namespace KonSoft.Shared.Hosting.Microservices
namespace KonSoft.Shared.Hosting.Microservices;
[DependsOn(
typeof(KonSoftSharedHostingAspNetCoreModule),
typeof(AbpBackgroundJobsRabbitMqModule),
typeof(AbpAspNetCoreAuthenticationJwtBearerModule),
typeof(AbpEventBusRabbitMqModule),
typeof(AbpCachingStackExchangeRedisModule),
typeof(AbpDistributedLockingModule),
typeof(AbpAspNetCoreMvcUiMultiTenancyModule),
typeof(AbpEntityFrameworkCoreModule),
typeof(AbpSwashbuckleModule)
)]
public class KonSoftSharedHostingMicroservicesModule : AbpModule
{
[DependsOn(
typeof(KonSoftSharedHostingAspNetCoreModule),
typeof(AbpBackgroundJobsRabbitMqModule),
typeof(AbpAspNetCoreAuthenticationJwtBearerModule),
typeof(AbpEventBusRabbitMqModule),
typeof(AbpCachingStackExchangeRedisModule),
typeof(AbpDistributedLockingModule),
typeof(AbpAspNetCoreMvcUiMultiTenancyModule),
typeof(AbpEntityFrameworkCoreModule),
typeof(AbpSwashbuckleModule)
)]
public class KonSoftSharedHostingMicroservicesModule : AbpModule
public override void ConfigureServices(ServiceConfigurationContext context)
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
var configuration = context.Services.GetConfiguration();
var hostingEnvironment = context.Services.GetHostingEnvironment();
var configuration = context.Services.GetConfiguration();
var hostingEnvironment = context.Services.GetHostingEnvironment();
ConfigureCache();
ConfigureDataProtection(context, configuration);
ConfigureAuthentication(context, configuration);
ConfigureCors(context, configuration);
}
private void ConfigureCache()
{
Configure<AbpDistributedCacheOptions>(options => { options.KeyPrefix = "KonSoft:"; });
}
private void ConfigureDataProtection(
ServiceConfigurationContext context,
IConfiguration configuration)
{
context.Services.AddDataProtection().SetApplicationName("KonSoft")
.PersistKeysToStackExchangeRedis(ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]!),
"KonSoft-Protection-Keys");
context.Services.AddSingleton<IDistributedLockProvider>(_ =>
new RedisDistributedSynchronizationProvider(ConnectionMultiplexer
.Connect(configuration["Redis:Configuration"]!).GetDatabase()));
}
private void ConfigureAuthentication(ServiceConfigurationContext context, IConfiguration configuration)
{
context.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddAbpJwtBearer(options =>
{
options.Authority = configuration["AuthServer:Authority"];
options.RequireHttpsMetadata = configuration.GetValue<bool>("AuthServer:RequireHttpsMetadata");
options.Audience = KonSoftConsts.AuthServerAudience;
});
context.Services.Configure<AbpClaimsPrincipalFactoryOptions>(options =>
{
options.IsDynamicClaimsEnabled = true;
});
}
private void ConfigureCors(ServiceConfigurationContext context, IConfiguration configuration)
{
context.Services.AddCors(options =>
{
options.AddDefaultPolicy(builder =>
{
builder
.WithOrigins(configuration["App:CorsOrigins"]?
.Split(",", StringSplitOptions.RemoveEmptyEntries)
.Select(o => o.RemovePostFix("/"))
.ToArray() ?? [])
.WithAbpExposedHeaders()
.SetIsOriginAllowedToAllowWildcardSubdomains()
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
}
ConfigureCache();
ConfigureDataProtection(context, configuration);
ConfigureAuthentication(context, configuration);
ConfigureCors(context, configuration);
}
}
private void ConfigureCache()
{
Configure<AbpDistributedCacheOptions>(options => { options.KeyPrefix = "KonSoft:"; });
}
private void ConfigureDataProtection(
ServiceConfigurationContext context,
IConfiguration configuration)
{
context.Services.AddDataProtection().SetApplicationName("KonSoft")
.PersistKeysToStackExchangeRedis(ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]!),
"KonSoft-Protection-Keys");
context.Services.AddSingleton<IDistributedLockProvider>(_ =>
new RedisDistributedSynchronizationProvider(ConnectionMultiplexer
.Connect(configuration["Redis:Configuration"]!).GetDatabase()));
}
private void ConfigureAuthentication(ServiceConfigurationContext context, IConfiguration configuration)
{
context.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddAbpJwtBearer(options =>
{
options.Authority = configuration["AuthServer:Authority"];
options.RequireHttpsMetadata = configuration.GetValue<bool>("AuthServer:RequireHttpsMetadata");
options.Audience = KonSoftConsts.AuthServerAudience;
});
context.Services.Configure<AbpClaimsPrincipalFactoryOptions>(options =>
{
options.IsDynamicClaimsEnabled = true;
});
}
private void ConfigureCors(ServiceConfigurationContext context, IConfiguration configuration)
{
context.Services.AddCors(options =>
{
options.AddDefaultPolicy(builder =>
{
builder
.WithOrigins(configuration["App:CorsOrigins"]?
.Split(",", StringSplitOptions.RemoveEmptyEntries)
.Select(o => o.RemovePostFix("/"))
.ToArray() ?? [])
.WithAbpExposedHeaders()
.SetIsOriginAllowedToAllowWildcardSubdomains()
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
}
}