天天看點

[Abp 源碼分析]十一、權限驗證

0.簡介

Abp 本身內建了一套權限驗證體系,通過 ASP.NET Core 的過濾器與 Castle 的攔截器進行攔截請求,并進行權限驗證。在 Abp 架構内部,權限分為兩塊,一個是功能(Feature),一個是權限項(Permission),在更多的時候兩者僅僅是概念不同而已,大體處理流程還是一樣的。

由于 Abp 本身是針對多租戶架構進行設計的,功能是相對于租戶而言,比如針對 A 租戶他每月的短信發送配額為 10000 條,而針對 B 租戶其配額為 5000 條,可能 C 租戶該功能都沒有開通。

本篇文章僅針對基本的驗證機制進行解析,後續文章會進行詳解。

0.1 驗證流程圖

[Abp 源碼分析]十一、權限驗證

1.啟動流程

1.1 流程圖

[Abp 源碼分析]十一、權限驗證

1.2 代碼流程

首先在注入 Abp 架構的時候,通過注入過濾器一起将權限驗證過濾器進行了注入。

internal static class AbpMvcOptionsExtensions
{
    // ... 其他代碼

    private static void AddFilters(MvcOptions options)
    {
        // ... 其他注入的過濾器
        options.Filters.AddService(typeof(AbpAuthorizationFilter));
        // ... 其他注入的過濾器
    }

    // ... 其他代碼
}
           

Abp 除了攔截驗證 API 接口,同時也通過 Castle Windsor Interceptor 來驗證普通類型的方法,來檢測目前使用者是否有權限進行調用。攔截器的注冊則是存放在

AbpBootstrapper

對象初始化的時候,通過

AddInterceptorRegistrars()

方法注入 Abp 自帶的攔截器對象。

private AbpBootstrapper([NotNull] Type startupModule, [CanBeNull] Action<AbpBootstrapperOptions> optionsAction = null)
{
    Check.NotNull(startupModule, nameof(startupModule));

    var options = new AbpBootstrapperOptions();
    optionsAction?.Invoke(options);
    
    // 其他初始化代碼
    
    // 判斷使用者在啟用 Abp 架構的是時候是否禁用了所有的攔截器
    if (!options.DisableAllInterceptors)
    {
        // 初始化攔截器
        AddInterceptorRegistrars();
    }
}

private void AddInterceptorRegistrars()
{
    // 參數驗證攔截器注冊
    ValidationInterceptorRegistrar.Initialize(IocManager);
    // 審計資訊記錄攔截器注冊
    AuditingInterceptorRegistrar.Initialize(IocManager);
    // 實體變更追蹤攔截器注冊
    EntityHistoryInterceptorRegistrar.Initialize(IocManager);
    // 工作單元攔截器注冊
    UnitOfWorkRegistrar.Initialize(IocManager);
    // 授權攔截器注冊
    AuthorizationInterceptorRegistrar.Initialize(IocManager);
}
           

Abp 通過注入過濾器與攔截器就能夠從源頭驗證并控制權限校驗邏輯,以上就是 Abp 在啟動時所做的操作。

2.代碼分析

總體來說,Abp 針對權限的驗證就是攔截+檢測,整體思路即是這樣,隻是實作可能略微複雜,請耐心往下看。

2.1 權限攔截器與權限過濾器

首先我們從入口點開始分析代碼,在上一節我們說過 Abp 通過攔截器與過濾器來實作權限的攔截與處理,那麼在其内部是如何進行處理的呢?

其實很簡單,在權限攔截器與權限過濾器的内部實作都使用了

IAuthorizationHelper

AuthorizeAsync()

方法來進行權限校驗。

2.1.1 權限過濾器代碼實作

public class AbpAuthorizationFilter : IAsyncAuthorizationFilter, ITransientDependency
{
    public ILogger Logger { get; set; }

