如何删除授权'特定的Swagger Doc的按钮?

发布于 2025-02-08 04:16:05 字数 3076 浏览 2 评论 0原文

我像这样配置了我的招摇:

services.AddSwaggerGen(
    options =>
    {
        options.SwaggerDoc(
            IntegrationApiVersion, 
            new OpenApiInfo { Title = IntegrationApiName, Version = IntegrationApiVersion });
        options.SwaggerDoc(
            ApplicationApiVersion, 
            new OpenApiInfo { Title = ApplicationApiName, Version = ApplicationApiVersion });

        options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
        {
            Description = "Bearer Token: e.g. \"Bearer <your token here>\"",
            Name = "Authorization",
            In = ParameterLocation.Header,
            Type = SecuritySchemeType.ApiKey,
            Scheme = "Bearer",
            Reference = new OpenApiReference
            {
                Id = "Bearer",
                Type = ReferenceType.SecurityScheme
            }
        });

        options.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
        options.EnableAnnotations();
        options.SchemaFilter<SmartEnumSchemaFilter>();

        options.SupportNonNullableReferenceTypes();
        options.UseAllOfToExtendReferenceSchemas();
        options.IncludeXmlComments(
            Path.Combine(AppContext.BaseDirectory, $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"),
            includeControllerXmlComments: true);

        options.OperationFilter<TestOperationFilter>();
    })
.AddFluentValidationRulesToSwagger();

但是,由于我的文档中只有1个需要身份验证IntegrationApiversion,所以我想隐藏另一个文档的“授权”按钮。我发现呼叫addSecutityDefinition是添加按钮的方法,但是它不能让我定义API名称/版本,而且我无法弄清楚如何从

管理的 过滤器中设置该定义为了显示小锁显示,JWT auth通过添加此过滤器来表现出色:

public class TestOperationFilter : IOperationFilter
{
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
        if (context.DocumentName == SwaggerConfiguration.IntegrationApiVersion)
        {
            operation.Security = new List<OpenApiSecurityRequirement>
            {
                new OpenApiSecurityRequirement
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id = "Bearer"
                            },
                            Scheme = "oauth2",
                            Name = "Bearer",
                            In = ParameterLocation.Header
                        },
                        Array.Empty<string>()
                    }
                }
            }; 
        }
    }
}

但是我无法弄清楚如何为“授权”按钮本身应用相同的解决方案,以便它出现在我的IntegrationApiversion页面上,

我能找到的是人们在做称为nonbodyParameter以在过滤器中添加SecurityDefinition,但似乎我不再可用。有人有建议吗?

使用swashbuckle 6.3.0

I configure my swagger like this:

services.AddSwaggerGen(
    options =>
    {
        options.SwaggerDoc(
            IntegrationApiVersion, 
            new OpenApiInfo { Title = IntegrationApiName, Version = IntegrationApiVersion });
        options.SwaggerDoc(
            ApplicationApiVersion, 
            new OpenApiInfo { Title = ApplicationApiName, Version = ApplicationApiVersion });

        options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
        {
            Description = "Bearer Token: e.g. \"Bearer <your token here>\"",
            Name = "Authorization",
            In = ParameterLocation.Header,
            Type = SecuritySchemeType.ApiKey,
            Scheme = "Bearer",
            Reference = new OpenApiReference
            {
                Id = "Bearer",
                Type = ReferenceType.SecurityScheme
            }
        });

        options.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
        options.EnableAnnotations();
        options.SchemaFilter<SmartEnumSchemaFilter>();

        options.SupportNonNullableReferenceTypes();
        options.UseAllOfToExtendReferenceSchemas();
        options.IncludeXmlComments(
            Path.Combine(AppContext.BaseDirectory, 
quot;{Assembly.GetExecutingAssembly().GetName().Name}.xml"),
            includeControllerXmlComments: true);

        options.OperationFilter<TestOperationFilter>();
    })
.AddFluentValidationRulesToSwagger();

But since only 1 of my documents requires authentication IntegrationApiVersion, I want to hide the 'Authorize' button for the other doc. I found that the call to AddSecutityDefinition is what adds the button, but it does not let me define an api name/version, and I can't figure out how to set that definition from a filter

I managed to get the little locks to display, and JWT auth works great by adding this filter:

public class TestOperationFilter : IOperationFilter
{
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
        if (context.DocumentName == SwaggerConfiguration.IntegrationApiVersion)
        {
            operation.Security = new List<OpenApiSecurityRequirement>
            {
                new OpenApiSecurityRequirement
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id = "Bearer"
                            },
                            Scheme = "oauth2",
                            Name = "Bearer",
                            In = ParameterLocation.Header
                        },
                        Array.Empty<string>()
                    }
                }
            }; 
        }
    }
}

But I can't figure out how to apply the same solution for the 'Authorize' button itself, so that it only appears on my IntegrationApiVersion page

The closes thing I could find was people doing this, but using something called NonBodyParameter to add a SecurityDefinition inside a filter, but it seems that type is no longer available to me. Does anyone have any advice?

Using SwashBuckle 6.3.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可爱暴击 2025-02-15 04:16:05

安全定义将在文档级别添加,因此您必须修改文档。

在这里,我写了一个文档过滤器,该过滤器仅删除该文档的架构。

public class SwaggerDocumentFilter : IDocumentFilter
{
    public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
    {
        if(context.DocumentName == SwaggerConfiguration.ApplicationApiVersion)
        {
            swaggerDoc.Components.SecuritySchemes.Remove("Bearer");
        }
    }
}

希望它有帮助

Security definition gets add at the document level so you have to modify the document.

here I have written a document filter which removes the schema only for this document.

public class SwaggerDocumentFilter : IDocumentFilter
{
    public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
    {
        if(context.DocumentName == SwaggerConfiguration.ApplicationApiVersion)
        {
            swaggerDoc.Components.SecuritySchemes.Remove("Bearer");
        }
    }
}

hope it is helpful

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文