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>();
|
||||
|
||||
Reference in New Issue
Block a user