	// 權限驗證類,這個才是真正針對權限進行驗證的對象
    private readonly IAuthorizationHelper _authorizationHelper;
    // 異常包裝器,這個玩意兒在我的《[Abp 源碼分析]十、異常處理》有講,主要是用來封裝沒有授權時傳回的錯誤資訊
    private readonly IErrorInfoBuilder _errorInfoBuilder;
    // 事件總線處理器,同樣在我的《[Abp 源碼分析]九、事件總線》有講,在這裡用于觸發一個未授權請求引發的事件,使用者可以監聽此事件來進行自己的處理
    private readonly IEventBus _eventBus;

	// 構造注入
    public AbpAuthorizationFilter(
        IAuthorizationHelper authorizationHelper,
        IErrorInfoBuilder errorInfoBuilder,
        IEventBus eventBus)
    {
        _authorizationHelper = authorizationHelper;
        _errorInfoBuilder = errorInfoBuilder;
        _eventBus = eventBus;
        Logger = NullLogger.Instance;
    }

    public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
    {
        // 如果注入了 IAllowAnonymousFilter 過濾器則允許所有匿名請求
        if (context.Filters.Any(item => item is IAllowAnonymousFilter))
        {
            return;
        }

	    // 如果不是一個控制器方法則直接傳回
        if (!context.ActionDescriptor.IsControllerAction())
        {
            return;
        }

		// 開始使用 IAuthorizationHelper 來進行權限校驗
        try
        {
            await _authorizationHelper.AuthorizeAsync(
                context.ActionDescriptor.GetMethodInfo(),
                context.ActionDescriptor.GetMethodInfo().DeclaringType
            );
        }
        // 如果是未授權異常的處理邏輯
        catch (AbpAuthorizationException ex)
        {
        	// 記錄日志
            Logger.Warn(ex.ToString(), ex);

		    // 觸發異常事件
            _eventBus.Trigger(this, new AbpHandledExceptionData(ex));

		    // 如果接口的傳回類型為 ObjectResult,則采用 AjaxResponse 對象進行封裝資訊
            if (ActionResultHelper.IsObjectResult(context.ActionDescriptor.GetMethodInfo().ReturnType))
            {
                context.Result = new ObjectResult(new AjaxResponse(_errorInfoBuilder.BuildForException(ex), true))
                {
                    StatusCode = context.HttpContext.User.Identity.IsAuthenticated
                        ? (int) System.Net.HttpStatusCode.Forbidden
                        : (int) System.Net.HttpStatusCode.Unauthorized
                };
            }
            else
            {
                context.Result = new ChallengeResult();
            }
        }
        // 其他異常則顯示為内部異常資訊
        catch (Exception ex)
        {
            Logger.Error(ex.ToString(), ex);

            _eventBus.Trigger(this, new AbpHandledExceptionData(ex));

            if (ActionResultHelper.IsObjectResult(context.ActionDescriptor.GetMethodInfo().ReturnType))
            {
                context.Result = new ObjectResult(new AjaxResponse(_errorInfoBuilder.BuildForException(ex)))
                {
                    StatusCode = (int) System.Net.HttpStatusCode.InternalServerError
                };
            }
            else
            {
                //TODO: How to return Error page?
                context.Result = new StatusCodeResult((int)System.Net.HttpStatusCode.InternalServerError);
            }
        }
    }
}
           

2.1.2 權限攔截器初始化綁定

權限攔截器在 Abp 架構初始化完成的時候就開始監聽了元件注冊事件,隻要被注入的類型實作了

AbpAuthorizeAttribute

特性與

RequiresFeatureAttribute

特性都會被注入

AuthorizationInterceptor

攔截器。

internal static class AuthorizationInterceptorRegistrar
{
    public static void Initialize(IIocManager iocManager)
    {
        // 監聽 DI 元件注冊事件
        iocManager.IocContainer.Kernel.ComponentRegistered += Kernel_ComponentRegistered;            
    }

