当前位置:网站首页>Cookie、Session、JWT、token四者间的区别与联系
Cookie、Session、JWT、token四者间的区别与联系
2022-07-05 06:24:00 【魏云舒】
Cookie、Session、JWT、Token区别与联系
http无状态请求
- 网络通信使用http协议,每次请求之间并没有关联
访问接口需认证
- 后端接口并不能向外部暴露,只有认证通过的请求才能够访问,常用的认证方式如:用户名密码、短信验证码,但http是无状态的,每次请求都需要进行认证;
- 不想每次发送请求都输入用户名密码,可以将用户名密码放在浏览器当中,但这样一旦浏览器受到攻击,所有的密码都会泄露;
Cookie
- 浏览器中有个存放Cookie的地方,Cookie里可以存放<key,value>这种键值对,也可以存放对应的有效期,和相应的访问域名地址,用户访问该域名时,就拿到这里对应的cookie;
- 当用户第一次拿用户名密码访问建立请求后,服务端可以将用户名密码放在cookie当中,浏览器下次再使用时,就可以直接登录,但这样无异于裸奔,非常不安全;
Session
- 服务器内存中存放客户端状态信息,客户端访问后,建立一个唯一标识session_id和其他相应信息,存放在本地中,并且将这个session_id放到返回的cookie当中,传回给浏览器;浏览器下次访问该服务时,就直接拿session_id作为标识,服务器中与存储的session_id进行比对;当服务器中的session_id有效期到了,对应的就过期了,如果用户退出,浏览器中的cookie也相应的删除
JWT
JWT(Json web token)由三部分组成:
- Header :描述 JWT 的元数据。定义了生成签名的算法以及 Token 的类型。
- Payload(负载):用来存放实际需要传递的数据
- Signature(签名):服务器通过Payload、Header和一个密钥(secret)使用 Header 里面指定的签名算法(默认是 HMAC SHA256)生成。
用户登录后,服务端生成JWT,将一些不敏感的信息放在JWT的Payload当中,传回给用户;
用户下次登录时拿JWT作为自己的身份信息,直接登录;但这样JWT由用户控制,在其未失效之前,服务端对其没有控制权限;
Token
- 一段不携带具体信息的字符串,将用户信息存放在Redis或Mysql等内存当中,Token作为其key
- 用户登录,将Token返回给用户,每次登录后先通过token读取用户信息,然后在进行校验等;
Refresh Token
- Token具有时效,Refresh Token实效稍长,Token失效后,用Refresh Token刷新Token,保持Token的延续性
三个问题:
Cookie作为客户端存储、session作为服务端存储,将Session_id作为客户端与服务端的关联,客户端通过Session_id验证自己的登录状态;Cookie(用户名+加密密码)与JWT如此之像;Cookie/session与token如此之像,他们最主要的区别是什么?
Session存储在单个服务器当中,当用户同时在线量多是,Session会占用较多的内存;而且当网站采用集群部署时,多台服务器之间需要共享用户登录态。那将Session放到mysql中存放不就解决了吗?
Cookie会有跨域问题,token不存在跨域问题,那为何不将Cookie当成token一样使用?
边栏推荐
- Alibaba established the enterprise digital intelligence service company "Lingyang" to focus on enterprise digital growth
- Vant Weapp SwipeCell設置多個按鈕
- 7.Oracle-表结构
- 容斥原理 AcWing 890. 能被整除的数
- 论文阅读报告
- 4. Oracle redo log file management
- 5. Oracle TABLESPACE
- 如何正确在CSDN问答进行提问
- Application of recyclerview
- [QT] QT multithreading development qthread
猜你喜欢

Gaussian elimination acwing 884 Gauss elimination for solving XOR linear equations

Dameng database all

7.Oracle-表结构

Design specification for mobile folding screen

Suppose a bank's ATM machine, which allows users to deposit and withdraw money. Now there is 200 yuan in an account, and both user a and user B have the right to deposit and withdraw money from this a

博弈论 AcWing 892. 台阶-Nim游戏

Stack acwing 3302 Expression evaluation

Inclusion exclusion principle acwing 890 Divisible number

Vant weapp swippecell set multiple buttons

Redis-01. First meet redis
随机推荐
安装OpenCV--conda建立虚拟环境并在jupyter中添加此环境的kernel
FFmpeg build下载(包含old version)
2048项目实现
Sum of two numbers, the numbers in the array are converted to decimal, added, and output inversely
求组合数 AcWing 888. 求组合数 IV
Bash exercise 17 writing scripts to install the server side of FRP reverse proxy software
[Gaode map POI stepping pit] amap Placesearch cannot be used
Design specification for mobile folding screen
栈 AcWing 3302. 表达式求值
Sorting out the latest Android interview points in 2022 to help you easily win the offer - attached is the summary of Android intermediate and advanced interview questions in 2022
The route of wechat applet jumps again without triggering onload
达梦数据库全部
LeetCode-54
PR automatically moves forward after deleting clips
Vscode configures the typera editor for MD
Find the combination number acwing 887 Find combination number III
5. Oracle TABLESPACE
H5内嵌App适配暗黑模式
MPLS experiment
Record of problems in ollvm compilation