日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

RBAC vs ABAC

發布時間:2024/3/12 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 RBAC vs ABAC 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

RBAC(Role-based Access Control)

基于角色的權限控制

其基本思想是,對系統操作的各種權限不是直接授予具體的用戶,而是在用戶集合與權限集合之間建立一個角色集合。每一種角色對應一組相應的權限。一旦用戶被分配了適當的角色后,該用戶就擁有此角色的所有操作權限。這樣做的好處是,不必在每次創建用戶時都進行分配權限的操作,只要分配用戶相應的角色即可,而且角色的權限變更比用戶的權限變更要少得多,這樣將簡化用戶的權限管理,減少系統的開銷。(來源于百科)

比方說給用戶分管理員和普通用戶,這樣不同用戶可以訪問的頁面可以設置成不一樣。
在identityServer中增加roles選項

public static IEnumerable<IdentityResource> GetIdentityResources(){return new IdentityResource[]{new IdentityResources.OpenId(),new IdentityResources.Profile(),new IdentityResource("roles","角色",new List<string>{JwtClaimTypes.Role})};}

client的可以查看的范圍也增加上roles

AllowedScopes = new List<string>{IdentityServerConstants.StandardScopes.OpenId,IdentityServerConstants.StandardScopes.Profile,"api1","roles",

user中增加role角色屬性

public static List<TestUser> Users = new List<TestUser>{new TestUser{SubjectId = "818727", Username = "alice", Password = "alice", Claims = {new Claim(JwtClaimTypes.Name, "Alice Smith"),new Claim(JwtClaimTypes.GivenName, "Alice"),new Claim(JwtClaimTypes.FamilyName, "Smith"),new Claim(JwtClaimTypes.Email, "AliceSmith@email.com"),new Claim(JwtClaimTypes.EmailVerified, "true", ClaimValueTypes.Boolean),new Claim(JwtClaimTypes.WebSite, "http://alice.com"),new Claim(JwtClaimTypes.Address, @"{ 'street_address': 'One Hacker Way', 'locality': 'Heidelberg', 'postal_code': 69118, 'country': 'Germany' }", IdentityServer4.IdentityServerConstants.ClaimValueTypes.Json),new Claim(JwtClaimTypes.Role,"管理員")}},new TestUser{SubjectId = "88421113", Username = "bob", Password = "bob", Claims = {new Claim(JwtClaimTypes.Name, "Bob Smith"),new Claim(JwtClaimTypes.GivenName, "Bob"),new Claim(JwtClaimTypes.FamilyName, "Smith"),new Claim(JwtClaimTypes.Email, "BobSmith@email.com"),new Claim(JwtClaimTypes.EmailVerified, "true", ClaimValueTypes.Boolean),new Claim(JwtClaimTypes.WebSite, "http://bob.com"),new Claim(JwtClaimTypes.Address, @"{ 'street_address': 'One Hacker Way', 'locality': 'Heidelberg', 'postal_code': 69118, 'country': 'Germany' }", IdentityServer4.IdentityServerConstants.ClaimValueTypes.Json),new Claim("location", "somewhere"),new Claim(JwtClaimTypes.Role,"普通用戶")}}

mvc client中增加對role的獲取

options.Scope.Clear();options.Scope.Add("api1");//options.Scope.Add("offline_access");options.Scope.Add("roles");

還有,需要將jwt中的role映射到aspnetcore中的claim

options.TokenValidationParameters = new TokenValidationParameters{NameClaimType = JwtClaimTypes.Name,RoleClaimType = JwtClaimTypes.Role};

這樣在action上可以配置不同的權限,

[Authorize(Roles = "管理員")]//[Authorize(Policy = "SmithInSomeWhere")]public async Task<IActionResult> About(){

綜上,可以看出這樣的權限配置比較單一,不夠靈活,如果每個user都賦權限的話,需要將role都增加上,修改困難。而且類似是根據崗位進行劃分,比如供應鏈崗位、財務崗等。

ABAC(Attribute-based Access Control)

基于屬性的權限驗證控制
ABAC則是通過動態將一個或一組屬性來判斷是否滿足設定的條件來進行授權判斷。屬性通常來說分為四類:用戶屬性(如用戶年齡),環境屬性(如當前時間),操作屬性(如讀取)和對象屬性(如一篇文章,又稱資源屬性),可以看出配置非常靈活,能夠滿足幾乎所有需求。
增加條件

public class SmithRequirementAutherzation:IAuthorizationRequirement{public SmithRequirementAutherzation(){}}

處理

public class SmithAuthHandler: AuthorizationHandler<SmithRequirementAutherzation>{protected override Task HandleRequirementAsync(AuthorizationHandlerContext context,SmithRequirementAutherzation requirement){var familyName = context.User.Claims.FirstOrDefault(c => c.Type == JwtClaimTypes.FamilyName)?.Value;var location = context.User.Claims.FirstOrDefault((c => c.Type == "location"))?.Value;if (familyName == "Smith" && location == "somewhere"){context.Succeed(requirement);return Task.CompletedTask;}else{context.Fail();return Task.CompletedTask;}}}

注冊權限條件

services.AddAuthorization(options =>{options.AddPolicy("SmithInSomeWhere", builder =>{builder.AddRequirements(new SmithRequirementAutherzation());});});services.AddSingleton<IAuthorizationHandler, SmithAuthHandler>();

使用權限控制

[Authorize(Policy = "SmithInSomeWhere")]public IActionResult Index(){var role = User.IsInRole("管理員");return View();}

結論

綜上可以看出,ABAC的權限控制非常靈活,只需要改動權限控制條件就可以滿足不同的權限控制,滿足不同的權限要求。

總結

以上是生活随笔為你收集整理的RBAC vs ABAC的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。