新增SwaggerBasicAuthMiddleware中间件,访问api接口的时候可以要求输入用户名和密码

This commit is contained in:
FalconFly 2024-04-17 10:43:49 +08:00
parent fd89050bbb
commit e52930e99a
3 changed files with 142 additions and 0 deletions

View File

@ -0,0 +1,19 @@
using Microsoft.AspNetCore.Builder;
namespace Falcon.SugarApi.Swagger
{
/// <summary>
/// 应用扩展
/// </summary>
public static class ApplicationBuilderExtend
{
/// <summary>
/// 使用基础认证
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
public static IApplicationBuilder UseSwaggerBasicAuth(this IApplicationBuilder builder) {
return builder.UseMiddleware<SwaggerBasicAuthMiddleware>();
}
}
}

View File

@ -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
{
/// <summary>
/// Swagger接口基础认证
/// </summary>
public class SwaggerBasicAuthMiddleware
{
/// <summary>
/// 下一步请求
/// </summary>
private readonly RequestDelegate Next;
/// <summary>
/// 配置
/// </summary>
public SwaggerOptions Options { get; private set; }
/// <summary>
/// 构造中间件
/// </summary>
/// <param name="next">下一步</param>
/// <param name="options">配置</param>
public SwaggerBasicAuthMiddleware(RequestDelegate next,SwaggerOptions options) {
Next = next;
this.Options = options;
}
/// <summary>
/// 执行中间件
/// </summary>
/// <param name="context">HttpContext</param>
/// <returns>任务</returns>
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);
}
/// <summary>
/// 进入下一个中间件
/// </summary>
public virtual async Task ToNext(HttpContext context) {
await Next.Invoke(context).ConfigureAwait(false);
return;
}
/// <summary>
/// 需要认证
/// </summary>
public virtual void needAuth(HttpContext context) {
context.Response.Headers["WWW-Authenticate"] = "Basic";
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
}
}
}

View File

@ -0,0 +1,34 @@
namespace Falcon.SugarApi.Swagger
{
/// <summary>
/// Swagger配置选项
/// </summary>
public class SwaggerOptions
{
/// <summary>
/// 是否启用Swagger
/// </summary>
public bool Enable { get; set; } = true;
/// <summary>
/// swagger路由前缀
/// </summary>
public string Prefix { get; set; } = "";
/// <summary>
/// 接口网页标题
/// </summary>
public string DocumentTitle { get; set; } = "ApiService Api接口";
/// <summary>
/// 是否启用认证
/// </summary>
public bool UseAuth { get; set; } = false;
/// <summary>
/// 认证用户名
/// </summary>
public string? AuthName { get; set; } = "";
/// <summary>
/// 认证用户密码
/// </summary>
public string? AuthPassword { get; set; } = "";
}
}