feat: 优化权限使用方式
This commit is contained in:
@@ -17,30 +17,13 @@ namespace Yi.Framework.Auth.JwtBearer.Authorization
|
||||
|
||||
public class PermissionAttribute : ActionFilterAttribute
|
||||
{
|
||||
private string Permission { get; set; }
|
||||
internal string Code { get; set; }
|
||||
|
||||
public PermissionAttribute(string permission)
|
||||
public PermissionAttribute(string code)
|
||||
{
|
||||
this.Permission = permission;
|
||||
this.Code = code;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 动作鉴权
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public override void OnActionExecuting(ActionExecutingContext context)
|
||||
{
|
||||
|
||||
var permissionHandler = ServiceLocatorModel.Instance.GetRequiredService<IPermissionHandler>();
|
||||
|
||||
var result = permissionHandler.IsPass(Permission);
|
||||
|
||||
if (!result)
|
||||
{
|
||||
throw new AuthException(message: $"您无权限访问该接口-{ context.HttpContext.Request.Path.Value}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Microsoft.AspNetCore.Mvc.Controllers;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Yi.Framework.Auth.JwtBearer.Authorization;
|
||||
using Yi.Framework.Core.Exceptions;
|
||||
|
||||
namespace SF.AspNetCore.Auth.Authorization;
|
||||
public class PermissionGlobalAttribute : ActionFilterAttribute
|
||||
{
|
||||
private readonly IPermissionHandler _permissionHandler;
|
||||
public PermissionGlobalAttribute(IPermissionHandler permissionHandler)
|
||||
{
|
||||
_permissionHandler=permissionHandler;
|
||||
}
|
||||
public override void OnActionExecuting(ActionExecutingContext context)
|
||||
{
|
||||
if (context.ActionDescriptor is not ControllerActionDescriptor controllerActionDescriptor) return;
|
||||
PermissionAttribute? perAttribute = controllerActionDescriptor.MethodInfo.GetCustomAttributes(inherit: true)
|
||||
.FirstOrDefault(a => a.GetType().Equals(typeof(PermissionAttribute))) as PermissionAttribute;
|
||||
//空对象直接返回
|
||||
if (perAttribute is null) return;
|
||||
|
||||
var result = _permissionHandler.IsPass(perAttribute.Code);
|
||||
|
||||
if (!result)
|
||||
{
|
||||
throw new AuthException(message: $"您无权限访问该接口-{context.HttpContext.Request.Path.Value}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SF.AspNetCore.Auth.Authorization;
|
||||
using StartupModules;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -32,6 +34,10 @@ namespace Yi.Framework.Auth.JwtBearer
|
||||
{
|
||||
option.AddScheme<YiJwtAuthenticationHandler>(YiJwtAuthenticationHandler.YiJwtSchemeName, YiJwtAuthenticationHandler.YiJwtSchemeName);
|
||||
});
|
||||
services.AddSingleton<PermissionGlobalAttribute>();
|
||||
services.AddControllers(options => {
|
||||
options.Filters.Add<PermissionGlobalAttribute>();
|
||||
});
|
||||
//services.AddSingleton<PermissionAttribute>();
|
||||
//services.AddControllers(options => {
|
||||
// options.Filters.Add<PermissionAttribute>();
|
||||
|
||||
@@ -11,6 +11,7 @@ using Yi.RBAC.Domain.Identity.Repositories;
|
||||
using SqlSugar;
|
||||
using Mapster;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Yi.Framework.Auth.JwtBearer.Authorization;
|
||||
|
||||
namespace Yi.RBAC.Application.Identity
|
||||
{
|
||||
|
||||
@@ -10,7 +10,5 @@ namespace Yi.RBAC.Domain.Shared.Identity.Etos
|
||||
{
|
||||
public long UserId { get; set; }
|
||||
public string UserName { get; set; }
|
||||
|
||||
public string LogMsg { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -741,6 +741,20 @@
|
||||
登录信息
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:Yi.RBAC.Domain.Logs.Event.LoginEventHandler.GetClientInfo(Microsoft.AspNetCore.Http.HttpContext)">
|
||||
<summary>
|
||||
获取客户端信息
|
||||
</summary>
|
||||
<param name="context"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:Yi.RBAC.Domain.Logs.Event.LoginEventHandler.GetLoginLogInfo(Microsoft.AspNetCore.Http.HttpContext)">
|
||||
<summary>
|
||||
记录用户登陆信息
|
||||
</summary>
|
||||
<param name="context"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="T:Yi.RBAC.Domain.Setting.Entities.ConfigEntity">
|
||||
<summary>
|
||||
配置表
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Cike.EventBus.EventHandlerAbstracts;
|
||||
using IPTools.Core;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -6,6 +7,8 @@ using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UAParser;
|
||||
using Yi.Framework.AspNetCore.Extensions;
|
||||
using Yi.Framework.Ddd.Repositories;
|
||||
using Yi.RBAC.Domain.Logs.Entities;
|
||||
using Yi.RBAC.Domain.Shared.Identity.Etos;
|
||||
@@ -23,14 +26,56 @@ namespace Yi.RBAC.Domain.Logs.Event
|
||||
}
|
||||
public Task HandlerAsync(LoginEventArgs eventData)
|
||||
{
|
||||
var loginLogEntity = new LoginLogEntity();
|
||||
var loginLogEntity = GetLoginLogInfo(_httpContext);
|
||||
loginLogEntity.Id = SnowflakeHelper.NextId;
|
||||
loginLogEntity.LogMsg = eventData.LogMsg;
|
||||
loginLogEntity.LogMsg = eventData.UserName + "登录系统";
|
||||
loginLogEntity.LoginUser = eventData.UserName;
|
||||
loginLogEntity.LoginIp = _httpContext.GetClientIp();
|
||||
|
||||
_loginLogRepository.InsertAsync(loginLogEntity);
|
||||
Console.WriteLine(eventData.UserName + "登录系统");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取客户端信息
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
private static ClientInfo GetClientInfo(HttpContext context)
|
||||
{
|
||||
var str = context.GetUserAgent();
|
||||
var uaParser = Parser.GetDefault();
|
||||
ClientInfo c = uaParser.Parse(str);
|
||||
return c;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录用户登陆信息
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
private static LoginLogEntity GetLoginLogInfo(HttpContext context)
|
||||
{
|
||||
var ipAddr = context.GetClientIp();
|
||||
IpInfo location;
|
||||
if (ipAddr == "127.0.0.1")
|
||||
{
|
||||
location = new IpInfo() { Province = "本地", City = "本机" };
|
||||
}
|
||||
else
|
||||
{
|
||||
location = IpTool.Search(ipAddr);
|
||||
}
|
||||
ClientInfo clientInfo = GetClientInfo(context);
|
||||
LoginLogEntity entity = new()
|
||||
{
|
||||
Browser = clientInfo.Device.Family,
|
||||
Os = clientInfo.OS.ToString(),
|
||||
LoginIp = ipAddr,
|
||||
LoginLocation = location.Province + "-" + location.City
|
||||
};
|
||||
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Hei.Captcha" Version="0.3.0" />
|
||||
<PackageReference Include="IPTools.China" Version="1.6.0" />
|
||||
<PackageReference Include="UAParser" Version="3.1.47" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\framework\Yi.Framework.Data\Yi.Framework.Data.csproj" />
|
||||
|
||||
@@ -16,6 +16,9 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="ip2region.db">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="key.pem">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
|
||||
BIN
Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Web/ip2region.db
Normal file
BIN
Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Web/ip2region.db
Normal file
Binary file not shown.
Reference in New Issue
Block a user