知识卡片
认证失败与授权失败分开
受保护接口先要确认调用者身份,再判断其权限。未认证就访问时,书中以 401 提示先认证;身份已确认但无权访问资源或操作时,以 403 表示禁止。分开返回让客户端知道问题在身份凭证还是资源权限。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
原书出处
参考来源
- 位置:《Microservice APIs》第 4 章《Principles of REST API design》(源文件:_epub-src/OEBPS/Text/04.htm)
- 结论依据:原文依次说明未认证请求应返回 401、已认证但无权限请求应返回 403,并用查看他人订单举例。
- 原始内容:The first happens when a client sends an unauthenticated request to a protected endpoint. In that case, we must tell them that they should first authenticate. ... The second error happens when a user is correctly authenticated and tries to use an endpoint or a resource they are not authorized to access. ... we address this scenario with the 403 (Forbidden) status code