diff --git a/aspnet-core/services/LY.MicroService.BackendAdmin.HttpApi.Host/appsettings.Development.json b/aspnet-core/services/LY.MicroService.BackendAdmin.HttpApi.Host/appsettings.Development.json index 7491b882c..a5e788276 100644 --- a/aspnet-core/services/LY.MicroService.BackendAdmin.HttpApi.Host/appsettings.Development.json +++ b/aspnet-core/services/LY.MicroService.BackendAdmin.HttpApi.Host/appsettings.Development.json @@ -14,7 +14,8 @@ "KnownProxies": [ "localhost" ] - } + }, + "CorsOrigins": "http://localhost:3100" }, "ConnectionStrings": { "Default": "Server=localhost;Database=Platform;User Id=root;Password=123456", diff --git a/aspnet-core/services/LY.MicroService.LocalizationManagement.HttpApi.Host/appsettings.Development.json b/aspnet-core/services/LY.MicroService.LocalizationManagement.HttpApi.Host/appsettings.Development.json index e0f8327fd..fbb5e23e9 100644 --- a/aspnet-core/services/LY.MicroService.LocalizationManagement.HttpApi.Host/appsettings.Development.json +++ b/aspnet-core/services/LY.MicroService.LocalizationManagement.HttpApi.Host/appsettings.Development.json @@ -8,7 +8,8 @@ "tag": "Localization" }, "App": { - "TrackingEntitiesChanged": true + "TrackingEntitiesChanged": true, + "CorsOrigins": "http://localhost:3100" }, "ConnectionStrings": { "Default": "Server=localhost;Database=Platform;User Id=root;Password=123456", diff --git a/aspnet-core/services/LY.MicroService.PlatformManagement.HttpApi.Host/appsettings.Development.json b/aspnet-core/services/LY.MicroService.PlatformManagement.HttpApi.Host/appsettings.Development.json index 090b239ce..c35347b98 100644 --- a/aspnet-core/services/LY.MicroService.PlatformManagement.HttpApi.Host/appsettings.Development.json +++ b/aspnet-core/services/LY.MicroService.PlatformManagement.HttpApi.Host/appsettings.Development.json @@ -13,7 +13,8 @@ "KnownProxies": [ "localhost" ] - } + }, + "CorsOrigins": "http://localhost:3100" }, "RemoteServices": { "AbpIdentity": { diff --git a/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/appsettings.Development.json b/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/appsettings.Development.json index ae590fb5f..d8cd5a3d3 100644 --- a/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/appsettings.Development.json +++ b/aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/appsettings.Development.json @@ -8,7 +8,7 @@ "tag": "Message" }, "App": { - "CorsOrigins": "http://localhost:9527,http://localhost:30000,http://localhost:3100", + "CorsOrigins": "http://localhost:3100", "Forwarded": { "ForwardedHeaders": 5, "KnownProxies": [ diff --git a/aspnet-core/services/LY.MicroService.identityServer.HttpApi.Host/appsettings.Development.json b/aspnet-core/services/LY.MicroService.identityServer.HttpApi.Host/appsettings.Development.json index 82973b8e2..b5e8b15e6 100644 --- a/aspnet-core/services/LY.MicroService.identityServer.HttpApi.Host/appsettings.Development.json +++ b/aspnet-core/services/LY.MicroService.identityServer.HttpApi.Host/appsettings.Development.json @@ -10,7 +10,7 @@ "App": { "TrackingEntitiesChanged": true, "SelfUrl": "http://localhost:30015/", - "CorsOrigins": "http://localhost:4200,http://localhost:9527,http://localhost:3100" + "CorsOrigins": "http://localhost:3100" }, "ConnectionStrings": { "Default": "Server=localhost;Database=IdentityServer;User Id=root;Password=123456", diff --git a/aspnet-core/services/LY.MicroService.identityServer/appsettings.Development.json b/aspnet-core/services/LY.MicroService.identityServer/appsettings.Development.json index 0f9f92842..50f6a344c 100644 --- a/aspnet-core/services/LY.MicroService.identityServer/appsettings.Development.json +++ b/aspnet-core/services/LY.MicroService.identityServer/appsettings.Development.json @@ -10,7 +10,7 @@ "App": { "TrackingEntitiesChanged": true, "SelfUrl": "http://localhost:44385/", - "CorsOrigins": "http://localhost:4200,http://localhost:9527,http://localhost:3100" + "CorsOrigins": "http://localhost:3100" }, "AppSelfUrl": "http://localhost:44385/", "ConnectionStrings": { diff --git a/gateways/internal/LINGYUN.MicroService.Internal.ApiGateway/src/LINGYUN.MicroService.Internal.ApiGateway/InternalApiGatewayModule.Configure.cs b/gateways/internal/LINGYUN.MicroService.Internal.ApiGateway/src/LINGYUN.MicroService.Internal.ApiGateway/InternalApiGatewayModule.Configure.cs index 4148d9578..3f9c7216d 100644 --- a/gateways/internal/LINGYUN.MicroService.Internal.ApiGateway/src/LINGYUN.MicroService.Internal.ApiGateway/InternalApiGatewayModule.Configure.cs +++ b/gateways/internal/LINGYUN.MicroService.Internal.ApiGateway/src/LINGYUN.MicroService.Internal.ApiGateway/InternalApiGatewayModule.Configure.cs @@ -3,6 +3,7 @@ using LINGYUN.MicroService.Internal.ApiGateway.Localization; using LINGYUN.MicroService.Internal.ApiGateway.Ocelot.Configuration.Repository; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Routing; @@ -17,6 +18,7 @@ using Ocelot.Multiplexer; using Ocelot.Provider.Polly; using StackExchange.Redis; using System; +using System.Linq; using System.Text.Encodings.Web; using System.Text.Unicode; using Volo.Abp.Caching; @@ -29,6 +31,8 @@ namespace LINGYUN.MicroService.Internal.ApiGateway { public partial class InternalApiGatewayModule { + protected const string DefaultCorsPolicyName = "Default"; + private void PreConfigureApp() { AbpSerilogEnrichersConsts.ApplicationName = "Internal-ApiGateWay"; @@ -178,6 +182,30 @@ namespace LINGYUN.MicroService.Internal.ApiGateway }); } + private void ConfigureCors(IServiceCollection services, IConfiguration configuration) + { + services.AddCors(options => + { + options.AddPolicy(DefaultCorsPolicyName, builder => + { + builder + .WithOrigins( + configuration["App:CorsOrigins"] + .Split(",", StringSplitOptions.RemoveEmptyEntries) + .Select(o => o.RemovePostFix("/")) + .ToArray() + ) + .WithAbpExposedHeaders() + // 引用 LINGYUN.Abp.AspNetCore.Mvc.Wrapper 包时可替换为 WithAbpWrapExposedHeaders + .WithExposedHeaders("_AbpWrapResult", "_AbpDontWrapResult") + .SetIsOriginAllowedToAllowWildcardSubdomains() + .AllowAnyHeader() + .AllowAnyMethod() + .AllowCredentials(); + }); + }); + } + private void ConfigureSecurity(IServiceCollection services, IConfiguration configuration, bool isDevelopment = false) { services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) diff --git a/gateways/internal/LINGYUN.MicroService.Internal.ApiGateway/src/LINGYUN.MicroService.Internal.ApiGateway/InternalApiGatewayModule.cs b/gateways/internal/LINGYUN.MicroService.Internal.ApiGateway/src/LINGYUN.MicroService.Internal.ApiGateway/InternalApiGatewayModule.cs index 42bd70aa6..31cc54a87 100644 --- a/gateways/internal/LINGYUN.MicroService.Internal.ApiGateway/src/LINGYUN.MicroService.Internal.ApiGateway/InternalApiGatewayModule.cs +++ b/gateways/internal/LINGYUN.MicroService.Internal.ApiGateway/src/LINGYUN.MicroService.Internal.ApiGateway/InternalApiGatewayModule.cs @@ -47,6 +47,7 @@ namespace LINGYUN.MicroService.Internal.ApiGateway ConfigureMvc(context.Services); ConfigureSwagger(context.Services); + ConfigureCors(context.Services, configuration); ConfigureOcelot(context.Services, configuration); } @@ -56,6 +57,7 @@ namespace LINGYUN.MicroService.Internal.ApiGateway app.UseAuditing(); app.UseStaticFiles(); app.UseRouting(); + app.UseCors(DefaultCorsPolicyName); app.UseAuthentication(); app.UseAbpClaimsMap(); app.MapWhen( diff --git a/gateways/internal/LINGYUN.MicroService.Internal.ApiGateway/src/LINGYUN.MicroService.Internal.ApiGateway/appsettings.Development.json b/gateways/internal/LINGYUN.MicroService.Internal.ApiGateway/src/LINGYUN.MicroService.Internal.ApiGateway/appsettings.Development.json index 836e80b3f..881e00207 100644 --- a/gateways/internal/LINGYUN.MicroService.Internal.ApiGateway/src/LINGYUN.MicroService.Internal.ApiGateway/appsettings.Development.json +++ b/gateways/internal/LINGYUN.MicroService.Internal.ApiGateway/src/LINGYUN.MicroService.Internal.ApiGateway/appsettings.Development.json @@ -7,6 +7,9 @@ "name": "ApiGateway", "tag": "ApiGateway" }, + "App": { + "CorsOrigins": "http://localhost:3100" + }, "ApiGateway": { "AppId": "TEST-APP", "DownstreamOpenApis": [