    private static void Kernel_ComponentRegistered(string key, IHandler handler)
    {
        // 判斷注入的類型是否符合要求
        if (ShouldIntercept(handler.ComponentModel.Implementation))
        {
            // 符合要求,針對該元件添權重限攔截器
            handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(AuthorizationInterceptor))); 
        }
    }

    private static bool ShouldIntercept(Type type)
    {
        if (SelfOrMethodsDefinesAttribute<AbpAuthorizeAttribute>(type))
        {
            return true;
        }

        if (SelfOrMethodsDefinesAttribute<RequiresFeatureAttribute>(type))
        {
            return true;
        }

        return false;
    }

    private static bool SelfOrMethodsDefinesAttribute<TAttr>(Type type)
    {
        // 判斷傳入的 Type 有定義 TAttr 類型的特性
        if (type.GetTypeInfo().IsDefined(typeof(TAttr), true))
        {
            return true;
        }

        // 或者說,該類型的所有公開的方法是否有方法标注了 TAttr 類型的特性
        return type
            .GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
            .Any(m => m.IsDefined(typeof(TAttr), true));
    }
}
           

2.1.3 權限攔截器實作

Abp 架構針對權限攔截器的實作則是簡單了許多,隻是在被攔截的方法在執行的時候,會直接使用

IAuthorizationHelper

進行權限驗證。

public class AuthorizationInterceptor : IInterceptor
{
    private readonly IAuthorizationHelper _authorizationHelper;

    public AuthorizationInterceptor(IAuthorizationHelper authorizationHelper)
    {
        _authorizationHelper = authorizationHelper;
    }

    public void Intercept(IInvocation invocation)
    {
    	// 使用 IAuthorizationHelper 進行權限驗證
        _authorizationHelper.Authorize(invocation.MethodInvocationTarget, invocation.TargetType);
        invocation.Proceed();
    }
}
           

2.2 權限特性

在 Abp 架構裡面定義了兩組特性,第一個是

AbpMvcAuthorizeAttribute

,适用于 MVC 控制器,它是直接繼承了 ASP .NET Core 自帶的權限驗證特性

AuthorizeAttribute

,當控制器或者控制器内部的方法标注了該特性,就會進入之前 Abp 定義的權限過濾器

AbpAuthorizationFilter

内部。

第二種特性則是

AbpAuthorizeAttribute

,該特性适用于應用服務層,也就是實作了

IApplicationService

接口的類型所使用的。

它們兩個的内部定義基本一樣,傳入一個或者多哦個具體的權限項,以便給

IAuthorizationHelper

作驗證使用。

在 Abp 架構内部,每一個權限其實就是一個字元串,比如說使用者資料新增,是一個權限,那麼你可以直接建立一個

"Administration.UserManagement.CreateUser"

字元作為其權限項,那麼代碼示例就如下:

[AbpAuthorize("Administration.UserManagement.CreateUser")]
public void CreateUser(CreateUserInput input)
{
    // 如果使用者沒有 Administration.UserManagement.CreateUser 權限,則不會進入到本方法
}
           

下面是

AbpAuthorizeAttribute

權限特性的定義,另外一個 MVC 權限特性定義也是一樣的:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
public class AbpAuthorizeAttribute : Attribute, IAbpAuthorizeAttribute
{
	// 特性擁有的權限項集合
    public string[] Permissions { get; }
    
    // 用于确定是否需要驗證使用者是否擁有 Permission 數組内所有權限項,如果為 True 則使用者需要擁有所有權限才能夠操作接口,如果為 False 的話,使用者隻要擁有其中一個權限項則可以通過驗證,預設值為:False
    public bool RequireAllPermissions { get; set; }

    public AbpAuthorizeAttribute(params string[] permissions)
    {
        Permissions = permissions;
    }
}
           

權限特性一般都會打在你的控制器/應用服務層的類定義,或者方法之上,當你為你的 API 接口标注了權限特性,那麼目前請求的使用者沒有所需要的權限,則一律會被攔截器/過濾器阻止請求。

2.3 權限驗證

當如果使用者請求的方法或者控制器是标注了授權特性的話,都會通過

IAuthorizationHelper

進行驗證,它一共有兩個公開方法。

public interface IAuthorizationHelper
{
    // 判斷使用者是否擁有一組權限特性所标注的權限
    Task AuthorizeAsync(IEnumerable<IAbpAuthorizeAttribute> authorizeAttributes);

