【发布时间】:2019-07-29 12:06:42
【问题描述】:
我在多个微服务上大摇大摆地工作。 部署到 Azure 时,由于安全最佳实践,我们需要一起删除 swagger 选项。 使用 .net 核心 2.1 寻找定义的例子。
【问题讨论】:
标签: swagger asp.net-core-2.0 swagger-ui
我在多个微服务上大摇大摆地工作。 部署到 Azure 时,由于安全最佳实践,我们需要一起删除 swagger 选项。 使用 .net 核心 2.1 寻找定义的例子。
【问题讨论】:
标签: swagger asp.net-core-2.0 swagger-ui
首先,什么是“安全最佳实践”?将 API 文档用于生产并没有错。这实际上是重点:客户应该能够查看文档,以便他们可以正确使用您的 API。如果这些微服务不被外部客户端使用,那么问题就更小了,因为无论如何,外面的人都无法访问服务或文档。如果服务是公开的,那么它们也应该要求对请求进行授权,并且可以通过相同的机制锁定文档本身。
无论如何,如果您坚持在生产环境中删除它,最好的选择是一开始就不要添加它。换句话说,将所有 Swagger 设置包装在 Startup.cs 和 if (env.IsDevelopment()) 中,或者如果您希望它在暂存环境之类的东西中可用:if (!env.IsProduction())。
【讨论】:
documentation 和service。
如果您是从 .net core 3.1 开始的:
假设 Startup 类的构造函数将注入的 IConfiguration 复制到名为 configuration 的本地字段,那么您可以像这样设置 Configure 方法:
public void configure(IApplicationBuilder app, IWebHostEnvironment env)
{
var applicationName = configuration.GetValue<string>("ApplicationName") ?? "MyApi";
var basePath = configuration.GetValue<string>("BasePath");
if (!string.IsNullOrEmpty(basePath))
app.UsePathBase(basePath);
if (!env.IsProduction())
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint($"{basePath}/swagger/v1/swagger.json",
$"{applicationName} {ReflectionUtils.GetAssemblyVersion<Program>()}");
});
}
}
【讨论】:
第一个选项,在配置方法中禁用它:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// read from config
var config = (IConfiguration)app.ApplicationServices.GetService(typeof(IConfiguration));
enableSwagger = bool.Parse(config.GetValue<string>("EnableSwagger") ?? "false");
if (enableSwagger /*|| env.IsDevelopment()*/)
{
app.UseSwagger(o =>
{
o.RouteTemplate = "swagger/{documentName}/swagger.json";
});
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
}
}
或使用:
if (env.IsDevelopment())
{
...
}
第二种选择是完全删除代码文件中的所有引用和使用
#if USE_SWAGGER
using Microsoft.OpenApi.Models;
...
#endif
#if USE_SWAGGER
<some method>
...
#endif
添加.csproj:
<Choose>
<When Condition="$(DefineConstants.Contains('USE_SWAGGER'))">
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard3.1'">
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="5.6.3" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="5.6.3" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.6.3" />
</ItemGroup>
</When>
<Otherwise />
</Choose>
或
<Choose>
<When Condition="'$(Configuration)' == 'Debug'">
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard3.1'">
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="5.6.3" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="5.6.3" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.6.3" />
</ItemGroup>
</When>
<Otherwise />
</Choose>
玩一玩,你就会变得深刻!
【讨论】:
您也可以在您不想在文档中显示的控制器类中使用[ApiExplorerSettings(IgnoreApi = true)]。这是一个排除操作,意味着将显示每个类,除了标有此属性的类。 (当您只想让第三方消费者看到您的一组 API 文档时,这很好);
【讨论】: