如何在 Angular 中使用 Node-Casbin?
How to use Node-Casbin in Angular?
有关于 Node-Casbin 库的问题。我想在前端 Angular 中使用这个库。不幸的是,我不知道我该怎么做。我想将这个库与字符串一起使用。所以我想将策略和数据声明为字符串 - 而不是文件。
- 已用 Angular: 10.0.0
- 使用的 Casbin 版本:5.1.6
我对编码所做的一切:
在 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');
}
}
但我仍然收到来自 Angular 的错误,例如:
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 - 但我仍然收到错误消息。
你能帮我吗?
非常感谢!
你不应该在客户端包含这样的逻辑。除此之外,你在浏览器中没有文件系统(库似乎依赖于它),在前端包含这些东西将是一个高安全性问题。
如果您确实需要将此类功能包含到您的应用程序中,则应仅将其放在后端。
有关于 Node-Casbin 库的问题。我想在前端 Angular 中使用这个库。不幸的是,我不知道我该怎么做。我想将这个库与字符串一起使用。所以我想将策略和数据声明为字符串 - 而不是文件。
- 已用 Angular: 10.0.0
- 使用的 Casbin 版本:5.1.6
我对编码所做的一切: 在 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');
}
}
但我仍然收到来自 Angular 的错误,例如:
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 - 但我仍然收到错误消息。
你能帮我吗? 非常感谢!
你不应该在客户端包含这样的逻辑。除此之外,你在浏览器中没有文件系统(库似乎依赖于它),在前端包含这些东西将是一个高安全性问题。
如果您确实需要将此类功能包含到您的应用程序中,则应仅将其放在后端。