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的权限控制非常灵活,只需要改动权限控制条件就可以满足不同的权限控制,满足不同的权限要求。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值