[Abp 源碼分析]權限驗證

點擊上方藍字關注我們


0.簡介

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

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

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

0.1 驗證流程圖

1.啓動流程

1.1 流程圖

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 來進行更加詳細的講解權限與功能的實現。


作者:myzony

出處:https://www.cnblogs.com/myzony/p/9466162.html

本文分享自微信公衆號 - 一線碼農聊技術(dotnetfly)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章