    // 判斷使用者是否擁有,被調用的方法所标注的權限
    Task AuthorizeAsync(MethodInfo methodInfo, Type type);
}
           

在其預設的實作當中,注入了兩個相對重要的元件,第一個是

IAbpSession

,它是 Abp 架構定義的使用者會話狀态,如果目前使用者處于登入狀态的時候,其内部必定有值,在這裡主要用于判斷使用者是否登入。

第二個則是

IPermissionChecker

,它則是用于具體的檢測邏輯,如果說

IAuthorizationHelper

是用來提供權限驗證的工具,那麼

IPermissionChecker

就是權限驗證的核心,在

IPermissionChecker

内部則是真正的對傳入的權限進行了驗證邏輯。

IPermissionChecker

本身隻有兩個方法,都傳回的

bool

值,有權限則為

true

沒有則為

false

,其接口定義如下:

// 權限檢測器
public interface IPermissionChecker
{
    // 傳入一個權限項的值,判斷目前使用者是否擁有該權限
    Task<bool> IsGrantedAsync(string permissionName);

    // 傳入一個使用者辨別,判斷該使用者是否擁有制定的權限項
    Task<bool> IsGrantedAsync(UserIdentifier user, string permissionName);
}
           

可以看到 Abp 架構本身針對于設計來說,都考慮了各個元件的可替換性與擴充性,你可以随時通過替換

IAuthorizationHelper

或者是

IPermissionChecker

的實作來達到自己想要的效果,這點值得我們在編寫代碼的時候學習。

說了這麼多,下面我們來看一下

IAuthorizationHelper

的具體實作吧:

public class AuthorizationHelper : IAuthorizationHelper, ITransientDependency
{
    public IAbpSession AbpSession { get; set; }
    public IPermissionChecker PermissionChecker { get; set; }
    public IFeatureChecker FeatureChecker { get; set; }
    public ILocalizationManager LocalizationManager { get; set; }

    private readonly IFeatureChecker _featureChecker;
    private readonly IAuthorizationConfiguration _authConfiguration;

    public AuthorizationHelper(IFeatureChecker featureChecker, IAuthorizationConfiguration authConfiguration)
    {
        _featureChecker = featureChecker;
        _authConfiguration = authConfiguration;
        AbpSession = NullAbpSession.Instance;
        PermissionChecker = NullPermissionChecker.Instance;
        LocalizationManager = NullLocalizationManager.Instance;
    }

    public virtual async Task AuthorizeAsync(IEnumerable<IAbpAuthorizeAttribute> authorizeAttributes)
    {
    	// 判斷是否啟用了授權系統,沒有啟用則直接跳過不做驗證
        if (!_authConfiguration.IsEnabled)
        {
            return;
        }

	    // 如果目前的使用者會話狀态其 SessionId 沒有值,則說明使用者沒有登入,抛出授權驗證失敗異常
        if (!AbpSession.UserId.HasValue)
        {
            throw new AbpAuthorizationException(
                LocalizationManager.GetString(AbpConsts.LocalizationSourceName, "CurrentUserDidNotLoginToTheApplication")
                );
        }

		// 周遊所有授權特性,通過 IPermissionChecker 來驗證使用者是否擁有這些特性所标注的權限
        foreach (var authorizeAttribute in authorizeAttributes)
        {
            await PermissionChecker.AuthorizeAsync(authorizeAttribute.RequireAllPermissions, authorizeAttribute.Permissions);
        }
    }

	// 授權過濾器與授權攔截器調用的方法,傳入一個方法定義與方法所在的類的類型
    public virtual async Task AuthorizeAsync(MethodInfo methodInfo, Type type)
    {
    	// 檢測産品功能
        await CheckFeatures(methodInfo, type);
        // 檢測權限
        await CheckPermissions(methodInfo, type);
    }

