You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
382 lines
16 KiB
382 lines
16 KiB
using DotNetCore.CAP;
|
|
using LINGYUN.Abp.Aliyun.SettingManagement;
|
|
using LINGYUN.Abp.AspNetCore.HttpOverrides;
|
|
using LINGYUN.Abp.Auditing;
|
|
using LINGYUN.Abp.EventBus.CAP;
|
|
using LINGYUN.Abp.ExceptionHandling;
|
|
using LINGYUN.Abp.ExceptionHandling.Emailing;
|
|
using LINGYUN.Abp.FeatureManagement;
|
|
using LINGYUN.Abp.LocalizationManagement;
|
|
using LINGYUN.Abp.LocalizationManagement.EntityFrameworkCore;
|
|
using LINGYUN.Abp.MessageService;
|
|
using LINGYUN.Abp.MultiTenancy.DbFinder;
|
|
using LINGYUN.Abp.OssManagement;
|
|
using LINGYUN.Abp.PermissionManagement.Identity;
|
|
using LINGYUN.Abp.SettingManagement;
|
|
using LINGYUN.Abp.Sms.Aliyun;
|
|
using LINGYUN.Abp.TenantManagement;
|
|
using LINGYUN.Abp.WeChat.SettingManagement;
|
|
using LINGYUN.ApiGateway;
|
|
using LINGYUN.Platform;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.Extensions.Caching.StackExchangeRedis;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.OpenApi.Models;
|
|
using StackExchange.Redis;
|
|
using System;
|
|
using System.Text;
|
|
using Volo.Abp;
|
|
using Volo.Abp.AspNetCore.Authentication.JwtBearer;
|
|
using Volo.Abp.AspNetCore.Mvc.UI.MultiTenancy;
|
|
using Volo.Abp.AspNetCore.Security.Claims;
|
|
using Volo.Abp.Auditing;
|
|
using Volo.Abp.AuditLogging.EntityFrameworkCore;
|
|
using Volo.Abp.Authorization.Permissions;
|
|
using Volo.Abp.Autofac;
|
|
using Volo.Abp.Caching;
|
|
using Volo.Abp.Caching.StackExchangeRedis;
|
|
using Volo.Abp.Data;
|
|
using Volo.Abp.Domain.Entities.Events.Distributed;
|
|
using Volo.Abp.EntityFrameworkCore;
|
|
using Volo.Abp.EntityFrameworkCore.MySQL;
|
|
using Volo.Abp.FeatureManagement;
|
|
using Volo.Abp.FeatureManagement.EntityFrameworkCore;
|
|
using Volo.Abp.Identity.EntityFrameworkCore;
|
|
using Volo.Abp.Identity.Localization;
|
|
using Volo.Abp.IdentityServer.EntityFrameworkCore;
|
|
using Volo.Abp.Json;
|
|
using Volo.Abp.Localization;
|
|
using Volo.Abp.Modularity;
|
|
using Volo.Abp.MultiTenancy;
|
|
using Volo.Abp.PermissionManagement;
|
|
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
|
|
using Volo.Abp.PermissionManagement.HttpApi;
|
|
using Volo.Abp.PermissionManagement.IdentityServer;
|
|
using Volo.Abp.Security.Claims;
|
|
using Volo.Abp.Security.Encryption;
|
|
using Volo.Abp.SettingManagement.EntityFrameworkCore;
|
|
using Volo.Abp.TenantManagement.EntityFrameworkCore;
|
|
using Volo.Abp.Threading;
|
|
using Volo.Abp.VirtualFileSystem;
|
|
|
|
namespace LINGYUN.Abp.BackendAdmin
|
|
{
|
|
[DependsOn(
|
|
typeof(AbpAspNetCoreMvcUiMultiTenancyModule),
|
|
typeof(PlatformApplicationContractModule),
|
|
typeof(ApiGatewayApplicationContractsModule),
|
|
typeof(AbpOssManagementApplicationContractsModule),
|
|
typeof(AbpMessageServiceApplicationContractsModule),
|
|
typeof(AbpLocalizationManagementApplicationContractsModule),
|
|
typeof(LINGYUN.Abp.Account.AbpAccountApplicationContractsModule),// 引用类似的包主要用于聚合权限管理和设置
|
|
typeof(LINGYUN.Abp.Identity.AbpIdentityApplicationContractsModule),
|
|
typeof(LINGYUN.Abp.IdentityServer.AbpIdentityServerApplicationContractsModule),
|
|
typeof(AbpSettingManagementApplicationModule),
|
|
typeof(AbpSettingManagementHttpApiModule),
|
|
typeof(AbpPermissionManagementApplicationModule),
|
|
typeof(AbpPermissionManagementHttpApiModule),
|
|
typeof(AbpFeatureManagementApplicationModule),
|
|
typeof(AbpFeatureManagementHttpApiModule),
|
|
typeof(AbpFeatureManagementClientModule),
|
|
typeof(AbpAuditingApplicationModule),
|
|
typeof(AbpAuditingHttpApiModule),
|
|
typeof(AbpTenantManagementApplicationModule),
|
|
typeof(AbpTenantManagementHttpApiModule),
|
|
typeof(AbpWeChatSettingManagementModule),// 微信配置管理模块
|
|
typeof(AbpAliyunSettingManagementModule),// 阿里云配置管理模块
|
|
typeof(AbpEntityFrameworkCoreMySQLModule),
|
|
typeof(AbpIdentityEntityFrameworkCoreModule),// 用户角色权限需要引用包
|
|
typeof(AbpIdentityServerEntityFrameworkCoreModule), // 客户端权限需要引用包
|
|
typeof(AbpAuditLoggingEntityFrameworkCoreModule),
|
|
typeof(AbpTenantManagementEntityFrameworkCoreModule),
|
|
typeof(AbpSettingManagementEntityFrameworkCoreModule),
|
|
typeof(AbpPermissionManagementDomainIdentityModule),
|
|
typeof(AbpPermissionManagementDomainIdentityServerModule),
|
|
typeof(AbpPermissionManagementEntityFrameworkCoreModule),
|
|
typeof(AbpFeatureManagementEntityFrameworkCoreModule),
|
|
typeof(AbpLocalizationManagementEntityFrameworkCoreModule),
|
|
typeof(AbpAspNetCoreAuthenticationJwtBearerModule),
|
|
typeof(AbpEmailingExceptionHandlingModule),
|
|
typeof(AbpCAPEventBusModule),
|
|
typeof(AbpAliyunSmsModule),
|
|
typeof(AbpDbFinderMultiTenancyModule),
|
|
typeof(AbpCachingStackExchangeRedisModule),
|
|
typeof(AbpAspNetCoreHttpOverridesModule),
|
|
typeof(AbpAutofacModule)
|
|
)]
|
|
public class BackendAdminHostModule : AbpModule
|
|
{
|
|
public override void PreConfigureServices(ServiceConfigurationContext context)
|
|
{
|
|
var configuration = context.Services.GetConfiguration();
|
|
|
|
PreConfigure<CapOptions>(options =>
|
|
{
|
|
options
|
|
.UseMySql(configuration.GetConnectionString("Default"))
|
|
.UseRabbitMQ(rabbitMQOptions =>
|
|
{
|
|
configuration.GetSection("CAP:RabbitMQ").Bind(rabbitMQOptions);
|
|
})
|
|
.UseDashboard();
|
|
});
|
|
}
|
|
|
|
public override void ConfigureServices(ServiceConfigurationContext context)
|
|
{
|
|
var hostingEnvironment = context.Services.GetHostingEnvironment();
|
|
var configuration = hostingEnvironment.BuildConfiguration();
|
|
|
|
// 配置Ef
|
|
Configure<AbpDbContextOptions>(options =>
|
|
{
|
|
options.UseMySQL();
|
|
});
|
|
|
|
// 解决某些不支持类型的序列化
|
|
Configure<AbpJsonOptions>(options =>
|
|
{
|
|
// See: https://docs.abp.io/en/abp/4.0/Migration-Guides/Abp-4_0#always-use-the-newtonsoft-json
|
|
options.UseHybridSerializer = false;
|
|
});
|
|
// 中文序列化的编码问题
|
|
//Configure<AbpSystemTextJsonSerializerOptions>(options =>
|
|
//{
|
|
// options.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All);
|
|
//});
|
|
|
|
// 加解密
|
|
Configure<AbpStringEncryptionOptions>(options =>
|
|
{
|
|
var encryptionConfiguration = configuration.GetSection("Encryption");
|
|
if (encryptionConfiguration.Exists())
|
|
{
|
|
options.DefaultPassPhrase = encryptionConfiguration["PassPhrase"] ?? options.DefaultPassPhrase;
|
|
options.DefaultSalt = encryptionConfiguration.GetSection("Salt").Exists()
|
|
? Encoding.ASCII.GetBytes(encryptionConfiguration["Salt"])
|
|
: options.DefaultSalt;
|
|
options.InitVectorBytes = encryptionConfiguration.GetSection("InitVector").Exists()
|
|
? Encoding.ASCII.GetBytes(encryptionConfiguration["InitVector"])
|
|
: options.InitVectorBytes;
|
|
}
|
|
});
|
|
|
|
Configure<PermissionManagementOptions>(options =>
|
|
{
|
|
// Rename IdentityServer.Client.ManagePermissions
|
|
// See https://github.com/abpframework/abp/blob/dev/modules/identityserver/src/Volo.Abp.PermissionManagement.Domain.IdentityServer/Volo/Abp/PermissionManagement/IdentityServer/AbpPermissionManagementDomainIdentityServerModule.cs
|
|
options.ProviderPolicies[ClientPermissionValueProvider.ProviderName] =
|
|
LINGYUN.Abp.IdentityServer.AbpIdentityServerPermissions.Clients.ManagePermissions;
|
|
});
|
|
|
|
// 自定义需要处理的异常
|
|
Configure<AbpExceptionHandlingOptions>(options =>
|
|
{
|
|
// 加入需要处理的异常类型
|
|
options.Handlers.Add<Volo.Abp.Data.AbpDbConcurrencyException>();
|
|
options.Handlers.Add<AbpInitializationException>();
|
|
options.Handlers.Add<ObjectDisposedException>();
|
|
options.Handlers.Add<StackOverflowException>();
|
|
options.Handlers.Add<OutOfMemoryException>();
|
|
options.Handlers.Add<System.Data.Common.DbException>();
|
|
options.Handlers.Add<Microsoft.EntityFrameworkCore.DbUpdateException>();
|
|
options.Handlers.Add<System.Data.DBConcurrencyException>();
|
|
});
|
|
// 自定义需要发送邮件通知的异常类型
|
|
Configure<AbpEmailExceptionHandlingOptions>(options =>
|
|
{
|
|
// 是否发送堆栈信息
|
|
options.SendStackTrace = true;
|
|
// 未指定异常接收者的默认接收邮件
|
|
// 请指定自己的邮件地址
|
|
// options.DefaultReceiveEmail = "colin.in@foxmail.com";
|
|
});
|
|
|
|
|
|
Configure<AbpDistributedCacheOptions>(options =>
|
|
{
|
|
// 最好统一命名,不然某个缓存变动其他应用服务有例外发生
|
|
options.KeyPrefix = "LINGYUN.Abp.Application";
|
|
// 滑动过期30天
|
|
options.GlobalCacheEntryOptions.SlidingExpiration = TimeSpan.FromDays(30);
|
|
// 绝对过期60天
|
|
options.GlobalCacheEntryOptions.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(60);
|
|
});
|
|
|
|
Configure<AbpDistributedEntityEventOptions>(options =>
|
|
{
|
|
options.AutoEventSelectors.AddNamespace("Volo.Abp.TenantManagement");
|
|
});
|
|
|
|
Configure<RedisCacheOptions>(options =>
|
|
{
|
|
var redisConfig = ConfigurationOptions.Parse(options.Configuration);
|
|
options.ConfigurationOptions = redisConfig;
|
|
options.InstanceName = configuration["Redis:InstanceName"];
|
|
});
|
|
|
|
Configure<AbpVirtualFileSystemOptions>(options =>
|
|
{
|
|
options.FileSets.AddEmbedded<BackendAdminHostModule>("LINGYUN.Abp.BackendAdmin");
|
|
});
|
|
|
|
// 多租户
|
|
Configure<AbpMultiTenancyOptions>(options =>
|
|
{
|
|
options.IsEnabled = true;
|
|
});
|
|
|
|
var tenantResolveCfg = configuration.GetSection("App:Domains");
|
|
if (tenantResolveCfg.Exists())
|
|
{
|
|
Configure<AbpTenantResolveOptions>(options =>
|
|
{
|
|
var domains = tenantResolveCfg.Get<string[]>();
|
|
foreach (var domain in domains)
|
|
{
|
|
options.AddDomainTenantResolver(domain);
|
|
}
|
|
});
|
|
}
|
|
|
|
Configure<AbpAuditingOptions>(options =>
|
|
{
|
|
options.ApplicationName = "Backend-Admin";
|
|
// 是否启用实体变更记录
|
|
var entitiesChangedConfig = configuration.GetSection("App:TrackingEntitiesChanged");
|
|
if (entitiesChangedConfig.Exists() && entitiesChangedConfig.Get<bool>())
|
|
{
|
|
options
|
|
.EntityHistorySelectors
|
|
.AddAllEntities();
|
|
}
|
|
});
|
|
|
|
// Swagger
|
|
context.Services.AddSwaggerGen(
|
|
options =>
|
|
{
|
|
options.SwaggerDoc("v1", new OpenApiInfo { Title = "BackendAdmin API", Version = "v1" });
|
|
options.DocInclusionPredicate((docName, description) => true);
|
|
options.CustomSchemaIds(type => type.FullName);
|
|
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
|
{
|
|
Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
|
|
Name = "Authorization",
|
|
In = ParameterLocation.Header,
|
|
Scheme = "bearer",
|
|
Type = SecuritySchemeType.Http,
|
|
BearerFormat = "JWT"
|
|
});
|
|
options.AddSecurityRequirement(new OpenApiSecurityRequirement
|
|
{
|
|
{
|
|
new OpenApiSecurityScheme
|
|
{
|
|
Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" }
|
|
},
|
|
new string[] { }
|
|
}
|
|
});
|
|
});
|
|
|
|
// 支持本地化语言类型
|
|
Configure<AbpLocalizationOptions>(options =>
|
|
{
|
|
options.Languages.Add(new LanguageInfo("en", "en", "English"));
|
|
options.Languages.Add(new LanguageInfo("zh-Hans", "zh-Hans", "简体中文"));
|
|
|
|
options.Resources
|
|
.Get<IdentityResource>()
|
|
.AddVirtualJson("/Localization");
|
|
options
|
|
.AddLanguagesMapOrUpdate(
|
|
"vue-admin-element-ui",
|
|
new NameValue("zh-Hans", "zh"),
|
|
new NameValue("en", "en"));
|
|
options
|
|
.AddLanguageFilesMapOrUpdate(
|
|
"vue-admin-element-ui",
|
|
new NameValue("zh-Hans", "zh"),
|
|
new NameValue("en", "en"));
|
|
|
|
options.Resources.AddDynamic();
|
|
});
|
|
|
|
Configure<AbpClaimsMapOptions>(options =>
|
|
{
|
|
options.Maps.TryAdd("name", () => AbpClaimTypes.UserName);
|
|
});
|
|
|
|
context.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
.AddJwtBearer(options =>
|
|
{
|
|
options.Authority = configuration["AuthServer:Authority"];
|
|
options.RequireHttpsMetadata = false;
|
|
options.Audience = configuration["AuthServer:ApiName"];
|
|
});
|
|
|
|
if (!hostingEnvironment.IsDevelopment())
|
|
{
|
|
var redis = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]);
|
|
context.Services
|
|
.AddDataProtection()
|
|
.PersistKeysToStackExchangeRedis(redis, "BackendAdmin-Protection-Keys");
|
|
}
|
|
}
|
|
|
|
public override void OnApplicationInitialization(ApplicationInitializationContext context)
|
|
{
|
|
var app = context.GetApplicationBuilder();
|
|
// http调用链
|
|
app.UseCorrelationId();
|
|
// 虚拟文件系统
|
|
app.UseStaticFiles();
|
|
// 本地化
|
|
app.UseAbpRequestLocalization();
|
|
//路由
|
|
app.UseRouting();
|
|
// 认证
|
|
app.UseAuthentication();
|
|
// jwt
|
|
app.UseJwtTokenMiddleware();
|
|
// 多租户
|
|
app.UseMultiTenancy();
|
|
// Swagger
|
|
app.UseSwagger();
|
|
// Swagger可视化界面
|
|
app.UseSwaggerUI(options =>
|
|
{
|
|
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Support BackendAdmin API");
|
|
});
|
|
// 审计日志
|
|
app.UseAuditing();
|
|
// 处理微信消息
|
|
// app.UseWeChatSignature();
|
|
// 路由
|
|
app.UseConfiguredEndpoints();
|
|
|
|
// 调试代理连接信息用,上线后注释掉
|
|
app.UseProxyConnectTest();
|
|
|
|
if (context.GetEnvironment().IsDevelopment())
|
|
{
|
|
SeedData(context);
|
|
}
|
|
}
|
|
|
|
private void SeedData(ApplicationInitializationContext context)
|
|
{
|
|
AsyncHelper.RunSync(async () =>
|
|
{
|
|
using var scope = context.ServiceProvider.CreateScope();
|
|
await scope.ServiceProvider.GetRequiredService<IDataSeeder>().SeedAsync();
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|