|
|
@ -1,4 +1,6 @@
|
|
|
|
using Furion;
|
|
|
|
using Furion;
|
|
|
|
|
|
|
|
using Furion.FriendlyException;
|
|
|
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Microsoft.AspNetCore.Mvc.Controllers;
|
|
|
|
using Microsoft.AspNetCore.Mvc.Controllers;
|
|
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
|
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
|
|
@ -12,25 +14,48 @@ using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Myshipping.Core
|
|
|
|
namespace Myshipping.Core
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// API接口调用用户鉴权Filter
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
public class ApiUserFilter : IAsyncActionFilter
|
|
|
|
public class ApiUserFilter : IAsyncActionFilter
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
|
|
|
|
public Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var actionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;
|
|
|
|
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)
|
|
|
|
var key = context.HttpContext.Request.Headers[CommonConst.API_USER_HEADER_KEY].ToString();
|
|
|
|
//&& context.HttpContext.Request.Headers.ContainsKey(CommonConst.API_USER_HEADER_SECRET))
|
|
|
|
var secret = context.HttpContext.Request.Headers[CommonConst.API_USER_HEADER_SECRET].ToString();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var httpContext = App.GetService<IHttpContextAccessor>().HttpContext;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var repApiAuth = App.GetService<SqlSugarRepository<DjyApiAuth>>();
|
|
|
|
|
|
|
|
var repTenant = App.GetService<SqlSugarRepository<SysTenant>>();
|
|
|
|
|
|
|
|
var repUser = App.GetService<SqlSugarRepository<SysUser>>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//未设置ApiCode时,使用方法名称
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(apiUser.ApiCode))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var httpContext = App.GetService<IHttpContextAccessor>().HttpContext;
|
|
|
|
apiUser.ApiCode = actionDescriptor.MethodInfo.Name;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var repTenant = App.GetService<SqlSugarRepository<SysTenant>>();
|
|
|
|
var auth = repApiAuth.AsQueryable().Filter(null, true).First(x =>
|
|
|
|
var repUser = App.GetService<SqlSugarRepository<SysUser>>();
|
|
|
|
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);
|
|
|
|
if (auth != null && (!auth.ExpireDate.HasValue || auth.ExpireDate > DateTime.Now))
|
|
|
|
var user = repUser.AsQueryable().Filter(null, true).First(x => x.Id == 142307070910551L);
|
|
|
|
{
|
|
|
|
|
|
|
|
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");
|
|
|
|
ClaimsIdentity identity = new ClaimsIdentity("AuthenticationTypes.Federation");
|
|
|
|
identity.AddClaim(new Claim(type: ClaimConst.CLAINM_USERID, value: user.Id.ToString()));
|
|
|
|
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));
|
|
|
|
identity.AddClaim(new Claim(type: ClaimConst.TENANT_NAME, value: tenant.Name));
|
|
|
|
ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(identity);
|
|
|
|
ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(identity);
|
|
|
|
httpContext.User = claimsPrincipal;
|
|
|
|
httpContext.User = claimsPrincipal;
|
|
|
|
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw Oops.Oh("无权调用!请检查授权或联系管理员。");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return next();
|
|
|
|
return next();
|
|
|
|