Skip to content

Commit 41fdc30

Browse files
committed
Update SpringInterviewQuestions.md
1 parent 5c4413a commit 41fdc30

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

docs/system-design/framework/spring/SpringInterviewQuestions.md

+24-9
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ String transient4; // not persistent because of @Transient
334334

335335
## 10. Spring Security
336336

337-
### 10.1 认证 (Authentication) 和授权 (Authorization)
337+
### 10.1 区分认证 (Authentication) 和授权 (Authorization)
338338

339339
这是一个绝大多数人都会混淆的问题。首先先从读音上来认识这两个名词,很多人都会把它俩的读音搞混,所以我建议你先先去查一查这两个单词到底该怎么读,他们的具体含义是什么。
340340

@@ -374,13 +374,25 @@ Cookie 和 Session都是用来跟踪浏览器用户身份的会话方式,但
374374

375375
Cookie 数据保存在客户端(浏览器端),Session 数据保存在服务器端。相对来说 Session 安全性更高。如果使用 Cookie 的一些敏感信息不要写入 Cookie 中,最好能将 Cookie 信息加密然后使用到的时候再去服务器端解密。
376376

377-
### 10.3 什么是JWT?
377+
### 10.3 什么是 Token?什么是 JWT?
378378

379379
我们在上一个问题中探讨了使用 Session 来鉴别用户的身份,并且给出了几个 Spring Session 的案例分享。 我们知道 Session 信息需要保存一份在服务器端。这种方式会带来一些麻烦,比如需要我们保证保存 Session 信息服务器的可用性、不适合移动端(依赖Cookie)等等。
380380

381-
有没有一种不需要自己存放 Session 信息就能实现身份验证的方式呢?当然!JWT (JSON Web Token) 就是一个很不错的解决办法。通过这种方式服务器端就不需要保存 Session 数据了,扩展性得到提升。
381+
有没有一种不需要自己存放 Session 信息就能实现身份验证的方式呢?使用 Token 即可!JWT (JSON Web Token) 就是这种方式的实现,通过这种方式服务器端就不需要保存 Session 数据了,只用在客户端保存服务端返回给客户的 Token 就可以了,扩展性得到提升。
382382

383-
在基于 Token 进行身份验证的的应用程序中,服务器通过有效负载(`payload`)和一个密钥(`secret`)创建 令牌(`Token`)并将 `Token` 发送给客户端,客户端将 `Token` 保存在 Cookie 或者 localStorage 里面,以后客户端发出的所有请求都会携带这个令牌。你可以把它放在 Cookie 里面自动发送,但是这样不能跨域,所以更好的做法是放在 HTTP Header 的 Authorization字段中:` Authorization: Bearer Token`
383+
**JWT 本质上就一段签名的 JSON 格式的数据。由于它是带有签名的,因此接收者便可以验证它的真实性。**
384+
385+
下面是 [RFC 7519](https://link.juejin.im/?target=https%3A%2F%2Ftools.ietf.org%2Fhtml%2Frfc7519) 对 JWT 做的较为正式的定义。
386+
387+
> JSON Web Token (JWT) is a compact, URL-safe means of representing claims to be transferred between two parties. The claims in a JWT are encoded as a JSON object that is used as the payload of a JSON Web Signature (JWS) structure or as the plaintext of a JSON Web Encryption (JWE) structure, enabling the claims to be digitally signed or integrity protected with a Message Authentication Code (MAC) and/or encrypted. ——[JSON Web Token (JWT)](https://tools.ietf.org/html/rfc7519)
388+
389+
JWT 由 3 部分构成:
390+
391+
1. Header :描述 JWT 的元数据。定义了生成签名的算法以及 Token 的类型。
392+
2. Payload(负载):用来存放实际需要传递的数据
393+
3. Signature(签名):服务器通过`Payload``Header`和一个密钥(`secret`)使用 Header 里面指定的签名算法(默认是 HMAC SHA256)生成。
394+
395+
在基于 Token 进行身份验证的的应用程序中,服务器通过`Payload``Header`和一个密钥(`secret`)创建令牌(`Token`)并将 `Token` 发送给客户端,客户端将 `Token` 保存在 Cookie 或者 localStorage 里面,以后客户端发出的所有请求都会携带这个令牌。你可以把它放在 Cookie 里面自动发送,但是这样不能跨域,所以更好的做法是放在 HTTP Header 的 Authorization字段中:` Authorization: Bearer Token`
384396

385397
![Token Based Authentication flow](https://my-blog-to-use.oss-cn-beijing.aliyuncs.com/2019-7/Token-Based-Authentication.png)
386398

@@ -390,16 +402,19 @@ Cookie 数据保存在客户端(浏览器端),Session 数据保存在服务器
390402
4. 服务端检查 JWT 并从中获取用户相关信息。
391403

392404

393-
394-
> JSON Web Token (JWT) is a compact, URL-safe means of representing claims to be transferred between two parties. The claims in a JWT are encoded as a JSON object that is used as the payload of a JSON Web Signature (JWS) structure or as the plaintext of a JSON Web Encryption (JWE) structure, enabling the claims to be digitally signed or integrity protected with a Message Authentication Code (MAC) and/or encrypted. ——[JSON Web Token (JWT)](https://tools.ietf.org/html/rfc7519)
395-
396-
397-
398405
推荐阅读:
399406

400407
- [JWT (JSON Web Tokens) Are Better Than Session Cookies](https://dzone.com/articles/jwtjson-web-tokens-are-better-than-session-cookies)
401408
- [JSON Web Tokens (JWT) 与 Sessions](https://juejin.im/entry/577b7b56a3413100618c2938)
402409
- [JSON Web Token 入门教程](https://www.ruanyifeng.com/blog/2018/07/json_web_token-tutorial.html)
410+
- [彻底理解Cookie,Session,Token](https://mp.weixin.qq.com/s?__biz=Mzg2OTA0Njk0OA==&mid=2247485603&idx=1&sn=c8d324f44d6102e7b44554733da10bb7&chksm=cea24768f9d5ce7efe7291ddabce02b68db34073c7e7d9a7dc9a7f01c5a80cebe33ac75248df&token=844918801&lang=zh_CN#rd)
411+
412+
### 10.4 Session vs Token
413+
414+
415+
416+
### 10.5 什么是OAuth 2.0
417+
403418

404419
## 参考
405420

0 commit comments

Comments
 (0)