你好,我是回环的新手,但是我仍然坚持创建和使用角色。因此,我基本上要做的是创建2个角色,并基于这些角色,我希望限制某些用户访问某些资源问题是,每次尝试从api get
获得一些信息时,我都会得到此信息
{
"error": {
"statusCode": 401,
"name": "Error",
"message": "Authorization Required",
"code": "AUTHORIZATION_REQUIRED",
"stack": "Error: Authorization Required\n at C:\\Users\\HP\\Desktop\\battle-horse\\battle-horse\\node_modules\\loopback\\lib\\application.js:433:21\n at C:\\Users\\HP\\Desktop\\battle-horse\\battle-horse\\node_modules\\loopback\\lib\\model.js:359:7\n at C:\\Users\\HP\\Desktop\\battle-horse\\battle-horse\\node_modules\\loopback\\common\\models\\acl.js:536:16\n at C:\\Users\\HP\\Desktop\\battle-horse\\battle-horse\\node_modules\\async\\dist\\async.js:3888:9\n at C:\\Users\\HP\\Desktop\\battle-horse\\battle-horse\\node_modules\\async\\dist\\async.js:473:16\n at iteratorCallback (C:\\Users\\HP\\Desktop\\battle-horse\\battle-horse\\node_modules\\async\\dist\\async.js:1064:13)\n at C:\\Users\\HP\\Desktop\\battle-horse\\battle-horse\\node_modules\\async\\dist\\async.js:969:16\n at C:\\Users\\HP\\Desktop\\battle-horse\\battle-horse\\node_modules\\async\\dist\\async.js:3885:13\n at C:\\Users\\HP\\Desktop\\battle-horse\\battle-horse\\node_modules\\loopback\\common\\models\\acl.js:518:17\n at C:\\Users\\HP\\Desktop\\battle-horse\\battle-horse\\node_modules\\loopback\\common\\models\\role.js:447:21\n at _combinedTickCallback (internal/process/next_tick.js:131:7)\n at process._tickCallback (internal/process/next_tick.js:180:9)"
}
}
在我的应用程序中,我有2个模型:
1.Client (which extends build in User Model) and has role ```bs_client```
2.Admin(which also extends the build in User Model)
请注意,这些模型是使用回送cli创建的,尚未创建任何关系。
lb model
我正在使用Mongodb作为数据库,这是我的数据源文件
"mongodb": {
"host": "",
"port": 0,
"url": "mongodb+srv://general:234234@#/#@##@?retryWrites=true&w=majority",
"database": "database",
"password": "password",
"name": "mongodb",
"user": "general",
"useNewUrlParser": true,
"includeSubDomains": true,
"useUnifiedTopology": true,
"connector": "mongodb"
}
似乎数据已正确添加到我的集合中(Role
,Rolemapping
,Client
和Access Token
)。
我正在使用此创建时动态地将角色分配给每个客户端
Client.observe('after save', function setRole(ctx, next) {
if (ctx.instance) {
if (ctx.isNewInstance) {
// look up role based on type
//
app.models.Role.find({where: {name: 'bs_client'}}, function(err, role) {
if (err) { return console.log(err); }
if (role) {
app.models.RoleMapping.create({
principalType: app.models.RoleMapping.User,
principalId: ctx.instance.id,
roleId: role.id,
}, function(err, roleMapping) {
if (err) { return console.log(err); }
console.log('User assigned RoleID ' + role.id + ' (' + ctx.instance.type + ')');
});
};
});
}
} next();
});
这是我的model-config.json
{
"_meta": {
"sources": [
"loopback/common/models",
"loopback/server/models",
"../common/models",
"./models"
],
"mixins": [
"loopback/common/mixins",
"loopback/server/mixins",
"../common/mixins",
"./mixins"
]
},
"User": {
"dataSource": "mongodb",
"public": false
},
"AccessToken": {
"dataSource": "mongodb",
"public": false
},
"ACL": {
"dataSource": "mongodb",
"public": false
},
"RoleMapping": {
"dataSource": "mongodb",
"public": true,
"options": {
"strictObjectIDCoercion": true
}
},
"Role": {
"dataSource": "mongodb",
"public": true
},
"Email": {
"dataSource": "Email"
},
"Client": {
"dataSource": "mongodb",
"public": true
},
}
和client.json
中>
"acls": [ { "accessType": "*", "principalType": "CLIENT", "principalId": "bs_client", "permission": "DENY" }, { "accessType": "READ", "principalType": "CLIENT", "principalId": "bs_client", "permission": "ALLOW" }, { "accessType": "EXECUTE", "principalType": "CLIENT", "principalId": "$authenticated", "permission": "ALLOW", "property": "create" }, { "accessType": "WRITE", "principalType": "CLIENT", "principalId": "bs_client", "permission": "ALLOW" } ],
跟随https://loopback.io/doc/en/lb3/Model-property-reference.html,一切都应该正常工作,为什么我无法使用上述配置来检索“客户端”。
提前感谢。
[您好,我是回环的新手,但是我仍然坚持创建和使用角色。因此,我基本上要做的是创建2个角色,并基于这些角色,我想限制某些用户访问某些。 ..
此行在“ acls”:“ principalType”:“ ROLE”,]中到处都应该是这样。
示例ACL: