From 747c61472a24ad0eac3a74be719aed31325d02d6 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Wed, 19 Jun 2024 10:33:20 +0800 Subject: [PATCH] Remove Tenant placeholders when configure Swagger Oidc --- ...AbpSwaggerGenServiceCollectionExtensions.cs | 18 +++++++++++++----- .../wwwroot/swagger/ui/abp.swagger.js | 14 ++++++++++---- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerGenServiceCollectionExtensions.cs b/framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerGenServiceCollectionExtensions.cs index 0acdf852e5..1238d766be 100644 --- a/framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerGenServiceCollectionExtensions.cs +++ b/framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerGenServiceCollectionExtensions.cs @@ -5,6 +5,7 @@ using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.SwaggerGen; using Swashbuckle.AspNetCore.SwaggerUI; using Volo.Abp.Content; +using Volo.Abp.MultiTenancy; using Volo.Abp.Swashbuckle; namespace Microsoft.Extensions.DependencyInjection; @@ -89,16 +90,15 @@ public static class AbpSwaggerGenServiceCollectionExtensions Action? setupAction = null) { var discoveryUrl = discoveryEndpoint != null ? - new Uri($"{discoveryEndpoint.TrimEnd('/')}/.well-known/openid-configuration") : - new Uri($"{authority.TrimEnd('/')}/.well-known/openid-configuration"); - + $"{discoveryEndpoint.TrimEnd('/')}/.well-known/openid-configuration": + $"{authority.TrimEnd('/')}/.well-known/openid-configuration"; flows ??= new [] { AbpSwaggerOidcFlows.AuthorizationCode }; services.Configure(swaggerUiOptions => { swaggerUiOptions.ConfigObject.AdditionalItems["oidcSupportedFlows"] = flows; swaggerUiOptions.ConfigObject.AdditionalItems["oidcSupportedScopes"] = scopes; - swaggerUiOptions.ConfigObject.AdditionalItems["oidcDiscoveryEndpoint"] = discoveryEndpoint; + swaggerUiOptions.ConfigObject.AdditionalItems["oidcDiscoveryEndpoint"] = discoveryUrl; }); return services @@ -109,7 +109,7 @@ public static class AbpSwaggerGenServiceCollectionExtensions options.AddSecurityDefinition("oidc", new OpenApiSecurityScheme { Type = SecuritySchemeType.OpenIdConnect, - OpenIdConnectUrl = discoveryUrl + OpenIdConnectUrl = new Uri(RemoveTenantPlaceholders(discoveryUrl)) }); options.AddSecurityRequirement(new OpenApiSecurityRequirement @@ -129,4 +129,12 @@ public static class AbpSwaggerGenServiceCollectionExtensions setupAction?.Invoke(options); }); } + + private static string RemoveTenantPlaceholders(string url) + { + return url + .Replace(MultiTenantUrlProvider.TenantPlaceHolder + ".", string.Empty) + .Replace(MultiTenantUrlProvider.TenantIdPlaceHolder + ".", string.Empty) + .Replace(MultiTenantUrlProvider.TenantNamePlaceHolder + ".", string.Empty); + } } diff --git a/framework/src/Volo.Abp.Swashbuckle/wwwroot/swagger/ui/abp.swagger.js b/framework/src/Volo.Abp.Swashbuckle/wwwroot/swagger/ui/abp.swagger.js index 07deea114b..003105a81a 100644 --- a/framework/src/Volo.Abp.Swashbuckle/wwwroot/swagger/ui/abp.swagger.js +++ b/framework/src/Volo.Abp.Swashbuckle/wwwroot/swagger/ui/abp.swagger.js @@ -90,13 +90,19 @@ var abp = abp || {}; await getAbpApplicationConfiguration(); } - url.replace(tenantPlaceHolders[0], abp.currentTenant.id); - url.replace(tenantPlaceHolders[1], abp.currentTenant.name); + if(abp.currentTenant.id == null && abp.currentTenant.name == null){ + return url + .replace(tenantPlaceHolders[0] + ".", "") + .replace(tenantPlaceHolders[1] + ".", "") + .replace(tenantPlaceHolders[2] + ".", ""); + } + + url = url.replace(tenantPlaceHolders[0], abp.currentTenant.id).url.replace(tenantPlaceHolders[1], abp.currentTenant.name); if(abp.currentTenant.name != null){ - url.replace(tenantPlaceHolders[2], abp.currentTenant.name); + url = url.replace(tenantPlaceHolders[2], abp.currentTenant.name); }else if (abp.currentTenant.id != null){ - url.replace(tenantPlaceHolders[2], abp.currentTenant.id); + url = url.replace(tenantPlaceHolders[2], abp.currentTenant.id); } return url;