From e52930e99ac0ba59dd44a320ffb48a0fc6f53ad5 Mon Sep 17 00:00:00 2001 From: FalconFly <12919280+falconfly@user.noreply.gitee.com> Date: Wed, 17 Apr 2024 10:43:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9ESwaggerBasicAuthMiddleware?= =?UTF-8?q?=E4=B8=AD=E9=97=B4=E4=BB=B6=EF=BC=8C=E8=AE=BF=E9=97=AEapi?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E7=9A=84=E6=97=B6=E5=80=99=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E8=A6=81=E6=B1=82=E8=BE=93=E5=85=A5=E7=94=A8=E6=88=B7=E5=90=8D?= =?UTF-8?q?=E5=92=8C=E5=AF=86=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Swagger/ApplicationBuilderExtend.cs | 19 ++++ .../Swagger/SwaggerBasicAuthMiddleware.cs | 89 +++++++++++++++++++ Falcon.SugarApi/Swagger/SwaggerOptions.cs | 34 +++++++ 3 files changed, 142 insertions(+) create mode 100644 Falcon.SugarApi/Swagger/ApplicationBuilderExtend.cs create mode 100644 Falcon.SugarApi/Swagger/SwaggerBasicAuthMiddleware.cs create mode 100644 Falcon.SugarApi/Swagger/SwaggerOptions.cs diff --git a/Falcon.SugarApi/Swagger/ApplicationBuilderExtend.cs b/Falcon.SugarApi/Swagger/ApplicationBuilderExtend.cs new file mode 100644 index 0000000..bfa0184 --- /dev/null +++ b/Falcon.SugarApi/Swagger/ApplicationBuilderExtend.cs @@ -0,0 +1,19 @@ +using Microsoft.AspNetCore.Builder; + +namespace Falcon.SugarApi.Swagger +{ + /// + /// 应用扩展 + /// + public static class ApplicationBuilderExtend + { + /// + /// 使用基础认证 + /// + /// + /// + public static IApplicationBuilder UseSwaggerBasicAuth(this IApplicationBuilder builder) { + return builder.UseMiddleware(); + } + } +} diff --git a/Falcon.SugarApi/Swagger/SwaggerBasicAuthMiddleware.cs b/Falcon.SugarApi/Swagger/SwaggerBasicAuthMiddleware.cs new file mode 100644 index 0000000..3cd0129 --- /dev/null +++ b/Falcon.SugarApi/Swagger/SwaggerBasicAuthMiddleware.cs @@ -0,0 +1,89 @@ +using Microsoft.AspNetCore.Http; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http.Headers; +using System.Text; +using System.Threading.Tasks; + +namespace Falcon.SugarApi.Swagger +{ + /// + /// Swagger接口基础认证 + /// + public class SwaggerBasicAuthMiddleware + { + /// + /// 下一步请求 + /// + private readonly RequestDelegate Next; + /// + /// 配置 + /// + public SwaggerOptions Options { get; private set; } + + /// + /// 构造中间件 + /// + /// 下一步 + /// 配置 + public SwaggerBasicAuthMiddleware(RequestDelegate next,SwaggerOptions options) { + Next = next; + this.Options = options; + } + + /// + /// 执行中间件 + /// + /// HttpContext + /// 任务 + public virtual async Task InvokeAsync(HttpContext context) { + if(!this.Options.UseAuth) { + await ToNext(context); + return; + } + var pf = this.Options.Prefix.StartsWith("/") ? this.Options.Prefix : "/" + this.Options.Prefix; + if(!context.Request.Path.StartsWithSegments(pf)) { + await ToNext(context); + return; + } + string authHeader = context.Request.Headers["Authorization"]; + if(authHeader == null || !authHeader.StartsWith("Basic ")) { + needAuth(context); + return; + } + var header = AuthenticationHeaderValue.Parse(authHeader); + if(header == null || header.Parameter == null) { + needAuth(context); + return; + } + var inBs = Convert.FromBase64String(header.Parameter); + var cred = Encoding.UTF8.GetString(inBs).Split(':'); + var (un, pw) = (cred[0], cred[1]); + //var un = cred[0]; + //var pw = cred[1]; + if(un == this.Options.AuthName && pw == this.Options.AuthPassword) { + await ToNext(context); + return; + } + needAuth(context); + } + + /// + /// 进入下一个中间件 + /// + public virtual async Task ToNext(HttpContext context) { + await Next.Invoke(context).ConfigureAwait(false); + return; + } + /// + /// 需要认证 + /// + public virtual void needAuth(HttpContext context) { + context.Response.Headers["WWW-Authenticate"] = "Basic"; + context.Response.StatusCode = (int)HttpStatusCode.Unauthorized; + } + + } +} diff --git a/Falcon.SugarApi/Swagger/SwaggerOptions.cs b/Falcon.SugarApi/Swagger/SwaggerOptions.cs new file mode 100644 index 0000000..4eb1e85 --- /dev/null +++ b/Falcon.SugarApi/Swagger/SwaggerOptions.cs @@ -0,0 +1,34 @@ +namespace Falcon.SugarApi.Swagger +{ + /// + /// Swagger配置选项 + /// + public class SwaggerOptions + { + /// + /// 是否启用Swagger + /// + public bool Enable { get; set; } = true; + /// + /// swagger路由前缀 + /// + public string Prefix { get; set; } = ""; + /// + /// 接口网页标题 + /// + public string DocumentTitle { get; set; } = "ApiService Api接口"; + + /// + /// 是否启用认证 + /// + public bool UseAuth { get; set; } = false; + /// + /// 认证用户名 + /// + public string? AuthName { get; set; } = ""; + /// + /// 认证用户密码 + /// + public string? AuthPassword { get; set; } = ""; + } +}