From 8f0643f615202729a9a6ef9034d190b6642c0541 Mon Sep 17 00:00:00 2001 From: wanghaomei Date: Mon, 9 Jan 2023 10:32:28 +0800 Subject: [PATCH] =?UTF-8?q?API=E6=8E=A5=E5=8F=A3=E6=8E=88=E6=9D=83?= =?UTF-8?q?=E7=BC=96=E8=BE=91=E4=B8=8E=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Myshipping.Application.xml | 6 +++ .../Service/DataSync/DataSyncService.cs | 6 ++- .../Attributes/ApiUserAttribute.cs | 2 +- Myshipping.Core/Filter/ApiUserFilter.cs | 46 +++++++++++++++---- Myshipping.Core/Myshipping.Core.xml | 15 ++---- .../Service/DjyApiAuth/DjyApiAuthService.cs | 6 ++- .../Service/DjyApiAuth/Dto/DjyApiAuthInput.cs | 18 ++++---- 7 files changed, 67 insertions(+), 32 deletions(-) diff --git a/Myshipping.Application/Myshipping.Application.xml b/Myshipping.Application/Myshipping.Application.xml index cf3b89a9..8e6a0e16 100644 --- a/Myshipping.Application/Myshipping.Application.xml +++ b/Myshipping.Application/Myshipping.Application.xml @@ -9968,6 +9968,12 @@ 数据同步服务 + + + 测试用 + + + 订舱客户同步 diff --git a/Myshipping.Application/Service/DataSync/DataSyncService.cs b/Myshipping.Application/Service/DataSync/DataSyncService.cs index d343a194..2fa325c9 100644 --- a/Myshipping.Application/Service/DataSync/DataSyncService.cs +++ b/Myshipping.Application/Service/DataSync/DataSyncService.cs @@ -75,7 +75,11 @@ namespace Myshipping.Application #endregion #region 其他 - [HttpGet("/DataSync/Test"), ApiUser] + /// + /// 测试用 + /// + /// + [HttpGet("/DataSync/Test"), ApiUser(ApiCode = "Test")] public async Task Test() { return $"当前用户:{UserManager.UserId} {UserManager.Name} ,当前租户:{UserManager.TENANT_ID} {UserManager.TENANT_NAME},管理员类型:{(UserManager.IsSuperAdmin ? "超级管理员" : (UserManager.IsTenantAdmin ? "租户管理员" : "普通用户"))}"; diff --git a/Myshipping.Core/Attributes/ApiUserAttribute.cs b/Myshipping.Core/Attributes/ApiUserAttribute.cs index 8d0bb588..81ae44e9 100644 --- a/Myshipping.Core/Attributes/ApiUserAttribute.cs +++ b/Myshipping.Core/Attributes/ApiUserAttribute.cs @@ -8,5 +8,5 @@ namespace Myshipping.Core; [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property)] public class ApiUserAttribute : Attribute { - + public string ApiCode { get; set; } } diff --git a/Myshipping.Core/Filter/ApiUserFilter.cs b/Myshipping.Core/Filter/ApiUserFilter.cs index 210c8f6e..d31b50c2 100644 --- a/Myshipping.Core/Filter/ApiUserFilter.cs +++ b/Myshipping.Core/Filter/ApiUserFilter.cs @@ -1,4 +1,6 @@ using Furion; +using Furion.FriendlyException; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.AspNetCore.Mvc.Filters; @@ -12,25 +14,48 @@ using System.Threading.Tasks; namespace Myshipping.Core { + /// + /// API接口调用用户鉴权Filter + /// public class ApiUserFilter : IAsyncActionFilter { public Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { var actionDescriptor = context.ActionDescriptor as ControllerActionDescriptor; - foreach (var metadata in actionDescriptor.EndpointMetadata) + var attrAllowAnonymous = actionDescriptor.EndpointMetadata.FirstOrDefault(x => x.GetType() == typeof(AllowAnonymousAttribute)); + var attrApiUser = actionDescriptor.EndpointMetadata.FirstOrDefault(x => x.GetType() == typeof(ApiUserAttribute)); + if (attrAllowAnonymous != null && attrApiUser != null) { - if (metadata.GetType() == typeof(ApiUserAttribute)) + var apiUser = attrApiUser as ApiUserAttribute; + if (context.HttpContext.Request.Headers.ContainsKey(CommonConst.API_USER_HEADER_KEY) + && context.HttpContext.Request.Headers.ContainsKey(CommonConst.API_USER_HEADER_SECRET)) { - //if (context.HttpContext.Request.Headers.ContainsKey(CommonConst.API_USER_HEADER_KEY) - //&& context.HttpContext.Request.Headers.ContainsKey(CommonConst.API_USER_HEADER_SECRET)) + var key = context.HttpContext.Request.Headers[CommonConst.API_USER_HEADER_KEY].ToString(); + var secret = context.HttpContext.Request.Headers[CommonConst.API_USER_HEADER_SECRET].ToString(); + + var httpContext = App.GetService().HttpContext; + + var repApiAuth = App.GetService>(); + var repTenant = App.GetService>(); + var repUser = App.GetService>(); + + //未设置ApiCode时,使用方法名称 + if (string.IsNullOrEmpty(apiUser.ApiCode)) { - var httpContext = App.GetService().HttpContext; + apiUser.ApiCode = actionDescriptor.MethodInfo.Name; + } - var repTenant = App.GetService>(); - var repUser = App.GetService>(); + var auth = repApiAuth.AsQueryable().Filter(null, true).First(x => + x.ApiCode == apiUser.ApiCode + && x.ApiKey == key + && x.ApiSecret == secret + && x.IsDeleted == false + && x.IsDisable == false); - var tenant = repTenant.AsQueryable().Filter(null, true).First(x => x.Id == 142307070918780L); - var user = repUser.AsQueryable().Filter(null, true).First(x => x.Id == 142307070910551L); + if (auth != null && (!auth.ExpireDate.HasValue || auth.ExpireDate > DateTime.Now)) + { + var tenant = repTenant.AsQueryable().Filter(null, true).First(x => x.Id == auth.TenantId); + var user = repUser.AsQueryable().Filter(null, true).First(x => x.Id == auth.UserId); ClaimsIdentity identity = new ClaimsIdentity("AuthenticationTypes.Federation"); identity.AddClaim(new Claim(type: ClaimConst.CLAINM_USERID, value: user.Id.ToString())); @@ -42,9 +67,10 @@ namespace Myshipping.Core identity.AddClaim(new Claim(type: ClaimConst.TENANT_NAME, value: tenant.Name)); ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(identity); httpContext.User = claimsPrincipal; + return next(); } - } + throw Oops.Oh("无权调用!请检查授权或联系管理员。"); } return next(); diff --git a/Myshipping.Core/Myshipping.Core.xml b/Myshipping.Core/Myshipping.Core.xml index 539b294e..5080a8f3 100644 --- a/Myshipping.Core/Myshipping.Core.xml +++ b/Myshipping.Core/Myshipping.Core.xml @@ -5365,6 +5365,11 @@ 文件名 返回合法的文件名 + + + API接口调用用户鉴权Filter + + 全局异常处理 @@ -9386,16 +9391,6 @@ 接口名称 - - - 接口KEY - - - - - 接口秘钥 - - 有效截止日期 diff --git a/Myshipping.Core/Service/DjyApiAuth/DjyApiAuthService.cs b/Myshipping.Core/Service/DjyApiAuth/DjyApiAuthService.cs index f2a5c522..3986ecd4 100644 --- a/Myshipping.Core/Service/DjyApiAuth/DjyApiAuthService.cs +++ b/Myshipping.Core/Service/DjyApiAuth/DjyApiAuthService.cs @@ -8,6 +8,8 @@ using System.Linq; using System.Threading.Tasks; using Myshipping.Core.Entity; using Microsoft.Extensions.Logging; +using System; +using Furion.DataEncryption; namespace Myshipping.Core.Service { @@ -53,9 +55,11 @@ namespace Myshipping.Core.Service public async Task Save(SaveDjyApiAuthInput input) { DjyApiAuth entity = null; - if (input.Id > 0) + if (input.Id == 0) { entity = input.Adapt(); + entity.ApiKey = Guid.NewGuid().ToString().Replace("-", "").ToLower(); + entity.ApiSecret = DESCEncryption.Encrypt(Guid.NewGuid().ToString(), "132456").ToLower(); await _rep.InsertAsync(entity); } else diff --git a/Myshipping.Core/Service/DjyApiAuth/Dto/DjyApiAuthInput.cs b/Myshipping.Core/Service/DjyApiAuth/Dto/DjyApiAuthInput.cs index cc0331f7..6924e9f1 100644 --- a/Myshipping.Core/Service/DjyApiAuth/Dto/DjyApiAuthInput.cs +++ b/Myshipping.Core/Service/DjyApiAuth/Dto/DjyApiAuthInput.cs @@ -19,15 +19,15 @@ namespace Myshipping.Core.Service /// public virtual string ApiName { get; set; } - /// - /// 接口KEY - /// - public virtual string ApiKey { get; set; } - - /// - /// 接口秘钥 - /// - public virtual string ApiSecret { get; set; } + ///// + ///// 接口KEY + ///// + //public virtual string ApiKey { get; set; } + + ///// + ///// 接口秘钥 + ///// + //public virtual string ApiSecret { get; set; } /// /// 有效截止日期