From 819b38d35c5cca8dd6756ccf32a67bf79ad537ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=86=9B?= <510423039@qq.com> Date: Wed, 13 Oct 2021 18:21:46 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20fix:=20=E4=BF=AE=E6=94=B9IdentitySe?= =?UTF-8?q?rver=E7=BC=93=E5=AD=98=E4=B8=8D=E4=B8=80=E8=87=B4=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../appsettings.Development.json | 3 - .../ProjectNameIdentityServerModule.cs | 167 ++++++++++-------- .../appsettings.Development.json | 27 +-- .../IdenityServerApiResourceManager.cs | 11 +- .../IdenityServerClientManager.cs | 1 - 5 files changed, 114 insertions(+), 95 deletions(-) diff --git a/aspnet-core/services/host/CompanyName.ProjectName.HttpApi.Host/appsettings.Development.json b/aspnet-core/services/host/CompanyName.ProjectName.HttpApi.Host/appsettings.Development.json index dd6b2314..29ae496c 100644 --- a/aspnet-core/services/host/CompanyName.ProjectName.HttpApi.Host/appsettings.Development.json +++ b/aspnet-core/services/host/CompanyName.ProjectName.HttpApi.Host/appsettings.Development.json @@ -24,9 +24,6 @@ "DatabaseId": 0 } }, - "StringEncryption": { - "DefaultPassPhrase": "YFLtqf75HkDnKR0n" - }, "Jwt": { "Audience": "CompanyNameProjectName", //客户端标识 diff --git a/aspnet-core/services/host/CompanyName.ProjectName.IdentityServer/ProjectNameIdentityServerModule.cs b/aspnet-core/services/host/CompanyName.ProjectName.IdentityServer/ProjectNameIdentityServerModule.cs index b2b48991..0a5aa425 100644 --- a/aspnet-core/services/host/CompanyName.ProjectName.IdentityServer/ProjectNameIdentityServerModule.cs +++ b/aspnet-core/services/host/CompanyName.ProjectName.IdentityServer/ProjectNameIdentityServerModule.cs @@ -14,6 +14,7 @@ using CompanyName.ProjectName.MultiTenancy; using IdentityServer4.Configuration; using IdentityServer4.Extensions; using IdentityServer4.Models; +using Microsoft.Extensions.Configuration; using StackExchange.Redis; using Volo.Abp; using Volo.Abp.Account; @@ -56,48 +57,53 @@ namespace CompanyName.ProjectName var hostingEnvironment = context.Services.GetHostingEnvironment(); var configuration = context.Services.GetConfiguration(); context.Services.AddSameSiteCookiePolicy(); - Configure(options => - { - options.Resources - .Get() - .AddBaseTypes( - typeof(AbpUiResource) - ); - - options.Languages.Add(new LanguageInfo("ar", "ar", "العربية")); - options.Languages.Add(new LanguageInfo("cs", "cs", "Čeština")); - options.Languages.Add(new LanguageInfo("en", "en", "English")); - options.Languages.Add(new LanguageInfo("en-GB", "en-GB", "English (UK)")); - options.Languages.Add(new LanguageInfo("fr", "fr", "Français")); - options.Languages.Add(new LanguageInfo("hu", "hu", "Magyar")); - options.Languages.Add(new LanguageInfo("pt-BR", "pt-BR", "Português")); - options.Languages.Add(new LanguageInfo("ru", "ru", "Русский")); - options.Languages.Add(new LanguageInfo("tr", "tr", "Türkçe")); - options.Languages.Add(new LanguageInfo("zh-Hans", "zh-Hans", "简体中文")); - options.Languages.Add(new LanguageInfo("zh-Hant", "zh-Hant", "繁體中文")); - options.Languages.Add(new LanguageInfo("de-DE", "de-DE", "Deutsch", "de")); - options.Languages.Add(new LanguageInfo("es", "es", "Español", "es")); - }); + ConfigureLocalization(); + ConfigureVirtualFileSystem(context); + ConfigureCache(context); + ConfigureCors(context, configuration); + ConfigureOther(configuration); + } - Configure(options => + public override void OnApplicationInitialization(ApplicationInitializationContext context) + { + var app = context.GetApplicationBuilder(); + var env = context.GetEnvironment(); + + if (env.IsDevelopment()) { - options.StyleBundles.Configure( - BasicThemeBundles.Styles.Global, - bundle => { bundle.AddFiles("/global-styles.css"); } - ); - }); + app.UseDeveloperExceptionPage(); + } - Configure(options => + app.UseAbpRequestLocalization(); + + if (!env.IsDevelopment()) { - //options.IsEnabledForGetRequests = true; - options.ApplicationName = "AuthServer"; - }); + app.UseErrorPage(); + } - if (hostingEnvironment.IsDevelopment()) + app.UseCorrelationId(); + app.UseStaticFiles(); + app.UseRouting(); + app.UseCors(DefaultCorsPolicyName); + app.UseCookiePolicy(); + app.UseAuthentication(); + + if (MultiTenancyConsts.IsEnabled) { - Configure(options => { options.FileSets.AddEmbedded(); }); + app.UseMultiTenancy(); } + app.UseUnitOfWork(); + app.UseIdentityServer(); + app.UseAuthorization(); + app.UseAuditing(); + app.UseAbpSerilogEnrichers(); + app.UseConfiguredEndpoints(); + } + + private void ConfigureOther(IConfiguration configuration) + + { Configure(options => { options.Applications["MVC"].RootUrl = configuration["App:SelfUrl"]; @@ -106,19 +112,16 @@ namespace CompanyName.ProjectName options.Applications["Angular"].RootUrl = configuration["App:ClientUrl"]; options.Applications["Angular"].Urls[AccountUrlNames.PasswordReset] = "account/reset-password"; }); - Configure(options => { options.IsJobExecutionEnabled = false; }); - - Configure(options => { options.KeyPrefix = "ProjectName:"; }); - - if (!hostingEnvironment.IsDevelopment()) + Configure(options => { - var redis = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]); - context.Services - .AddDataProtection() - .PersistKeysToStackExchangeRedis(redis, "ProjectName-Protection-Keys"); - } + //options.IsEnabledForGetRequests = true; + options.ApplicationName = "AuthServer"; + }); + } + private void ConfigureCors(ServiceConfigurationContext context, IConfiguration configuration) + { context.Services.AddCors(options => { options.AddPolicy(DefaultCorsPolicyName, builder => @@ -139,42 +142,60 @@ namespace CompanyName.ProjectName }); } - public override void OnApplicationInitialization(ApplicationInitializationContext context) + private void ConfigureLocalization() { - var app = context.GetApplicationBuilder(); - var env = context.GetEnvironment(); - - if (env.IsDevelopment()) + Configure(options => { - app.UseDeveloperExceptionPage(); - } + options.Resources + .Get() + .AddBaseTypes( + typeof(AbpUiResource) + ); - app.UseAbpRequestLocalization(); + options.Languages.Add(new LanguageInfo("ar", "ar", "العربية")); + options.Languages.Add(new LanguageInfo("cs", "cs", "Čeština")); + options.Languages.Add(new LanguageInfo("en", "en", "English")); + options.Languages.Add(new LanguageInfo("en-GB", "en-GB", "English (UK)")); + options.Languages.Add(new LanguageInfo("fr", "fr", "Français")); + options.Languages.Add(new LanguageInfo("hu", "hu", "Magyar")); + options.Languages.Add(new LanguageInfo("pt-BR", "pt-BR", "Português")); + options.Languages.Add(new LanguageInfo("ru", "ru", "Русский")); + options.Languages.Add(new LanguageInfo("tr", "tr", "Türkçe")); + options.Languages.Add(new LanguageInfo("zh-Hans", "zh-Hans", "简体中文")); + options.Languages.Add(new LanguageInfo("zh-Hant", "zh-Hant", "繁體中文")); + options.Languages.Add(new LanguageInfo("de-DE", "de-DE", "Deutsch", "de")); + options.Languages.Add(new LanguageInfo("es", "es", "Español", "es")); + }); + } - if (!env.IsDevelopment()) - { - app.UseErrorPage(); - } + /// + /// Redis缓存 + /// + private void ConfigureCache(ServiceConfigurationContext context) + { + var redisConnectionString = + context.Services.GetConfiguration().GetValue("Cache:Redis:ConnectionString"); + var redisDatabaseId = context.Services.GetConfiguration().GetValue("Cache:Redis:DatabaseId"); + var password = context.Services.GetConfiguration().GetValue("Cache:Redis:Password"); + var connectString = $"{redisConnectionString},password={password},defaultdatabase={redisDatabaseId}"; + var redis = ConnectionMultiplexer.Connect(connectString); + context.Services.AddStackExchangeRedisCache(options => { options.Configuration = connectString; }); + context.Services + .AddDataProtection() + .PersistKeysToStackExchangeRedis(redis, "ProjectName-Protection-Keys"); + } - app.UseCorrelationId(); - app.UseStaticFiles(); - app.UseRouting(); - app.UseCors(DefaultCorsPolicyName); - app.UseCookiePolicy(); - app.UseAuthentication(); + private void ConfigureVirtualFileSystem(ServiceConfigurationContext context) + { + Configure(options => { options.FileSets.AddEmbedded(); }); - if (MultiTenancyConsts.IsEnabled) + Configure(options => { - app.UseMultiTenancy(); - } - - app.UseUnitOfWork(); - app.UseIdentityServer(); - app.UseAuthorization(); - app.UseAuditing(); - app.UseAbpSerilogEnrichers(); - app.UseConfiguredEndpoints(); - + options.StyleBundles.Configure( + BasicThemeBundles.Styles.Global, + bundle => { bundle.AddFiles("/global-styles.css"); } + ); + }); } } } \ No newline at end of file diff --git a/aspnet-core/services/host/CompanyName.ProjectName.IdentityServer/appsettings.Development.json b/aspnet-core/services/host/CompanyName.ProjectName.IdentityServer/appsettings.Development.json index 0a1454fc..038c2b51 100644 --- a/aspnet-core/services/host/CompanyName.ProjectName.IdentityServer/appsettings.Development.json +++ b/aspnet-core/services/host/CompanyName.ProjectName.IdentityServer/appsettings.Development.json @@ -1,18 +1,19 @@ { - "App": { - "SelfUrl": "https://localhost:44354", - "ClientUrl": "http://localhost:4200", - "CorsOrigins": "https://*.ProjectName.com,http://localhost:4200,https://localhost:44307,https://localhost:44315", - "RedirectAllowedUrls": "http://localhost:4200,https://localhost:44307" - }, - "ConnectionStrings": { - "Default": "Data Source=localhost;Database=CompanyNameProjectNameDB;uid=root;pwd=1q2w3E*;charset=utf8mb4;Allow User Variables=true;AllowLoadLocalInfile=true" - }, + "App": { + "SelfUrl": "https://localhost:44354", + "ClientUrl": "http://localhost:4200", + "CorsOrigins": "https://*.ProjectName.com,http://localhost:4200,https://localhost:44307,https://localhost:44315", + "RedirectAllowedUrls": "http://localhost:4200,https://localhost:44307" + }, + "ConnectionStrings": { + "Default": "Data Source=localhost;Database=CompanyNameProjectNameDB;uid=root;pwd=1q2w3E*;charset=utf8mb4;Allow User Variables=true;AllowLoadLocalInfile=true" + }, + "Cache": { "Redis": { - "Configuration": "localhost,password=mypassword" - }, - "StringEncryption": { - "DefaultPassPhrase": "YFLtqf75HkDnKR0n" + "ConnectionString": "localhost", + "Password": "mypassword", + "DatabaseId": 0 } } +} diff --git a/aspnet-core/services/src/CompanyName.ProjectName.Domain/IdentityServer/IdenityServerApiResourceManager.cs b/aspnet-core/services/src/CompanyName.ProjectName.Domain/IdentityServer/IdenityServerApiResourceManager.cs index 2acd0e42..232d627d 100644 --- a/aspnet-core/services/src/CompanyName.ProjectName.Domain/IdentityServer/IdenityServerApiResourceManager.cs +++ b/aspnet-core/services/src/CompanyName.ProjectName.Domain/IdentityServer/IdenityServerApiResourceManager.cs @@ -119,18 +119,19 @@ namespace CompanyName.ProjectName.IdentityServer apiResource.ShowInDiscoveryDocument = showInDiscoveryDocument; if (secret.IsNotNullOrWhiteSpace()) { + // 判读密钥是否一样 if (apiResource.Secrets.Count > 0) { - apiResource.AddSecret(secret.ToSha256()); - } - else - { - if (apiResource.Secrets.Any(e => e.Value != secret)) + if (apiResource.Secrets.First().Value != secret) { apiResource.Secrets.Clear(); apiResource.AddSecret(secret.ToSha256()); } } + else + { + apiResource.AddSecret(secret.ToSha256()); + } } diff --git a/aspnet-core/services/src/CompanyName.ProjectName.Domain/IdentityServer/IdenityServerClientManager.cs b/aspnet-core/services/src/CompanyName.ProjectName.Domain/IdentityServer/IdenityServerClientManager.cs index 278d29d3..a7ed4a31 100644 --- a/aspnet-core/services/src/CompanyName.ProjectName.Domain/IdentityServer/IdenityServerClientManager.cs +++ b/aspnet-core/services/src/CompanyName.ProjectName.Domain/IdentityServer/IdenityServerClientManager.cs @@ -151,7 +151,6 @@ namespace CompanyName.ProjectName.IdentityServer { if (client.ClientSecrets.Count == 0) { - client.AddSecret(secret.ToSha256(), null, secretType, String.Empty); } else