如何删除授权'特定的Swagger Doc的按钮?
我像这样配置了我的招摇:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
安全定义将在文档级别添加,因此您必须修改文档。
在这里,我写了一个文档过滤器,该过滤器仅删除该文档的架构。
希望它有帮助
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.
hope it is helpful