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.
462 lines
16 KiB
462 lines
16 KiB
using DotNetCore.CAP;
|
|
using LINGYUN.Abp.BlobStoring.OssManagement;
|
|
using LINGYUN.Abp.Localization.CultureMap;
|
|
using LINGYUN.Abp.LocalizationManagement;
|
|
using LINGYUN.Abp.OpenIddict.AspNetCore.Session;
|
|
using LINGYUN.Abp.OpenIddict.LinkUser;
|
|
using LINGYUN.Abp.OpenIddict.Portal;
|
|
using LINGYUN.Abp.OpenIddict.Sms;
|
|
using LINGYUN.Abp.OpenIddict.WeChat;
|
|
using LINGYUN.Abp.Serilog.Enrichers.Application;
|
|
using LINGYUN.Abp.Serilog.Enrichers.UniqueId;
|
|
using LINGYUN.Abp.WeChat.Work;
|
|
using LINGYUN.Abp.Wrapper;
|
|
using LY.MicroService.AuthServer.Ui.Branding;
|
|
using Medallion.Threading;
|
|
using Medallion.Threading.Redis;
|
|
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Cors;
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
using Microsoft.AspNetCore.Extensions.DependencyInjection;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Routing;
|
|
using Microsoft.Extensions.Caching.StackExchangeRedis;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.IdentityModel.Logging;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using OpenIddict.Validation.AspNetCore;
|
|
using StackExchange.Redis;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text.Encodings.Web;
|
|
using System.Text.Unicode;
|
|
using Volo.Abp.Account.Localization;
|
|
using Volo.Abp.AspNetCore.Mvc;
|
|
using Volo.Abp.Auditing;
|
|
using Volo.Abp.BlobStoring;
|
|
using Volo.Abp.Caching;
|
|
using Volo.Abp.FeatureManagement;
|
|
using Volo.Abp.GlobalFeatures;
|
|
using Volo.Abp.Http.Client;
|
|
using Volo.Abp.Json;
|
|
using Volo.Abp.Json.SystemTextJson;
|
|
using Volo.Abp.Localization;
|
|
using Volo.Abp.MultiTenancy;
|
|
using Volo.Abp.OpenIddict;
|
|
using Volo.Abp.OpenIddict.WildcardDomains;
|
|
using Volo.Abp.Security.Claims;
|
|
using Volo.Abp.SettingManagement;
|
|
using Volo.Abp.Threading;
|
|
using Volo.Abp.Timing;
|
|
using Volo.Abp.UI.Navigation.Urls;
|
|
using Volo.Abp.VirtualFileSystem;
|
|
|
|
namespace LY.MicroService.AuthServer;
|
|
|
|
public partial class AuthServerModule
|
|
{
|
|
public static string ApplicationName { get; set; } = "AuthServer";
|
|
private readonly static OneTimeRunner OneTimeRunner = new OneTimeRunner();
|
|
|
|
private void PreConfigureFeature()
|
|
{
|
|
OneTimeRunner.Run(() =>
|
|
{
|
|
GlobalFeatureManager.Instance.Modules.Editions().EnableAll();
|
|
});
|
|
}
|
|
|
|
private void PreForwardedHeaders()
|
|
{
|
|
}
|
|
|
|
private void PreConfigureApp(IConfiguration configuration)
|
|
{
|
|
AbpSerilogEnrichersConsts.ApplicationName = ApplicationName;
|
|
|
|
PreConfigure<AbpSerilogEnrichersUniqueIdOptions>(options =>
|
|
{
|
|
// 以开放端口区别,应在0-31之间
|
|
options.SnowflakeIdOptions.WorkerId = 1;
|
|
options.SnowflakeIdOptions.WorkerIdBits = 5;
|
|
options.SnowflakeIdOptions.DatacenterId = 1;
|
|
});
|
|
|
|
if (configuration.GetValue<bool>("App:ShowPii"))
|
|
{
|
|
IdentityModelEventSource.ShowPII = true;
|
|
}
|
|
}
|
|
|
|
private void PreConfigureCAP(IConfiguration configuration)
|
|
{
|
|
PreConfigure<CapOptions>(options =>
|
|
{
|
|
options
|
|
.UseMySql(mySqlOptions =>
|
|
{
|
|
configuration.GetSection("CAP:MySql").Bind(mySqlOptions);
|
|
})
|
|
.UseRabbitMQ(rabbitMQOptions =>
|
|
{
|
|
configuration.GetSection("CAP:RabbitMQ").Bind(rabbitMQOptions);
|
|
})
|
|
.UseDashboard();
|
|
});
|
|
}
|
|
|
|
private void PreConfigureAuth()
|
|
{
|
|
PreConfigure<OpenIddictBuilder>(builder =>
|
|
{
|
|
builder.AddValidation(options =>
|
|
{
|
|
//options.AddAudiences("lingyun-abp-application");
|
|
|
|
options.UseLocalServer();
|
|
|
|
options.UseAspNetCore();
|
|
|
|
options.UseDataProtection();
|
|
});
|
|
});
|
|
}
|
|
|
|
private void PreConfigureCertificate(IConfiguration configuration, IWebHostEnvironment environment)
|
|
{
|
|
if (!environment.IsDevelopment())
|
|
{
|
|
PreConfigure<AbpOpenIddictAspNetCoreOptions>(options =>
|
|
{
|
|
options.AddDevelopmentEncryptionAndSigningCertificate = false;
|
|
});
|
|
|
|
PreConfigure<OpenIddictServerBuilder>(builder =>
|
|
{
|
|
builder.AddProductionEncryptionAndSigningCertificate("openiddict.pfx", "e1c48393-0c43-11f0-9582-4aecacda42db");
|
|
});
|
|
}
|
|
|
|
PreConfigure<OpenIddictServerBuilder>(builder =>
|
|
{
|
|
// builder.UseDataProtection();
|
|
|
|
// 禁用https
|
|
builder.UseAspNetCore()
|
|
.DisableTransportSecurityRequirement();
|
|
});
|
|
|
|
var wildcardDomains = configuration.GetSection("App:WildcardDomains").Get<List<string>>();
|
|
if (wildcardDomains?.Count > 0)
|
|
{
|
|
PreConfigure<AbpOpenIddictWildcardDomainOptions>(options =>
|
|
{
|
|
options.EnableWildcardDomainSupport = true;
|
|
options.WildcardDomainsFormat.AddIfNotContains(wildcardDomains);
|
|
});
|
|
}
|
|
}
|
|
|
|
private void ConfigureMvc(IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
Configure<AbpAspNetCoreMvcOptions>(options =>
|
|
{
|
|
options.ExposeIntegrationServices = true;
|
|
});
|
|
|
|
Configure<AbpEndpointRouterOptions>(options =>
|
|
{
|
|
options.EndpointConfigureActions.Add((builder) =>
|
|
{
|
|
builder.Endpoints.MapHealthChecks(configuration["App:HealthChecks"] ?? "/healthz");
|
|
});
|
|
});
|
|
|
|
services.AddHealthChecks();
|
|
}
|
|
|
|
private void ConfigureDataSeeder()
|
|
{
|
|
|
|
}
|
|
|
|
private void ConfigureFeatureManagement()
|
|
{
|
|
Configure<FeatureManagementOptions>(options =>
|
|
{
|
|
options.IsDynamicFeatureStoreEnabled = true;
|
|
});
|
|
}
|
|
|
|
private void ConfigureSettingManagement()
|
|
{
|
|
Configure<SettingManagementOptions>(options =>
|
|
{
|
|
options.IsDynamicSettingStoreEnabled = true;
|
|
});
|
|
}
|
|
|
|
private void ConfigureJsonSerializer(IConfiguration configuration)
|
|
{
|
|
// 统一时间日期格式
|
|
Configure<AbpJsonOptions>(options =>
|
|
{
|
|
var jsonConfiguration = configuration.GetSection("Json");
|
|
if (jsonConfiguration.Exists())
|
|
{
|
|
jsonConfiguration.Bind(options);
|
|
}
|
|
});
|
|
// 中文序列化的编码问题
|
|
Configure<AbpSystemTextJsonSerializerOptions>(options =>
|
|
{
|
|
options.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All);
|
|
});
|
|
}
|
|
|
|
private void ConfigureDistributedLocking(IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
var distributedLockEnabled = configuration["DistributedLock:IsEnabled"];
|
|
if (distributedLockEnabled.IsNullOrEmpty() || bool.Parse(distributedLockEnabled))
|
|
{
|
|
var redis = ConnectionMultiplexer.Connect(configuration["DistributedLock:Redis:Configuration"]);
|
|
services.AddSingleton<IDistributedLockProvider>(_ => new RedisDistributedSynchronizationProvider(redis.GetDatabase()));
|
|
}
|
|
}
|
|
|
|
private void ConfigureBranding(IConfiguration configuration)
|
|
{
|
|
Configure<AccountBrandingOptions>(options =>
|
|
{
|
|
configuration.GetSection("App:Branding").Bind(options);
|
|
});
|
|
}
|
|
|
|
private void ConfigureBlobStoring(IConfiguration configuration)
|
|
{
|
|
Configure<AbpBlobStoringOptions>(options =>
|
|
{
|
|
options.Containers.ConfigureAll((containerName, containerConfiguration) =>
|
|
{
|
|
containerConfiguration.UseOssManagement(config =>
|
|
{
|
|
configuration.GetSection("OssManagement").Bind(config);
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
private void ConfigureCaching(IConfiguration configuration)
|
|
{
|
|
Configure<AbpDistributedCacheOptions>(options =>
|
|
{
|
|
configuration.GetSection("DistributedCache").Bind(options);
|
|
});
|
|
|
|
Configure<RedisCacheOptions>(options =>
|
|
{
|
|
var redisConfig = ConfigurationOptions.Parse(options.Configuration);
|
|
options.ConfigurationOptions = redisConfig;
|
|
options.InstanceName = configuration["Redis:InstanceName"];
|
|
});
|
|
}
|
|
private void ConfigureIdentity(IConfiguration configuration)
|
|
{
|
|
// 增加配置文件定义,在新建租户时需要
|
|
Configure<IdentityOptions>(options =>
|
|
{
|
|
var identityConfiguration = configuration.GetSection("Identity");
|
|
if (identityConfiguration.Exists())
|
|
{
|
|
identityConfiguration.Bind(options);
|
|
}
|
|
});
|
|
Configure<AbpClaimsPrincipalFactoryOptions>(options =>
|
|
{
|
|
options.IsDynamicClaimsEnabled = true;
|
|
options.IsRemoteRefreshEnabled = false;
|
|
});
|
|
|
|
Configure<AbpOpenIddictAspNetCoreSessionOptions>(options =>
|
|
{
|
|
options.PersistentSessionGrantTypes.Add(SmsTokenExtensionGrantConsts.GrantType);
|
|
options.PersistentSessionGrantTypes.Add(PortalTokenExtensionGrantConsts.GrantType);
|
|
options.PersistentSessionGrantTypes.Add(LinkUserTokenExtensionGrantConsts.GrantType);
|
|
options.PersistentSessionGrantTypes.Add(WeChatTokenExtensionGrantConsts.OfficialGrantType);
|
|
options.PersistentSessionGrantTypes.Add(WeChatTokenExtensionGrantConsts.MiniProgramGrantType);
|
|
options.PersistentSessionGrantTypes.Add(AbpWeChatWorkGlobalConsts.GrantType);
|
|
});
|
|
}
|
|
private void ConfigureVirtualFileSystem()
|
|
{
|
|
Configure<AbpVirtualFileSystemOptions>(options =>
|
|
{
|
|
options.FileSets.AddEmbedded<AuthServerModule>("LY.MicroService.AuthServer");
|
|
});
|
|
}
|
|
private void ConfigureLocalization()
|
|
{
|
|
Configure<AbpLocalizationOptions>(options =>
|
|
{
|
|
options.Languages.Add(new LanguageInfo("en", "en", "English"));
|
|
options.Languages.Add(new LanguageInfo("zh-Hans", "zh-Hans", "简体中文"));
|
|
|
|
options.Resources
|
|
.Get<AccountResource>()
|
|
.AddVirtualJson("/Localization/Resources");
|
|
});
|
|
|
|
Configure<AbpLocalizationCultureMapOptions>(options =>
|
|
{
|
|
var zhHansCultureMapInfo = new CultureMapInfo
|
|
{
|
|
TargetCulture = "zh-Hans",
|
|
SourceCultures = new string[] { "zh", "zh_CN", "zh-CN" }
|
|
};
|
|
|
|
options.CulturesMaps.Add(zhHansCultureMapInfo);
|
|
options.UiCulturesMaps.Add(zhHansCultureMapInfo);
|
|
});
|
|
|
|
Configure<AbpLocalizationManagementOptions>(options =>
|
|
{
|
|
options.SaveStaticLocalizationsToDatabase = true;
|
|
});
|
|
}
|
|
private void ConfigureTiming(IConfiguration configuration)
|
|
{
|
|
Configure<AbpClockOptions>(options =>
|
|
{
|
|
configuration.GetSection("Clock").Bind(options);
|
|
});
|
|
}
|
|
|
|
private void ConfigureAuditing(IConfiguration configuration)
|
|
{
|
|
Configure<AbpAuditingOptions>(options =>
|
|
{
|
|
// options.IsEnabledForGetRequests = true;
|
|
options.ApplicationName = ApplicationName;
|
|
// 是否启用实体变更记录
|
|
var allEntitiesSelectorIsEnabled = configuration["Auditing:AllEntitiesSelector"];
|
|
if (allEntitiesSelectorIsEnabled.IsNullOrWhiteSpace() ||
|
|
(bool.TryParse(allEntitiesSelectorIsEnabled, out var enabled) && enabled))
|
|
{
|
|
options.EntityHistorySelectors.AddAllEntities();
|
|
}
|
|
});
|
|
}
|
|
private void ConfigureUrls(IConfiguration configuration)
|
|
{
|
|
Configure<AppUrlOptions>(options =>
|
|
{
|
|
var applicationConfiguration = configuration.GetSection("App:Urls:Applications");
|
|
foreach (var appConfig in applicationConfiguration.GetChildren())
|
|
{
|
|
options.Applications[appConfig.Key].RootUrl = appConfig["RootUrl"];
|
|
foreach (var urlsConfig in appConfig.GetSection("Urls").GetChildren())
|
|
{
|
|
options.Applications[appConfig.Key].Urls[urlsConfig.Key] = urlsConfig.Value;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
private void ConfigureSecurity(IServiceCollection services, IConfiguration configuration, bool isDevelopment = false)
|
|
{
|
|
services.ForwardIdentityAuthenticationForBearer(OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme);
|
|
|
|
services
|
|
.AddAuthentication()
|
|
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
|
|
{
|
|
options.ExpireTimeSpan = TimeSpan.FromDays(365);
|
|
})
|
|
.AddJwtBearer(options =>
|
|
{
|
|
configuration.GetSection("AuthServer").Bind(options);
|
|
|
|
var validIssuers = configuration.GetSection("AuthServer:ValidIssuers").Get<List<string>>();
|
|
if (validIssuers?.Count > 0)
|
|
{
|
|
options.TokenValidationParameters.ValidIssuers = validIssuers;
|
|
options.TokenValidationParameters.IssuerValidator = TokenWildcardIssuerValidator.IssuerValidator;
|
|
}
|
|
});
|
|
//.AddWeChatWork(options =>
|
|
//{
|
|
// options.SignInScheme = IdentityConstants.ExternalScheme;
|
|
//});
|
|
|
|
if (!isDevelopment)
|
|
{
|
|
var redis = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]);
|
|
services
|
|
.AddDataProtection()
|
|
.SetApplicationName("LINGYUN.Abp.Application")
|
|
.PersistKeysToStackExchangeRedis(redis, "LINGYUN.Abp.Application:DataProtection:Protection-Keys");
|
|
}
|
|
services.AddSameSiteCookiePolicy();
|
|
}
|
|
private void ConfigureMultiTenancy(IConfiguration configuration)
|
|
{
|
|
// 多租户
|
|
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);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
private void ConfigureCors(IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
services.AddCors(options =>
|
|
{
|
|
options.AddDefaultPolicy(builder =>
|
|
{
|
|
builder
|
|
.WithOrigins(
|
|
configuration["App:CorsOrigins"]
|
|
.Split(",", StringSplitOptions.RemoveEmptyEntries)
|
|
.Select(o => o.RemovePostFix("/"))
|
|
.ToArray()
|
|
)
|
|
.WithAbpExposedHeaders()
|
|
.WithAbpWrapExposedHeaders()
|
|
.SetIsOriginAllowedToAllowWildcardSubdomains()
|
|
.AllowAnyHeader()
|
|
.AllowAnyMethod()
|
|
.AllowCredentials();
|
|
});
|
|
});
|
|
}
|
|
|
|
private void PreConfigureWrapper()
|
|
{
|
|
PreConfigure<AbpHttpClientBuilderOptions>(options =>
|
|
{
|
|
// http服务间调用发送不需要包装结果的请求头
|
|
options.ProxyClientActions.Add(
|
|
(_, _, client) =>
|
|
{
|
|
client.DefaultRequestHeaders.TryAddWithoutValidation(AbpHttpWrapConsts.AbpDontWrapResult, "true");
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|