RBAC vs ABAC
RBAC(Role-based Access Control)
基于角色的權(quán)限控制
其基本思想是,對(duì)系統(tǒng)操作的各種權(quán)限不是直接授予具體的用戶,而是在用戶集合與權(quán)限集合之間建立一個(gè)角色集合。每一種角色對(duì)應(yīng)一組相應(yīng)的權(quán)限。一旦用戶被分配了適當(dāng)?shù)慕巧?#xff0c;該用戶就擁有此角色的所有操作權(quán)限。這樣做的好處是,不必在每次創(chuàng)建用戶時(shí)都進(jìn)行分配權(quán)限的操作,只要分配用戶相應(yīng)的角色即可,而且角色的權(quán)限變更比用戶的權(quán)限變更要少得多,這樣將簡(jiǎn)化用戶的權(quán)限管理,減少系統(tǒng)的開銷。(來源于百科)
比方說給用戶分管理員和普通用戶,這樣不同用戶可以訪問的頁面可以設(shè)置成不一樣。
在identityServer中增加roles選項(xiàng)
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中增加對(duì)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上可以配置不同的權(quán)限,
[Authorize(Roles = "管理員")]//[Authorize(Policy = "SmithInSomeWhere")]public async Task<IActionResult> About(){綜上,可以看出這樣的權(quán)限配置比較單一,不夠靈活,如果每個(gè)user都賦權(quán)限的話,需要將role都增加上,修改困難。而且類似是根據(jù)崗位進(jìn)行劃分,比如供應(yīng)鏈崗位、財(cái)務(wù)崗等。
ABAC(Attribute-based Access Control)
基于屬性的權(quán)限驗(yàn)證控制
ABAC則是通過動(dòng)態(tài)將一個(gè)或一組屬性來判斷是否滿足設(shè)定的條件來進(jìn)行授權(quán)判斷。屬性通常來說分為四類:用戶屬性(如用戶年齡),環(huán)境屬性(如當(dāng)前時(shí)間),操作屬性(如讀取)和對(duì)象屬性(如一篇文章,又稱資源屬性),可以看出配置非常靈活,能夠滿足幾乎所有需求。
增加條件
處理
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;}}}注冊(cè)權(quán)限條件
services.AddAuthorization(options =>{options.AddPolicy("SmithInSomeWhere", builder =>{builder.AddRequirements(new SmithRequirementAutherzation());});});services.AddSingleton<IAuthorizationHandler, SmithAuthHandler>();使用權(quán)限控制
[Authorize(Policy = "SmithInSomeWhere")]public IActionResult Index(){var role = User.IsInRole("管理員");return View();}結(jié)論
綜上可以看出,ABAC的權(quán)限控制非常靈活,只需要改動(dòng)權(quán)限控制條件就可以滿足不同的權(quán)限控制,滿足不同的權(quán)限要求。
總結(jié)
以上是生活随笔為你收集整理的RBAC vs ABAC的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ACL / RBAC/ABAC
- 下一篇: 填数字游戏ABCD*E=DCBA