From 05309204e6af205955c67b23c99664952fb48ccc Mon Sep 17 00:00:00 2001 From: jianweie code Date: Wed, 18 Oct 2023 23:39:30 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=96=B0=E5=A2=9E=E3=80=91=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E8=8E=B7=E5=8F=96=E7=94=A8=E6=88=B7=E7=9C=9F=E5=AE=9E?= =?UTF-8?q?ip=E5=9C=B0=E5=9D=80=E4=B8=AD=E9=97=B4=E4=BB=B6=E3=80=90RealIpM?= =?UTF-8?q?iddleware=E3=80=91=EF=BC=8C=E9=98=B2=E6=AD=A2Nginx=E8=BD=AC?= =?UTF-8?q?=E5=8F=91=E5=90=8E=E5=8F=AA=E8=83=BD=E8=8E=B7=E5=8F=96=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8ip=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CoreCms.Net.Middlewares/MiddlewareHelpers.cs | 12 +++++++- CoreCms.Net.Middlewares/RealIpMiddleware.cs | 31 ++++++++++++++++++++ CoreCms.Net.Web.Admin/Program.cs | 3 ++ CoreCms.Net.Web.WebApi/Program.cs | 2 ++ 4 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 CoreCms.Net.Middlewares/RealIpMiddleware.cs diff --git a/CoreCms.Net.Middlewares/MiddlewareHelpers.cs b/CoreCms.Net.Middlewares/MiddlewareHelpers.cs index 97bd5a0d..1427de5d 100644 --- a/CoreCms.Net.Middlewares/MiddlewareHelpers.cs +++ b/CoreCms.Net.Middlewares/MiddlewareHelpers.cs @@ -30,6 +30,16 @@ namespace CoreCms.Net.Middlewares return app.UseMiddleware(); } + /// + /// 获取用户访问真实ip + /// + /// + /// + public static IApplicationBuilder RealIpMiddleware(this IApplicationBuilder app) + { + return app.UseMiddleware(); + } + /// /// 异常处理中间件(后端模式) @@ -67,7 +77,7 @@ namespace CoreCms.Net.Middlewares /// /// /// - public static IApplicationBuilder UseRecordAccessLogsMildd(this IApplicationBuilder app,string dataSources) + public static IApplicationBuilder UseRecordAccessLogsMildd(this IApplicationBuilder app, string dataSources) { return app.UseMiddleware(dataSources); } diff --git a/CoreCms.Net.Middlewares/RealIpMiddleware.cs b/CoreCms.Net.Middlewares/RealIpMiddleware.cs new file mode 100644 index 00000000..957a1c44 --- /dev/null +++ b/CoreCms.Net.Middlewares/RealIpMiddleware.cs @@ -0,0 +1,31 @@ +using Microsoft.AspNetCore.Http; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace CoreCms.Net.Middlewares +{ + /// + /// 获取真实ip地址 + /// + public class RealIpMiddleware + { + private readonly RequestDelegate _next; public RealIpMiddleware(RequestDelegate next) + { + _next = next; + } + public Task Invoke(HttpContext context) + { + var headers = context.Request.Headers; + if (headers.ContainsKey("X-Forwarded-For")) + { + context.Connection.RemoteIpAddress = IPAddress.Parse(headers["X-Forwarded-For"].ToString().Split(',', StringSplitOptions.RemoveEmptyEntries)[0]); + } + return _next(context); + } + } + +} diff --git a/CoreCms.Net.Web.Admin/Program.cs b/CoreCms.Net.Web.Admin/Program.cs index 5c8cd435..e455e517 100644 --- a/CoreCms.Net.Web.Admin/Program.cs +++ b/CoreCms.Net.Web.Admin/Program.cs @@ -158,6 +158,8 @@ app.UseForwardedHeaders(new ForwardedHeadersOptions #endregion #region 中间件注册=================================================================== +// 获取用户访问真实ip +app.RealIpMiddleware(); // 启用并发限制数中间件 app.UseConcurrencyLimiter(); // 开启Ip限流 @@ -168,6 +170,7 @@ app.UseRequestResponseLog(); app.UseRecordAccessLogsMildd(GlobalEnumVars.CoreShopSystemCategory.Admin.ToString()); // 记录ip请求 (注意开启权限,不然本地无法写入) app.UseIpLogMildd(); + #endregion app.UseSwagger().UseSwaggerUI(c => diff --git a/CoreCms.Net.Web.WebApi/Program.cs b/CoreCms.Net.Web.WebApi/Program.cs index 2661463d..c6ebcaf1 100644 --- a/CoreCms.Net.Web.WebApi/Program.cs +++ b/CoreCms.Net.Web.WebApi/Program.cs @@ -171,6 +171,8 @@ app.UseForwardedHeaders(new ForwardedHeadersOptions #endregion #region 中间件注册=================================================================== +// 获取用户访问真实ip +app.RealIpMiddleware(); // 启用并发限制数中间件 app.UseConcurrencyLimiter(); // 开启Ip限流