From 4244342e9ae95924a9e5a39eae25a7217fc65ed5 Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Tue, 24 May 2022 10:45:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8C=BA=E5=88=86=E8=AF=B7=E6=B1=82=E4=B8=BB?= =?UTF-8?q?=E6=9C=BA=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Serilog/HttpContextEnricher.cs | 5 +- .../Serilog/SerilogSetup.cs | 2 +- .../LimitUserRequestAuthorization.cs | 130 ++++++++++++++++++ .../BusinessFilter/UserTypeAuthorization.cs | 46 ------- .../Service/ImageAndDoc/StudyService.cs | 4 +- IRaCIS.Core.Application/TestService.cs | 14 +- .../AuthUser/IUserInfo.cs | 2 + IRaCIS.Core.Domain.Share/AuthUser/UserInfo.cs | 11 ++ 8 files changed, 156 insertions(+), 58 deletions(-) create mode 100644 IRaCIS.Core.Application/BusinessFilter/LimitUserRequestAuthorization.cs delete mode 100644 IRaCIS.Core.Application/BusinessFilter/UserTypeAuthorization.cs diff --git a/IRaCIS.Core.API/_ServiceExtensions/Serilog/HttpContextEnricher.cs b/IRaCIS.Core.API/_ServiceExtensions/Serilog/HttpContextEnricher.cs index 494598f81..4948a695e 100644 --- a/IRaCIS.Core.API/_ServiceExtensions/Serilog/HttpContextEnricher.cs +++ b/IRaCIS.Core.API/_ServiceExtensions/Serilog/HttpContextEnricher.cs @@ -28,7 +28,10 @@ namespace IRaCIS.Core.API _enrichAction = (logEvent, propertyFactory, httpContext) => { logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("RequestIP", httpContext.Connection.RemoteIpAddress.ToString())); - + + logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("LocalIP", httpContext.Connection.LocalIpAddress.MapToIPv4().ToString())); + + //这样读取没用 //logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("RequestBody", await ReadRequestBody(httpContext.Request))); //logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("RequestIP", IPHelper.GetIP(httpContext.Request) )); diff --git a/IRaCIS.Core.API/_ServiceExtensions/Serilog/SerilogSetup.cs b/IRaCIS.Core.API/_ServiceExtensions/Serilog/SerilogSetup.cs index 5dc92875e..f4af9e13a 100644 --- a/IRaCIS.Core.API/_ServiceExtensions/Serilog/SerilogSetup.cs +++ b/IRaCIS.Core.API/_ServiceExtensions/Serilog/SerilogSetup.cs @@ -26,7 +26,7 @@ namespace IRaCIS.Core.API //控制台 方便调试 问题 我们显示记录日志 时 获取上下文的ip 和用户名 用户类型 .WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Warning, - outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3} ] {ClientIp} {TokenUserRealName} {TokenUserType} {Message:lj} {Properties:j}{NewLine} {Exception}") + outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3} ] {LocalIP} {ClientIp} {TokenUserRealName} {TokenUserType} {Message:lj} {Properties:j}{NewLine} {Exception}") .WriteTo.File($"{AppContext.BaseDirectory}Serilogs/.log", rollingInterval: RollingInterval.Day, outputTemplate: "{Timestamp:HH:mm:ss} || {Level} || {SourceContext:l} || {Message} ||{Exception} ||end {NewLine}"); //.WriteTo.MSSqlServer("Data Source=DESKTOP-4TU9A6M;Initial Catalog=CoreFrame;User ID=sa;Password=123456", "logs", autoCreateSqlTable: true, restrictedToMinimumLevel: LogEventLevel.Information)//从左至右四个参数分别是数据库连接字符串、表名、如果表不存在是否创建、最低等级。Serilog会默认创建一些列。 diff --git a/IRaCIS.Core.Application/BusinessFilter/LimitUserRequestAuthorization.cs b/IRaCIS.Core.Application/BusinessFilter/LimitUserRequestAuthorization.cs new file mode 100644 index 000000000..1426eaebc --- /dev/null +++ b/IRaCIS.Core.Application/BusinessFilter/LimitUserRequestAuthorization.cs @@ -0,0 +1,130 @@ +using EasyCaching.Core; +using IRaCIS.Core.Domain.Share; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; + + +namespace IRaCIS.Core.Application.BusinessFilter; + + + + +public class LimitUserRequestAuthorization : IAsyncAuthorizationFilter +{ + + + private readonly IEasyCachingProvider _provider; + + private readonly IUserInfo _userInfo; + + public LimitUserRequestAuthorization(IEasyCachingProvider provider, IUserInfo userInfo) + { + _provider = provider; + _userInfo = userInfo; + } + + + public async Task OnAuthorizationAsync(AuthorizationFilterContext context) + { + if (context.ActionDescriptor.EndpointMetadata.Any(item => item is IAllowAnonymous)) + { + //匿名访问的不处理 + } + else + { + //1、用户登陆的时候,设置缓存 + + + + //2、在这里取缓存 进行比较 看是否有其他人进行了登陆,如果其他人登陆了,就把之前用户挤掉 + + var cacheUserToken = (await _provider.GetAsync(_userInfo.Id.ToString())).Value; + + if (cacheUserToken == null) + { + //设置当前用户最新Token + await _provider.SetAsync(_userInfo.Id.ToString(), _userInfo.UserToken, TimeSpan.FromDays(7)); + + } + //是同一个人 + else if(cacheUserToken == _userInfo.UserToken) + { + + } + else + { + context.Result = new StatusCodeResult(401); + } + + var cacheHostToken = (await _provider.GetAsync(_userInfo.IP.ToString()+_userInfo.LocalIp.ToString())).Value; + + if (cacheHostToken == null) + { + //设置当前主机最新Token + await _provider.SetAsync(_userInfo.IP.ToString() + _userInfo.LocalIp.ToString(), _userInfo.UserToken, TimeSpan.FromDays(7)); + + } + //是同主机 + else if (cacheHostToken == _userInfo.UserToken) + { + + } + else + { + context.Result = new StatusCodeResult(401); + } + + } + } +} + + + + + + + + + + + + + + + //public class UserTypeRequirement : IAuthorizationRequirement + //{ + //} + + //public class UserTypeHandler : AuthorizationHandler + //{ + + // private IUserInfo _userInfo; + + // public UserTypeHandler(IUserInfo userInfo) + // { + // _userInfo = userInfo; + // } + + + // protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, UserTypeRequirement requirement) + // { + + // //if (context.User.Claims.Count() == 0) + // //{ + // // return Task.CompletedTask; + // //} + + // //string userId = context.User.Claims.First(c => c.Type == "Userid").Value; + // //string qq = context.User.Claims.First(c => c.Type == "QQ").Value; + + // //if (_UserService.Validata(userId, qq)) + // //{ + // // context.Succeed(requirement); //验证通过了 + // //} + // ////在这里就可以做验证 + + // return Task.CompletedTask; + // } + //} + diff --git a/IRaCIS.Core.Application/BusinessFilter/UserTypeAuthorization.cs b/IRaCIS.Core.Application/BusinessFilter/UserTypeAuthorization.cs deleted file mode 100644 index a46b853cb..000000000 --- a/IRaCIS.Core.Application/BusinessFilter/UserTypeAuthorization.cs +++ /dev/null @@ -1,46 +0,0 @@ -using IRaCIS.Core.Infra.EFCore; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc.Filters; -using System; -using System.Collections.Generic; -using System.Linq; - -namespace IRaCIS.Core.Application.BusinessFilter -{ - - //public class UserTypeRequirement : IAuthorizationRequirement - //{ - //} - - //public class UserTypeHandler : AuthorizationHandler - //{ - - // private IUserInfo _userInfo; - - // public UserTypeHandler(IUserInfo userInfo) - // { - // _userInfo = userInfo; - // } - - - // protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, UserTypeRequirement requirement) - // { - - // //if (context.User.Claims.Count() == 0) - // //{ - // // return Task.CompletedTask; - // //} - - // //string userId = context.User.Claims.First(c => c.Type == "Userid").Value; - // //string qq = context.User.Claims.First(c => c.Type == "QQ").Value; - - // //if (_UserService.Validata(userId, qq)) - // //{ - // // context.Succeed(requirement); //验证通过了 - // //} - // ////在这里就可以做验证 - - // return Task.CompletedTask; - // } - //} -} diff --git a/IRaCIS.Core.Application/Service/ImageAndDoc/StudyService.cs b/IRaCIS.Core.Application/Service/ImageAndDoc/StudyService.cs index 312acfa3d..a3acff7d3 100644 --- a/IRaCIS.Core.Application/Service/ImageAndDoc/StudyService.cs +++ b/IRaCIS.Core.Application/Service/ImageAndDoc/StudyService.cs @@ -1,13 +1,11 @@ using AutoMapper; using IRaCIS.Application.Interfaces; using IRaCIS.Core.Application.Contracts.Dicom.DTO; -using IRaCIS.Core.Infra.EFCore; using IRaCIS.Core.Domain.Share; using IRaCIS.Core.Application.Contracts; using IRaCIS.Core.Application.Service.Inspection.Interface; -using IRaCIS.Core.Application.Service.Inspection.DTO; -using Newtonsoft.Json; + namespace IRaCIS.Application.Services { diff --git a/IRaCIS.Core.Application/TestService.cs b/IRaCIS.Core.Application/TestService.cs index aa7668223..0daa69fc5 100644 --- a/IRaCIS.Core.Application/TestService.cs +++ b/IRaCIS.Core.Application/TestService.cs @@ -21,25 +21,25 @@ namespace IRaCIS.Application.Services public string Get() { - return String.Empty; + return _userInfo.LocalIp; } [HttpPost] public string Get(testModel testModel) { - var aaabb = _trialRepository.BatchDeleteNoTrackingAsync(t => t.Id == Guid.Empty).Result; + //var aaabb = _trialRepository.BatchDeleteNoTrackingAsync(t => t.Id == Guid.Empty).Result; - var aaaa = _dicRepository.BatchDeleteNoTrackingAsync(t => t.Id == Guid.Empty).Result; + //var aaaa = _dicRepository.BatchDeleteNoTrackingAsync(t => t.Id == Guid.Empty).Result; - var waitModifyEntity = _dicRepository.FirstOrDefaultAsync(t => t.Id == Guid.Parse("e2b97a6c-35a6-4aa3-7f27-08da13ab33ff")).GetAwaiter().GetResult(); + //var waitModifyEntity = _dicRepository.FirstOrDefaultAsync(t => t.Id == Guid.Parse("e2b97a6c-35a6-4aa3-7f27-08da13ab33ff")).GetAwaiter().GetResult(); - var tt = _dicRepository.UpdateAsync(waitModifyEntity, t => new Dictionary() { Description = "xxxxx" }, true).Result; + //var tt = _dicRepository.UpdateAsync(waitModifyEntity, t => new Dictionary() { Description = "xxxxx" }, true).Result; - var tt2 = _trialRepository.UpdatePartialFromQueryAsync(Guid.Parse("543d0000-3e10-0016-77e9-08da2827228a"), t => new Trial() { Indication = "WCH测试稽查002" }, true).Result; + //var tt2 = _trialRepository.UpdatePartialFromQueryAsync(Guid.Parse("543d0000-3e10-0016-77e9-08da2827228a"), t => new Trial() { Indication = "WCH测试稽查002" }, true).Result; @@ -68,7 +68,7 @@ namespace IRaCIS.Application.Services var b = _localizer["test{0}", "测试"]; //return _localizer["test{0}", "测试"]; - return _userInfo.RequestUrl; + return _userInfo.LocalIp; } } diff --git a/IRaCIS.Core.Domain.Share/AuthUser/IUserInfo.cs b/IRaCIS.Core.Domain.Share/AuthUser/IUserInfo.cs index 9da2ebb9e..a34aaa292 100644 --- a/IRaCIS.Core.Domain.Share/AuthUser/IUserInfo.cs +++ b/IRaCIS.Core.Domain.Share/AuthUser/IUserInfo.cs @@ -39,6 +39,8 @@ namespace IRaCIS.Core.Domain.Share string IP { get; } + string LocalIp { get; } + bool IsEn_Us { get; } string RequestUrl { get; } diff --git a/IRaCIS.Core.Domain.Share/AuthUser/UserInfo.cs b/IRaCIS.Core.Domain.Share/AuthUser/UserInfo.cs index 3428b4642..123147497 100644 --- a/IRaCIS.Core.Domain.Share/AuthUser/UserInfo.cs +++ b/IRaCIS.Core.Domain.Share/AuthUser/UserInfo.cs @@ -185,6 +185,17 @@ namespace IRaCIS.Core.Domain.Share } } + + public string LocalIp + { + get + { + + return _accessor?.HttpContext?.Request.Host.Value; + } + } + + public bool IsEn_Us { get