环回 api 错误 401 需要授权

Loopback api errors 401 Authorization Required

我正在使用后端 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"
  }
}

这是错误的屏幕截图。

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

  2. 您必须为 "User Model" 管理 ACL。 参考:https://loopback.io/doc/en/lb2/Controlling-data-access.html

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