From 8467804705615f7614c29e7ef7ac3e99f5c13a54 Mon Sep 17 00:00:00 2001 From: dengjunjie <dengjunjie@hnkhzn.com> Date: 星期二, 11 二月 2025 13:53:05 +0800 Subject: [PATCH] 优化WMS接口日志记录,添加接口名称和接口描述 --- 项目代码/WMS/WIDESEA_WMSServer/WIDESEA_Core/Middlewares/SwaggerAuthMiddleware.cs | 80 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 80 insertions(+), 0 deletions(-) diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Middlewares/SwaggerAuthMiddleware.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Middlewares/SwaggerAuthMiddleware.cs" new file mode 100644 index 0000000..feba460 --- /dev/null +++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Middlewares/SwaggerAuthMiddleware.cs" @@ -0,0 +1,80 @@ +锘縰sing Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace WIDESEA_Core.Middlewares +{ + public class SwaggerAuthMiddleware + { + + private readonly RequestDelegate next; + + public SwaggerAuthMiddleware(RequestDelegate next) + { + this.next = next; + } + + public async Task InvokeAsync(HttpContext context) + { + // 涔熷彲浠ユ牴鎹槸鍚︽槸鏈湴鍋氬垽鏂� IsLocalRequest + if (context.Request.Path.Value.ToLower().Contains("index.html")) + { + // 鍒ゆ柇鏉冮檺鏄惁姝g‘ + if (IsAuthorized(context)) + { + await next.Invoke(context); + return; + } + + // 鏃犳潈闄愶紝璺宠浆swagger鐧诲綍椤� + context.Response.Redirect("/swg-login.html"); + } + else + { + await next.Invoke(context); + } + } + + public bool IsAuthorized(HttpContext context) + { + // 浣跨敤session妯″紡 + // 鍙互浣跨敤鍏朵粬鐨� + return context.Session.GetString("swagger-code") == "success"; + } + + /// <summary> + /// 鍒ゆ柇鏄笉鏄湰鍦拌闂� + /// 鏈湴涓嶇敤swagger鎷︽埅 + /// </summary> + /// <param name="context"></param> + /// <returns></returns> + public bool IsLocalRequest(HttpContext context) + { + if (context.Connection.RemoteIpAddress == null && context.Connection.LocalIpAddress == null) + { + return true; + } + if (context.Connection.RemoteIpAddress.Equals(context.Connection.LocalIpAddress)) + { + return true; + } + if (IPAddress.IsLoopback(context.Connection.RemoteIpAddress)) + { + return true; + } + return false; + } + } + public static class SwaggerAuthorizeExtensions + { + public static IApplicationBuilder UseSwaggerAuthorized(this IApplicationBuilder builder) + { + return builder.UseMiddleware<SwaggerAuthMiddleware>(); + } + } +} -- Gitblit v1.9.3