有个关于Node-Casbin图书馆的问题。我想使用这个角度的库,在前端。不幸的是,我不知道该怎么做。我想用这个带字符串的库。因此,我想将策略和数据声明为字符串,而不是文件。
我对编码所做的一切:在package.json中,我添加了以下代码:
"browser": {
"fs": false,
"path": false,
"os": false
},我在服务中添加了代码,比如:
async loadEnforcer() {
const enforcer = (await new Enforcer().initWithString(
this.policy,
this.dataSet
)) as any;
const sub = 'alice'; // the user that wants to access a resource.
const obj = '/alice_data/'; // the resource that is going to be accessed.
const act = 'GET'; // the operation that the user performs on the resource.
console.log(enforcer);
console.log(
'the user permission is : ' + enforcer.getPermissionsForUser('alice')
);
if (enforcer.enforce(sub, obj, act) === true) {
console.log('permit alice to read data1');
} else {
console.log('deny the request, show an error');
}
}但我还是从角度上收到了一个错误,比如:
core.js:4197 ERROR Error: Uncaught (in promise): TypeError: fs_1.readFileSync is not a function
TypeError: fs_1.readFileSync is not a function
at Config.parse (config.js:64)
at Function.newConfig (config.js:29)
at Model.loadModel (model.js:109)
at Object.newModel (model.js:291)
at Enforcer.<anonymous> (enforcer.js:62)
at Generator.next (<anonymous>)
at enforcer.js:21
at new ZoneAwarePromise (zone-evergreen.js:960)
at push.../../node_modules/casbin/lib/enforcer.js.__awaiter (enforcer.js:17)
at Enforcer.initWithAdapter (enforcer.js:61)
at resolvePromise (zone-evergreen.js:798)
at zone-evergreen.js:705
at rejected (tslib.es6.js:72)
at ZoneDelegate.invoke (zone-evergreen.js:364)
at Object.onInvoke (core.js:27437)
at ZoneDelegate.invoke (zone-evergreen.js:363)
at Zone.run (zone-evergreen.js:123)
at zone-evergreen.js:857
at ZoneDelegate.invokeTask (zone-evergreen.js:399)
at Object.onInvokeTask (core.js:27425)我试着用和https://github.com/kowthalganesh/casbin-angular/blob/master/src/app/app.component.ts一样的方式去做--但是我仍然收到了一个错误。
你能帮我吗?非常感谢!
发布于 2020-10-08 09:10:29
您不应该在客户端包含这样的逻辑。此外,浏览器中没有文件系统( libary似乎依赖于此),将此类内容包含在前端将是一个高度安全的问题。
如果您确实需要将这些特性包含到应用程序中,则应该只将其放在后端。
https://stackoverflow.com/questions/64259071
复制相似问题