From b288e7effa0edc09baad6089433a38b2b29b29db Mon Sep 17 00:00:00 2001 From: sunpengfei <i@angelzzz.com> Date: 星期一, 08 九月 2025 15:33:03 +0800 Subject: [PATCH] feat:开发 --- FlexJobApi.Core/Utils/JwtUtils/JwtHandler.cs | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 58 insertions(+), 2 deletions(-) diff --git a/FlexJobApi.Core/Utils/JwtUtils/JwtHandler.cs b/FlexJobApi.Core/Utils/JwtUtils/JwtHandler.cs index e024dd2..d395149 100644 --- a/FlexJobApi.Core/Utils/JwtUtils/JwtHandler.cs +++ b/FlexJobApi.Core/Utils/JwtUtils/JwtHandler.cs @@ -1,7 +1,18 @@ -锘縰sing Furion.Authorization; +锘縰sing FlexJobApi.Core.Entities.Common; +using Furion; +using Furion.Authorization; +using Furion.DatabaseAccessor; using Furion.DataEncryption; +using Furion.DistributedIDGenerator; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Controllers; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; using System.Threading.Tasks; namespace FlexJobApi.Core; @@ -15,7 +26,52 @@ { await AuthorizeHandleAsync(context); } - else context.Fail(); + else + { + + var resourceLog = new ResourceLog(); + resourceLog.CreatedTime = DateTimeOffset.Now; + var stopwatch = Stopwatch.StartNew(); + var serviceScopeFactory = App.GetService<IServiceScopeFactory>(); + var serviceScope = serviceScopeFactory.CreateScope(); + var rep = serviceScope.ServiceProvider.GetRequiredService<IRepository<ResourceLog, LogDbContextLocator>>(); + resourceLog.Id = IDGen.NextID(); + resourceLog.TraceId = App.GetTraceId(); + resourceLog.Method = EnumUtils.GetEnum<EnumResourceMethod>(httpContext.Request.Method); + resourceLog.Domain = $"{httpContext.Request.Scheme}://{httpContext.Request.Host}"; + resourceLog.Path = httpContext.Request.Path; + resourceLog.ClientIpAddress = httpContext.GetRemoteIpAddressToIPv4(); + if (httpContext.Request.ContentType?.Contains("application/json") == true) + { + httpContext.Request.EnableBuffering(); // 鍏佽澶氭璇诲彇 + var body = await new StreamReader(httpContext.Request.Body).ReadToEndAsync(); + httpContext.Request.Body.Position = 0; // 閲嶇疆娴佷綅缃� + resourceLog.Request = body; + } + else + { + resourceLog.Request = httpContext.Request.Query.ToJson(); + } + resourceLog.RequestHeaders = httpContext.Request.Headers.ToJson(); + + context.Fail(); + + resourceLog.UpdatedTime = DateTimeOffset.Now; + resourceLog.Response = new FriendlyResult<object> + { + TraceId = resourceLog.TraceId, + Code = 401, + Success = false, + Timestamp = DateTime.Now.ToTimeStamp() + }.ToJson(); + resourceLog.ResponseHeaders = httpContext.Response.Headers.ToJson(); + resourceLog.IsSuccess = false; + + stopwatch.Stop(); + resourceLog.ElapsedMilliseconds = stopwatch.ElapsedMilliseconds; + + await rep.InsertNowAsync(resourceLog); + } } /// <summary> -- Gitblit v1.9.1