c# - 如何保护 .NET Core API 中的 swagger 端点?
问题描述
我在 .net core 2.1 中有一个 api。为了限制对各种端点的访问,我使用了 IdentityServer4 和 [Authorize] 属性。但是,我在开发过程中的目标是向我们的开发人员公开 api swagger 文档,以便他们无论在哪里工作都可以使用它。我面临的挑战是如何保护 swagger index.html 文件,以便只有他们才能看到 api 的详细信息。
我在 wwwroot/swagger/ui 文件夹中创建了一个自定义 index.html 文件,并且一切正常,但是,该文件使用来自/swagger/v1/swagger.json
不受保护的端点的数据。我想知道如何覆盖该特定端点的返回值,以便我可以添加自己的身份验证?
编辑:
目前,我已经通过以下中间件实现了上述目标:
public class SwaggerInterceptor
{
private readonly RequestDelegate _next;
public SwaggerInterceptor(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
var uri = context.Request.Path.ToString();
if (uri.StartsWith("/swagger/ui/index.html"))
{
var param = context.Request.QueryString.Value;
if (!param.Equals("?key=123"))
{
context.Response.StatusCode = 404;
context.Response.ContentType = "application/json";
await context.Response.WriteAsync("{\"result:\" \"Not Found\"}", Encoding.UTF8);
return;
}
}
await _next.Invoke(context);
}
}
public class Startup
{
//omitted code
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseMiddleware<SwaggerInterceptor>();
//omitted code
}
}
我不喜欢这种方法,因为它会检查每一个请求。有没有更好的方法来实现这一目标?以上仅保护 index.html 文件,但我可以对其进行调整以以类似方式保护 json 端点。
解决方案
您可以选择一些选项:
- 基本授权
- 使用身份服务器的 OpenId Connect 授权
基本授权
在这种情况下,您只需关闭 swagger 端点。
// Startup.cs
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
...
services.AddAuthentication()
.AddScheme<BasicAuthenticationOptions, BasicAuthenticationHandler>("Basic", _ => {});
...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
app.UseEndpoints(endpoints =>
{
...
var pipeline = endpoints.CreateApplicationBuilder().Build();
var basicAuthAttr = new AuthorizeAttribute { AuthenticationSchemes = "Basic" };
endpoints
.Map("/swagger/{documentName}/swagger.json", pipeline)
.RequireAuthorization(basicAuthAttr);
endpoints
.Map("/swagger/index.html", pipeline)
.RequireAuthorization(basicAuthAttr);
});
}
}
// BasicAuthenticationHandler.cs
public class BasicAuthenticationHandler : AuthenticationHandler<BasicAuthenticationOptions>
{
...
}
使用 IdentityServer4 进行 OIDC 授权
我为这个案例写了这篇文章:https ://medium.com/dev-genius/csharp-protecting-swagger-endpoints-82ae5cfc7eb1
推荐阅读
- android - 水平滚动时,GraphView 日期标签跳动
- json - 'curl: option -----END: is unknown' 使用 curl 将公钥上传到 jenkins SSH 凭证
- python - ValueError(在 python 中,使用 JSON 作为 dictConfig API 的输入)
- sql - 如何编写查询来控制基于链接表的数据表子表单的组合框?
- c - 通过动态内存分配计算字符串中的字符
- jquery - AJAX 客户端验证未正确调用
- javascript - 如何调试 vscode 扩展的 WebView javascript
- android - 具有多个视图的 RecyclerView 中的 Exoplayer
- javascript - d3.js 泛限制行为
- python - 此处的列表方法不起作用。它会抛出: TypeError: 'list' object is not callable