您的位置:首页 >聚焦 >

.NET 云原生架构师训练营(权限系统 系统演示 EntityAccess)--学习笔记

2022-02-27 12:49:36    来源:程序员客栈

▲点击上方“DotNet NB”关注公众号

回复“1”获取开发者路线图

学习分享丨作者/郑子铭

这是DotNetNB公众号的第195篇原创文章

目录

模块拆分

EntityAccess

模块拆分EntityAccess

实体权限

属性权限

实体权限

创建 student

https://localhost:7018/Student/dotnetnb2

获取 student

https://localhost:7018/Student

对 student 的 permission 做一个保护,创建一个 entitiy 的 permission,create 为 true,delete 和 update 为 false

https://localhost:7018/Permission/entity

参数

{ "key": "student-entity-create", "group": "ApplicationDbContext", "displayName": "student-entity-create", "description": "新增权限", "resources": [ "DotNetNB.WebApplication.Models.Student" ], "data": { "entityName": "DotNetNB.WebApplication.Models.Student", "create": true, "delete": false, "members": [ { "memberName": "Age", "update": false } ] }}

创建之后获取一下 permission,可以看到创建的对于 student-entity-create 的一个 action 的 permission

https://localhost:7018/Permission

创建完 permission 之后再创建 student 就会抛出 AuthenticationException 异常,未认证

https://localhost:7018/Student/dotnetnb2

这是通过 dbcontext 在 SavingChangesAsync 的时候由 Intersect 实现的,因为访问接口的时候没有带上 token

if (!createPermission.Intersect(claimValues).Any()) throw new AuthorizationException();

获取 token

https://localhost:7018/Authentication/login

接着把 token 放到创建 student 请求的 headers 之后,会抛出 AuthorizationException 异常,未授权

https://localhost:7018/Student/dotnetnb2

说明已经登录成功,完成认证,但是没有相关权限,因为 token 中没有包含 student-entity-create 的 permission

为用户添加 permission,因为 admin 用户拥有一个 admin 的 role,所以只需要将 permission 添加给 admin 的 role 即可

https://localhost:7018/Permission/addtorole?role=admin&permission=student-entity-create

permission 添加之后需要重新获取 token

https://localhost:7018/Authentication/login

解析 token 可以看到包含 student-entity-create 的 permission

使用这个 token 创建 student 就可以创建成功

https://localhost:7018/Student/dotnetnb2

获取所有 student,可以看到多了一个 dotnetnb2

https://localhost:7018/Student

但是这个 token 没有给 delete 的权限,所以 delete 会抛出 AuthorizationException 异常,未授权

https://localhost:7018/Student/dotnetnb2

因为之前的 permission 中 delete 为 false

"delete": false,

需要再添加一个包含 delete 权限的 permission:student-entity-create-and-delete

https://localhost:7018/Permission/entity

参数

{ "key": "student-entity-create-and-delete", "group": "ApplicationDbContext", "displayName": "student-entity-create-and-delete", "description": "新增删除权限", "resources": [ "DotNetNB.WebApplication.Models.Student" ], "data": { "entityName": "DotNetNB.WebApplication.Models.Student", "create": true, "delete": true, "members": [ { "memberName": "Age", "update": false } ] }}

创建之后获取一下 permission,可以看到 student-entity-create-and-delete 的 permission

https://localhost:7018/Permission

直接授权这个 permission 给用户 admin

https://localhost:7018/Permission/addtouser?username=admin&permission=student-entity-create-and-delete

重新获取 token

https://localhost:7018/Authentication/login

解析 token 看到包含 student-entity-create-and-delete

使用这个 token 删除 student 就可以成功

https://localhost:7018/Student/dotnetnb2

获取 student,可以发现 dotnetnb2 已经删除成功了

https://localhost:7018/Student属性权限

调用接口修改 student 的 age 属性,会抛出 AuthenticationException 异常,未认证

https://localhost:7018/Student/addAge/dotnetnb

登录之后使用 token 请求,会抛出 AuthorizationException 异常,未授权

https://localhost:7018/Student/addAge/dotnetnb

因为之前添加的 permission 对 age 属性不可修改

"members": [ { "memberName": "Age", "update": false }

需要新增一个可以修改 age 属性的 permission

https://localhost:7018/Permission/entity

参数

{ "key": "student-entity-all", "group": "ApplicationDbContext", "displayName": "student-entity-all", "description": "全部权限", "resources": [ "DotNetNB.WebApplication.Models.Student" ], "data": { "entityName": "DotNetNB.WebApplication.Models.Student", "create": true, "delete": true, "members": [ { "memberName": "Age", "update": true } ] }}

将该 permission 赋值给 admin,重新获取 token,再调用接口修改 student 的 age 属性

https://localhost:7018/Student/addAge/dotnetnb

修改成功后再获取 student,可以看到 dotnetnb 的 age 从 0 变为 1,修改成功

https://localhost:7018/StudentGitHub源码链接:

https://github.com/MingsonZheng/dotnetnb.securityrefactor 分支

课程链接

.NET云原生架构师训练营讲什么,怎么讲,讲多久

推荐阅读:《Kubernetes全栈架构师(Kubeadm高可用安装k8s集群)--学习笔记》《.NET 云原生架构师训练营(模块一 架构师与云原生)--学习笔记》《.NET Core开发实战(第1课:课程介绍)--学习笔记》

点击下方卡片关注DotNet NB

一起交流学习

▲点击上方卡片关注DotNet NB,一起交流学习

请在公众号后台

回复【路线图】获取.NET 2021开发者路线图回复【原创内容】获取公众号原创内容回复【峰会视频】获取.NET Conf开发者大会视频回复【个人简介】获取作者个人简介回复【年终总结】获取作者年终总结回复【加群】加入DotNet NB交流学习群长按识别下方二维码,或点击阅读原文。和我一起,交流学习,分享心得。

关键词: 年终总结 删除权限 和我一起

相关阅读