chore 抽象微服务基本类库
This commit is contained in:
@ -0,0 +1,27 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>KonSoft.Shared.Hosting.Microservices</RootNamespace>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\KonSoft.Shared.Hosting.AspNetCore\KonSoft.Shared.Hosting.AspNetCore.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.DataProtection.StackExchangeRedis" Version="8.0.20" />
|
||||
<PackageReference Include="DistributedLock.Redis" Version="1.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Volo.Abp.AspNetCore.Authentication.JwtBearer" Version="8.3.4" />
|
||||
<PackageReference Include="Volo.Abp.EventBus.RabbitMQ" Version="8.3.4" />
|
||||
<PackageReference Include="Volo.Abp.BackgroundJobs.RabbitMQ" Version="8.3.4" />
|
||||
<PackageReference Include="Volo.Abp.Caching.StackExchangeRedis" Version="8.3.4" />
|
||||
<PackageReference Include="Volo.Abp.EntityFrameworkCore" Version="8.3.4" />
|
||||
<PackageReference Include="Volo.Abp.DistributedLocking" Version="8.3.4" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@ -0,0 +1,100 @@
|
||||
using KonSoft.Shared.Hosting.AspNetCore;
|
||||
using Medallion.Threading;
|
||||
using Medallion.Threading.Redis;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using StackExchange.Redis;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Cors;
|
||||
using Volo.Abp.AspNetCore.Authentication.JwtBearer;
|
||||
using Volo.Abp.BackgroundJobs.RabbitMQ;
|
||||
using Volo.Abp.Caching;
|
||||
using Volo.Abp.Caching.StackExchangeRedis;
|
||||
using Volo.Abp.DistributedLocking;
|
||||
using Volo.Abp.EntityFrameworkCore;
|
||||
using Volo.Abp.EventBus.RabbitMq;
|
||||
using Volo.Abp.Modularity;
|
||||
using Volo.Abp.Security.Claims;
|
||||
|
||||
namespace KonSoft.Shared.Hosting.Microservices
|
||||
{
|
||||
[DependsOn(
|
||||
typeof(KonSoftSharedHostingAspNetCoreModule),
|
||||
typeof(AbpBackgroundJobsRabbitMqModule),
|
||||
typeof(AbpAspNetCoreAuthenticationJwtBearerModule),
|
||||
typeof(AbpEventBusRabbitMqModule),
|
||||
typeof(AbpCachingStackExchangeRedisModule),
|
||||
typeof(AbpDistributedLockingModule),
|
||||
typeof(AbpEntityFrameworkCoreModule)
|
||||
)]
|
||||
public class KonSoftSharedHostingMicroservicesModule : AbpModule
|
||||
{
|
||||
public override void ConfigureServices(ServiceConfigurationContext context)
|
||||
{
|
||||
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();
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user