一分钟讲清楚 Authentication 和 Authorization

我们在看文档经常会遇到这两个单词,但是很多人对这两个单词的理解有些模糊,所以我今天给大家简单介绍一下,以后读文档会更舒服一些。

Authentication 一般指身份验证,又称“验证”、“鉴权”,是指通过一定的手段,完成对用户身份的确认。
Authorization 一般是授权、委托的意思,向…授予职权或权力许可,批准等意思。

所以你可以看一下 Laravel 的文档

  1. Authentication ,主要讲的是和登录相关的内容。
  2. Authorization ,主要讲的是权限相关的内容(注意,Gates 和 Policies 都必须是在用户登录后才有意义,如果没有登录态,直接返回 false)。

讲完这个在说一说对应的 Http Status Code:

  1. Authentication:未登录的时候调用需要登录的接口,一般使用 401 Unauthorized。
  2. Authorization:登录后请求没有权限的接口,一般使用 403 Forbidden。

那么肯定有同学问了,为什么 401 是 Unauthorized 而不是 Unautheticated 呢?这不是坑人吗?事实上,参考 RFC,好像确实是弄错了。

我贴一篇解释,秒懂:原文链接

There's a problem with 401 Unauthorized, the HTTP status code for authentication errors. And that’s just it: it’s for authentication, not authorization. Receiving a 401 response is the server telling you, “you aren’t authenticated–either not authenticated at all or authenticated incorrectly–but please reauthenticate and try again.” To help you out, it will always include a WWW-Authenticate header that describes how to authenticate.

This is a response generally returned by your web server, not your web application.

It’s also something very temporary; the server is asking you to try again.

So, for authorization I use the 403 Forbidden response. It’s permanent, it’s tied to my application logic, and it’s a more concrete response than a 401.

Receiving a 403 response is the server telling you, “I’m sorry. I know who you are–I believe who you say you are–but you just don’t have permission to access this resource. Maybe if you ask the system administrator nicely, you’ll get permission. But please don’t bother me again until your predicament changes.”

In summary, a 401 Unauthorized response should be used for missing or bad authentication, and a 403 Forbidden response should be used afterwards, when the user is authenticated but isn’t authorized to perform the requested operation on the given resource.

原文链接:https://www.lijinma.com/blog/2017/02/17/au...

本作品采用《CC 协议》,转载必须注明作者和本文链接
写文字大部分时候是因为我希望能帮助到你,小部分时候是想做总结或做记录。我的微信是 lijinma,希望和你交朋友。 以下是我的公众账号,会分享我的学习和成长。
本帖由系统于 7年前 自动加精
lijinma
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 13
lijinma

咋没人给点反馈 - 。 -

7年前 评论

@lijinma 可能大家觉得 so easy 吧:)

7年前 评论

登陆 -> 登录 :walking:

7年前 评论
lijinma

@eddy8 恩恩,确实是,但是我之前没太理解 401 叫 Unauthorized,之后查了才了解。

7年前 评论
lijinma

@Hanccc 多谢提醒,改了。

7年前 评论

Authentication 和 Authorization 倒不容易搞错, 大不了查下翻译
倒是 Unauthorized /Unautheticated 这个确实涨知识了.

7年前 评论
lijinma
7年前 评论
lijinma
7年前 评论
Destiny

@lx1036 老哥,你这解释亮了。。。

7年前 评论
lijinma

@MrJing 嘿嘿

7年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!