gpt4 book ai didi

mysql - Loopback api 错误 401 需要授权

转载 作者:行者123 更新时间:2023-11-29 15:44:25 28 4
gpt4 key购买 nike

我正在使用后端 mysql 创建环回项目。我的服务器在 localhost:3000 上正确运行,但是当我向 localhost/api/Users 发出请求时,我收到以下错误。

Error
401 Authorization Required
code: AUTHORIZATION_REQUIRED
Error: Authorization Required
at C:\Users\Khundokar Nirjor\Desktop\Loopback\Loopbackshoppingcart\node_modules\loopback\lib\application.js:433:21
at C:\Users\Khundokar Nirjor\Desktop\Loopback\Loopbackshoppingcart\node_modules\loopback\lib\model.js:359:7
at C:\Users\Khundokar Nirjor\Desktop\Loopback\Loopbackshoppingcart\node_modules\loopback\common\models\acl.js:536:16
at C:\Users\Khundokar Nirjor\Desktop\Loopback\Loopbackshoppingcart\node_modules\async\dist\async.js:3888:9
at C:\Users\Khundokar Nirjor\Desktop\Loopback\Loopbackshoppingcart\node_modules\async\dist\async.js:473:16
at iteratorCallback (C:\Users\Khundokar Nirjor\Desktop\Loopback\Loopbackshoppingcart\node_modules\async\dist\async.js:1064:13)
at C:\Users\Khundokar Nirjor\Desktop\Loopback\Loopbackshoppingcart\node_modules\async\dist\async.js:969:16
at C:\Users\Khundokar Nirjor\Desktop\Loopback\Loopbackshoppingcart\node_modules\async\dist\async.js:3885:13
at C:\Users\Khundokar Nirjor\Desktop\Loopback\Loopbackshoppingcart\node_modules\loopback\common\models\acl.js:518:17
at C:\Users\Khundokar Nirjor\Desktop\Loopback\Loopbackshoppingcart\node_modules\loopback\common\models\role.js:447:21
at processTicksAndRejections (internal/process/task_queues.js:75:11)

这是我的 package.json 代码。

{
"name": "Loopbackshoppingcart",
"version": "1.0.0",
"main": "server/server.js",
"engines": {
"node": ">=6"
},
"scripts": {
"lint": "eslint .",
"start": "node .",
"posttest": "npm run lint && npm audit"
},
"dependencies": {
"compression": "^1.0.3",
"cors": "^2.5.2",
"helmet": "^3.10.0",
"loopback-boot": "^2.6.5",
"serve-favicon": "^2.0.1",
"strong-error-handler": "^3.0.0",
"loopback-component-explorer": "^6.2.0",
"loopback": "^3.22.0"
},
"devDependencies": {
"eslint": "^3.17.1",
"eslint-config-loopback": "^8.0.0"
},
"repository": {
"type": "",
"url": ""
},
"license": "UNLICENSED",
"description": "Loopbackshoppingcart"
}

这是 middleware.json 的代码

{
"initial:before": {
"loopback#favicon": {}
},
"initial": {
"compression": {},
"cors": {
"params": {
"origin": true,
"credentials": true,
"maxAge": 86400
}
},
"helmet#xssFilter": {},
"helmet#frameguard": {
"params": {
"action": "deny"
}
},
"helmet#hsts": {
"params": {
"maxAge": 0,
"includeSubdomains": true
}
},
"helmet#hidePoweredBy": {},
"helmet#ieNoOpen": {},
"helmet#noSniff": {},
"helmet#noCache": {
"enabled": false
}
},
"session": {},
"auth": {},
"parse": {},
"routes": {
"loopback#rest": {
"paths": [
"${restApiRoot}"
]
}
},
"files": {
"loopback#static": {
"params": "$!../client"
}
},
"final": {
"loopback#urlNotFound": {}
},
"final:after": {
"strong-error-handler": {}
}
}

这是 datasourec.json 的代码

{
"db": {
"name": "db",
"connector": "memory"
}
}

这是错误的屏幕截图。 enter image description here

最佳答案

  1. 首先,您必须将应用连接到 SQL 数据源。 引用:https://loopback.io/doc/en/lb2/Connecting-models-to-data-sources.html

  2. 您必须管理“用户模型”的 ACL。 引用:https://loopback.io/doc/en/lb2/Controlling-data-access.html

  3. 然后,您可以根据为用户模型定义的 ACL 创建并获取列出的用户。

关于mysql - Loopback api 错误 401 需要授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57228098/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com