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

目录

  • 模块拆分
  • 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/Student

GitHub源码链接:

GitHub - MingsonZheng/dotnetnb.security: 无代码埋点通用权限管理系统 refactor 分支

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值