    protected virtual async Task CheckFeatures(MethodInfo methodInfo, Type type)
    {
        var featureAttributes = ReflectionHelper.GetAttributesOfMemberAndType<RequiresFeatureAttribute>(methodInfo, type);

        if (featureAttributes.Count <= 0)
        {
            return;
        }

        foreach (var featureAttribute in featureAttributes)
        {
        	// 檢查目前使用者是否啟用了被調用方法标注上面的功能
            await _featureChecker.CheckEnabledAsync(featureAttribute.RequiresAll, featureAttribute.Features);
        }
    }

    protected virtual async Task CheckPermissions(MethodInfo methodInfo, Type type)
    {
    	// 判斷是否啟用了授權系統,沒有啟用則直接跳過不做驗證
        if (!_authConfiguration.IsEnabled)
        {
            return;
        }

	    // 判斷方法或者控制器類上是否标注了匿名通路特性,如果标注了,不做權限驗證
        if (AllowAnonymous(methodInfo, type))
        {
            return;
        }

	    // 獲得方法和類上面定義的所有權限特性數組
        var authorizeAttributes =
            ReflectionHelper
                .GetAttributesOfMemberAndType(methodInfo, type)
                .OfType<IAbpAuthorizeAttribute>()
                .ToArray();

	    // 如果一個都不存在,跳過驗證
        if (!authorizeAttributes.Any())
        {
            return;
        }

	    // 傳入所有權限特性,調用另外一個重載方法,使用 IPermissionChecker 針對這些特性進行具體驗證
        await AuthorizeAsync(authorizeAttributes);
    }

    private static bool AllowAnonymous(MemberInfo memberInfo, Type type)
    {
        return ReflectionHelper
            .GetAttributesOfMemberAndType(memberInfo, type)
            .OfType<IAbpAllowAnonymousAttribute>()
            .Any();
    }
}
           

看完上面你似乎并沒有看到哪兒有抛出

AbpAuthorizationException

的地方,這是因為 Abp 給

IPermissionChecker

添加了一個擴充方法,叫做

AuthorizeAsync()

,看他的具體實作你就知道,它在這個擴充方法裡面才真正調用了

IPermissionChecker.IsGrantedAsync()

方法進行權限驗證。

public static async Task AuthorizeAsync(this IPermissionChecker permissionChecker, bool requireAll, params string[] permissionNames)
{
    // 這裡還是調用的一個擴充方法,其内部是周遊傳入的權限項集合,針對每一個權限進行檢測
    if (await IsGrantedAsync(permissionChecker, requireAll, permissionNames))
    {
        return;
    }

    // 這兒呢就是本地化權限的名稱,用于抛出異常的時候給前端展示用的,裡面提列了你缺少的權限項有哪些
    var localizedPermissionNames = LocalizePermissionNames(permissionChecker, permissionNames);

    if (requireAll)
    {
        throw new AbpAuthorizationException(
            string.Format(
                L(
                    permissionChecker,
                    "AllOfThesePermissionsMustBeGranted",
                    "Required permissions are not granted. All of these permissions must be granted: {0}"
                ),
                string.Join(", ", localizedPermissionNames)
            )
        );
    }
    else
    {
        throw new AbpAuthorizationException(
            string.Format(
                L(
                    permissionChecker,
                    "AtLeastOneOfThesePermissionsMustBeGranted",
                    "Required permissions are not granted. At least one of these permissions must be granted: {0}"
                ),
                string.Join(", ", localizedPermissionNames)
            )
        );
    }
}
           

如果你感覺自己快被繞暈了,也不必驚慌...因為

IPermissionChecker

本身隻能針對單個權限進行檢查,是以這裡通過擴充了

IPermissionChecker

方法,使其能夠一次檢驗一個集合而已。

3.結語

本篇文章主要解析了 Abp 架構針對權限驗證所做的基本操作,整體思路還是十分簡單的,在 Abp 基本架構沒有涉及到使用者與角色的具體權限控制,這部分的内容是存放在 Abp.Zero 子產品當中的,下一篇文章将會結合 Abp.Zero 來進行更加詳細的講解權限與功能的實作。

4.點此跳轉到總目錄

繼續閱讀