当前位置:网站首页>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一样使用?
边栏推荐
- MySQL (UDF authorization)
- Inclusion exclusion principle acwing 890 Divisible number
- Find the combination number acwing 888 Find the combination number IV
- Winter vacation water test 1 Summary
- 达梦数据库全部
- Utf8 encoding
- Genesis builds a new generation of credit system
- 安装OpenCV--conda建立虚拟环境并在jupyter中添加此环境的kernel
- Sum of three terms (construction)
- H5 模块悬浮拖动效果
猜你喜欢
SolidWorks template and design library are convenient for designers to call
容斥原理 AcWing 890. 能被整除的数
Alibaba established the enterprise digital intelligence service company "Lingyang" to focus on enterprise digital growth
Configuration method and configuration file of SolidWorks GB profile library
TCP's understanding of three handshakes and four waves
Rehabilitation type force deduction brush question notes D1
MySQL (UDF authorization)
Use ffmpeg to rotate, flip up and down, and flip horizontally
Stack acwing 3302 Expression evaluation
Get class files and attributes by reflection
随机推荐
What is linting
confidential! Netease employee data analysis internal training course, white whoring! (attach a data package worth 399 yuan)
Inclusion exclusion principle acwing 890 Divisible number
Series of how MySQL works (VIII) 14 figures explain the atomicity of MySQL transactions and the principle of undo logging
Idea debug failed
2022-5-第四周日报
2.Oracle-数据文件的添加及管理
Find the combination number acwing 887 Find combination number III
[BMZCTF-pwn] ectf-2014 seddit
All English in the code
Chinese remainder theorem acwing 204 Strange way of expressing integers
[Chongqing Guangdong education] 1185t administrative leadership reference test of National Open University in autumn 2018
Configuration method and configuration file of SolidWorks GB profile library
How to make water ripple effect? This wave of water ripple effect pulls full of retro feeling
3.Oracle-控制文件的管理
H5 模块悬浮拖动效果
Redis-01. First meet redis
LSA Type Explanation - lsa-1 [type 1 LSA - router LSA] detailed explanation
[2021]GIRAFFE: Representing Scenes as Compositional Generative Neural Feature Fields
[QT] QT multithreading development qthread