注释
本部分介绍的数据 API 生成器功能在版本 2.0 及更高版本中提供。 有关详细信息,请参阅 版本 2.0 中的新增功能。
角色继承功能允许你在一个更为广泛的角色上定义权限,而更具体的角色可以自动继承这些权限。 如果不使用角色继承,您必须针对每个实体上的每个角色重复相同的权限块。 使用角色继承时,您只需在 anonymous 上定义访问权限,所有更广泛的角色都将获得相同的访问权限。
继承链
继承链从最低特权流向最高特权:
named-role → authenticated → anonymous
| 角色 | 继承自 | 备注 |
|---|---|---|
命名角色 (例如, editor) |
authenticated |
或者,如果未配置 authenticated,则从 anonymous 开始 |
authenticated |
anonymous |
当不存在显式 authenticated 块时适用 |
anonymous |
(无) | 链的基点;无回退机制 |
链条的意思是:
- 如果命名的角色没有权限块,DAB 会查找
authenticated块。 如果不存在,它将回退到anonymous。 - 如果没有
authenticated权限块,DAB 将使用该anonymous块。 - 如果
anonymous没有权限块,请求将被403 Forbidden拒绝。
继承的解决方式
当 DAB 评估请求时,它会确定有效角色,然后遍历继承链查找权限块:
- DAB 通过
X-MS-API-ROLE标头、令牌声明或默认值标识请求的有效角色。 - DAB 在
entities.<name>.permissions中查找与有效角色匹配的显式权限块。 - 如果不存在匹配块,DAB 将沿着链向上遍历:
authenticated→anonymous。 - 找到的第一个匹配块提供请求的权限。
- 如果链中没有任何块匹配任何角色,DAB 将返回
403 Forbidden。
注释
DAB 在每个请求中仅根据一个有效角色的上下文评估权限。 角色继承不会合并来自多个角色的权限。
示例
最低配置:所有角色的单个权限
在read上定义anonymous权限。 每个角色authenticated (以及任何命名角色)都会继承该访问权限。
{
"entities": {
"Book": {
"source": "dbo.books",
"permissions": [
{ "role": "anonymous", "actions": [ "read" ] }
]
}
}
}
此配置的有效权限:
Entity: Book
Role: anonymous | Actions: Read
Role: authenticated | Actions: Read (inherited from: anonymous)
Unconfigured roles | Inherit from: anonymous
分层配置:每个角色的不同访问权限
如果需要每个角色的不同访问级别,请显式定义每个访问级别。 继承仅填充您未配置的角色。
{
"entities": {
"Order": {
"source": "dbo.orders",
"permissions": [
{ "role": "anonymous", "actions": [ "read" ] },
{ "role": "authenticated", "actions": [ "read", "create" ] },
{ "role": "admin", "actions": [ "*" ] }
]
}
}
}
此配置的有效权限:
Entity: Order
Role: anonymous | Actions: Read
Role: authenticated | Actions: Read, Create
Role: admin | Actions: Create, Read, Update, Delete
Unconfigured roles | Inherit from: authenticated
除 admin 以外的任何命名角色(例如 viewer 或 support)均从 authenticated 继承,并获得 read 和 create 的访问权限。
无继承:完全阻止
如果anonymous没有权限块,并且链中的其他角色也都没有权限块,则对该实体的每个请求都会被拒绝。
{
"entities": {
"AuditLog": {
"source": "dbo.audit_log",
"permissions": [
{ "role": "admin", "actions": [ "read" ] }
]
}
}
}
在此配置中,只能 admin 访问 AuditLog。
authenticated 和 anonymous 没有可继承的权限块,因此 DAB 会拒绝来自这些角色且包含 403 Forbidden 的请求。
重要
当某个实体上配置了 authenticated 或命名角色,但 Unauthenticated 提供程序处于活动状态时,DAB 会在启动时发出警告。 当 Unauthenticated 处于活动状态时,那些角色永远不会被激活。 有关详细信息,请参阅 配置未经身份验证的提供程序。
查看有效权限
使用 dab configure --show-effective-permissions 可显示每个实体的解析权限,包括哪些角色从哪些角色继承了权限。 此命令是验证继承是否按预期运行而不运行引擎的最快方法。
dab configure --show-effective-permissions
还可以将特定配置文件作为目标:
dab configure --show-effective-permissions --config my-config.json
示例输出:
Entity: Book
Role: anonymous | Actions: Read
Role: authenticated | Actions: Read (inherited from: anonymous)
Unconfigured roles inherit from: anonymous
Entity: Order
Role: admin | Actions: Create, Read, Update, Delete
Role: anonymous | Actions: Read
Role: authenticated | Actions: Read (inherited from: anonymous)
Unconfigured roles inherit from: authenticated
有关完整选项,请参阅 --show-effective-permissions。
继承与显式权限
| 情景 | 建议 |
|---|---|
| 所有角色都应具有相同的访问权限 | 在anonymous上定义一次;让所有角色继承 |
| 经过身份验证的用户需要比匿名更多的访问权限 | 定义 anonymous 读取,添加 authenticated 创建/更新 |
命名角色需要比authenticated拥有更广泛的访问权限 |
明确地定义命名角色,其他的则继承自 authenticated |
命名角色所需访问权限比 authenticated 少 |
使用已精简过的操作显式地定义命名角色 |
| 实体必须完全私有 | 仅授予特定命名角色,保持 authenticated 和 anonymous 未定义。 |