abp icon indicating copy to clipboard operation
abp copied to clipboard

Can we hide ABP related HTTP API endpoints if desired?

Open hikalkan opened this issue 6 years ago • 15 comments

Even for an empty project, we have the following pre-defined HTTP endpoints:

image

We may want to disable these endpoints (not only hide from swagger, but completely disable them). So, we need to provide an option for it.

hikalkan avatar Apr 27 '20 13:04 hikalkan

I am hiding the abp endpoints on Swagger UI by adding a [ShowInSwagger] attribute to the controllers I want to see, then using this in my host module:

public class ShowInSwaggerAttributeFilter : IDocumentFilter
{
    public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
    {
        var filteredApis = context.ApiDescriptions.Where(a => a.CustomAttributes().Any(x => 
            x.GetType() == typeof(ShowInSwaggerAttribute)));

        foreach (var path in swaggerDoc.Paths.ToList())
        {
            if (filteredApis.All(x => ("/" + x.RelativePath) != path.Key))
                swaggerDoc.Paths.Remove(path.Key);
        }
    }
}

olicooper avatar May 18 '20 10:05 olicooper

How do I prevent the execution of certain methods and tell ABP to ignore them when creating endpoints (by convention)?

tinonetic avatar Jun 04 '20 11:06 tinonetic

it is greate enhancement, recently I am looking for it .

I don’t want to expose some pre-built apis

xyfy avatar Jun 22 '20 02:06 xyfy

Is there any update on this?

ozdogans avatar Jun 07 '21 13:06 ozdogans

I am hiding the abp endpoints on Swagger UI by adding a [ShowInSwagger] attribute to the controllers I want to see, then using this in my host module:

public class ShowInSwaggerAttributeFilter : IDocumentFilter
{
    public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
    {
        var filteredApis = context.ApiDescriptions.Where(a => a.CustomAttributes().Any(x => 
            x.GetType() == typeof(ShowInSwaggerAttribute)));

        foreach (var path in swaggerDoc.Paths.ToList())
        {
            if (filteredApis.All(x => ("/" + x.RelativePath) != path.Key))
                swaggerDoc.Paths.Remove(path.Key);
        }
    }
}

How do I make this work? I added this class to my host module, but swagger doesnt run it (I tried setting a breakpoint)

ArneCools avatar Jun 09 '21 10:06 ArneCools

@hikalkan @ArneCools ShowInSwaggerAttributeFilter must be added in .HttpApi.Host project configuration:

private static void ConfigureSwaggerServices(ServiceConfigurationContext context, IConfiguration configuration)
{
    context.Services.AddAbpSwaggerGenWithOAuth(
        configuration["AuthServer:Authority"],
        new Dictionary<string, string>
        {
            {"...", "... API"}
        },
        options =>
        {
            options.DocumentFilter<ShowInSwaggerAttributeFilter>(); // <- ADD THIS!
            options.SwaggerDoc("v1", new OpenApiInfo {Title = "... API", Version = "v1"});
            options.DocInclusionPredicate((docName, description) => true);
            options.CustomSchemaIds(type => type.FullName);
        });
}

yellow-dragon-cloud avatar Aug 16 '21 13:08 yellow-dragon-cloud

@hikalkan any news?

1d0n7kn0w avatar Sep 23 '21 14:09 1d0n7kn0w

private static void ConfigureSwaggerServices(ServiceConfigurationContext context, IConfiguration configuration)
{
   context.Services.AddAbpSwaggerGenWithOAuth(
   configuration["AuthServer:Authority"],
   new Dictionary<string, string>
   {
      {"...", "... API"}
   },
   options =>
   {
      options.SwaggerDoc("v1", new OpenApiInfo {Title = "... API", Version = "v1"});
      //options.DocInclusionPredicate((docName, description) => true); // <- Comment out this line of code
      options.CustomSchemaIds(type => type.FullName);
   });
}  

try it out

gaoconggit avatar Nov 01 '21 03:11 gaoconggit

