Swagger基础认真限定范围测试

This commit is contained in:
FalconFly 2024-04-19 14:00:38 +08:00
parent d27d4bec62
commit 3d338ea7cc

View File

@ -1,7 +1,6 @@
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Net; using System.Net;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Text; using System.Text;
@ -11,6 +10,7 @@ namespace Falcon.SugarApi.Swagger
{ {
/// <summary> /// <summary>
/// Swagger接口基础认证 /// Swagger接口基础认证
/// <para>安全认证只对swagger接口页面进行保护</para>
/// </summary> /// </summary>
public class SwaggerBasicAuthMiddleware public class SwaggerBasicAuthMiddleware
{ {
@ -47,12 +47,15 @@ namespace Falcon.SugarApi.Swagger
await ToNext(context); await ToNext(context);
return; return;
} }
var protectPaths = new List<string> { };
var pf = this.Options.Prefix.StartsWith("/") ? this.Options.Prefix : "/" + this.Options.Prefix; var pf = this.Options.Prefix.StartsWith("/") ? this.Options.Prefix : "/" + this.Options.Prefix;
if(!context.Request.Path.StartsWithSegments(pf)) { protectPaths.Add(pf);
protectPaths.Add(pf + "/index.html");
if(!protectPaths.Contains(context.Request.Path)) {
await ToNext(context); await ToNext(context);
return; return;
} }
string authHeader = context.Request.Headers["Authorization"]; string? authHeader = context.Request.Headers["Authorization"];
if(authHeader == null || !authHeader.StartsWith("Basic ")) { if(authHeader == null || !authHeader.StartsWith("Basic ")) {
needAuth(context); needAuth(context);
return; return;