我正在为公司开发内部管理系统。 某些API路由将检查用户的角色,其余路由将检查用户的权限。
目前,我该怎么做将用户的权限存储在JWT令牌中
{
"user": {
"name": "Oyster Lee",
"role": "root",
"image": ""
},
"OMS": 2147483647,
"WMS": 4095,
"iat": 1566536007,
"exp": 1567140807,
"iss": "Test"
}
我的权限是使用按位运算符。 但是每个系统最多只能使用31种权限。 我有31个以上,因此将需要替换按位运算符。
除此之外,在为用户分配新权限或角色后,他必须注销并一次又一次登录。
我在想我是否应该在每次向路由发送请求时检查数据库的用户许可。 会导致应用程序变重吗? 有什么利弊吗? 顺便说一句,我是MySQL作为我们的数据库。
前端还需要根据用户的许可或角色有条件地进行渲染。 我正在使用Nuxt.js SPA作为前端。
您正在构建的系统存在多个问题:
My permission is using a bitwise operator. But it can only use up to 31 types of permission in each system. I have more than 31 so the bitwise operator will need to be replaced.
对自己的逻辑进行硬编码My permission is using a bitwise operator. But it can only use up to 31 types of permission in each system. I have more than 31 so the bitwise operator will need to be replaced.
My permission is using a bitwise operator. But it can only use up to 31 types of permission in each system. I have more than 31 so the bitwise operator will need to be replaced.
那只是无法扩展 您需要的是从内部管理系统API分离和外部化授权逻辑。 有一种称为基于属性的访问控制(ABAC)可以实现此目的的模式。 使用ABAC,您可以使用基于拦截的模型, 策略执行点 (PEP)会拦截对您API的调用,并根据策略决策点 (PDP)检查是否应允许调用方(Alice)访问他们所访问的内容请求(文档,记录...)。
PDP配置有使用属性描述可能发生或无法发生的事件的策略。 例如:
编写此类策略有两个标准: alfa和xacml 。 ABAC体系结构有几种实现,包括开源(AuthZForce,AT&T ...)和商业(Axiomatics)。 请检查一下。 我也写在这个问题在其他职位SO很多,如这一个 。