The following example hides the API in Volo.Abp.Account.HttpApi

public override void OnPreApplicationInitialization(ApplicationInitializationContext context)
{
    var applicationPartManager = context.ServiceProvider.GetRequiredService<ApplicationPartManager>();

    applicationPartManager.ApplicationParts.RemoveAll(s => s.Name == "Volo.Abp.Account.HttpApi");
}

For more information, please visit ASP.NET Core Docs

cdlian avatar Nov 01 '21 10:11 cdlian

According to @cdlian answer this code snippet:

   public override void OnPreApplicationInitialization(ApplicationInitializationContext context)
    {
        var applicationPartManager = context.ServiceProvider.GetRequiredService<ApplicationPartManager>();
        applicationPartManager.ApplicationParts.RemoveAll(s => s.Name == "Volo.Abp.Account.HttpApi");
        applicationPartManager.ApplicationParts.RemoveAll(s => s.Name == "Volo.Abp.FeatureManagement.HttpApi");
        applicationPartManager.ApplicationParts.RemoveAll(s => s.Name == "Volo.Abp.Identity.HttpApi");
        applicationPartManager.ApplicationParts.RemoveAll(s => s.Name == "Volo.Abp.PermissionManagement.HttpApi");
        applicationPartManager.ApplicationParts.RemoveAll(s => s.Name == "Volo.Abp.SettingsManagement.HttpApi");
    }

hides everything except:

  • AbpApiDefinition
  • AbpApplicationConfiguration
  • AbpTenant
  • EmailSettings

Still confused about this.

AlexanderSysoev avatar Jan 19 '22 16:01 AlexanderSysoev

Is there any update on this?

zjsjmvn avatar Feb 16 '22 12:02 zjsjmvn

@AlexanderSysoev

By overriding the ApplyForControllers method of AbpServiceConvention, you can remove controllers you don't need.

[Dependency(ReplaceServices = true)]
[ExposeServices(typeof(IAbpServiceConvention))]
public class MyServiceConvention : AbpServiceConvention
{
    public MyServiceConvention(IOptions<AbpAspNetCoreMvcOptions> options, IConventionalRouteBuilder conventionalRouteBuilder) : base(options, conventionalRouteBuilder)
    {
    }

    protected override void ApplyForControllers(ApplicationModel application)
    {
        application.Controllers.RemoveAll(s => s.ControllerType == typeof(AbpApplicationConfigurationController));

        base.ApplyForControllers(application);
    }
}

cdlian avatar Feb 17 '22 04:02 cdlian

With v5.2 there is a built-in option to hide ABP-related endpoints on Swagger UI.

Call the HideAbpEndpoints method in your Swagger configuration as follows:

services.AddAbpSwaggerGen(
    options => 
    {
        //... other options
        
        //Hides ABP Related endpoints on Swagger UI
        options.HideAbpEndpoints();
    }
)

After that, ABP's default endpoints will still be functional, but will be hidden on Swagger UI. https://docs.abp.io/en/abp/5.2/API/Swagger-Integration#hide-abp-endpoints-on-swagger-ui

EngincanV avatar Mar 09 '22 15:03 EngincanV

private static void ConfigureSwaggerServices(ServiceConfigurationContext context, IConfiguration configuration)
{
   context.Services.AddAbpSwaggerGenWithOAuth(
   configuration["AuthServer:Authority"],
   new Dictionary<string, string>
   {
      {"...", "... API"}
   },
   options =>
   {
      options.SwaggerDoc("v1", new OpenApiInfo {Title = "... API", Version = "v1"});
      //options.DocInclusionPredicate((docName, description) => true); // <- Comment out this line of code
      options.CustomSchemaIds(type => type.FullName);
   });
}  

try it out

this is very helpful

truongkyit avatar Nov 23 '22 08:11 truongkyit

It is possible to force authentication on EmailSettings API

MathiasKowoll avatar Dec 03 '22 15:12 MathiasKowoll