loopback api返回错误AUTHORIZATION_REQUIRED

时间:2017-05-23 17:05:15

标签: node.js loopbackjs strongloop

我有两个型号:用户和汽车

用户acl:

 "acls": [
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "DENY"
    },
    {
      "accessType": "EXECUTE",
      "principalType": "ROLE",
      "principalId": "admin",
      "permission": "ALLOW",
      "property": "find"
    },
    {
      "accessType": "EXECUTE",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW",
      "property": "login"
    },
    {
      "accessType": "READ",
      "principalType": "ROLE",
      "principalId": "$owner",
      "permission": "ALLOW"
    },
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "admin",
      "permission": "ALLOW"
    }

car acl:

  "acls": [
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "DENY"
    },
    {
      "accessType": "EXECUTE",
      "principalType": "ROLE",
      "principalId": "$owner",
      "permission": "ALLOW",
      "property": "find"
    },
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$owner",
      "permission": "ALLOW"
    },
    {
      "accessType": "READ",
      "principalType": "ROLE",
      "principalId": "$owner",
      "permission": "ALLOW"
    },
    {
      "accessType": "EXECUTE",
      "principalType": "ROLE",
      "principalId": "$owner",
      "permission": "ALLOW"
    },
    {
      "accessType": "WRITE",
      "principalType": "ROLE",
      "principalId": "$owner",
      "permission": "ALLOW"
    },
    {
      "accessType": "WRITE",
      "principalType": "ROLE",
      "principalId": "$authenticated",
      "permission": "ALLOW"
    }

我可以使用api /cars添加汽车但无法获取使用api /cars的汽车列表,在获取它时返回AUTHORIZATION_REQUIRED错误代码。

注意:我以用户身份登录,然后我正在尝试获取用户拥有的汽车列表。

1 个答案:

答案 0 :(得分:0)

您可能遗漏了与Loopback documentation一致的模型关系:

  

要限定$ owner,目标模型需要具有belongsTo关系   到User模型(或模型从User扩展)和属性匹配   目标模型实例的外键。 $ owner的检查是   仅针对路径上具有':id'的远程方法执行   例如,GET / api / users /:id。

此外,我认为将default setting aclErrorStatus 401 更改为 403 是一个好主意。这样就可以更容易理解身份验证授权是否存在问题。