您的位置 首页 php

用了这么久,token和session的区别,你真的清楚了吗?

session和token都是用来保持会话,功能相同

一、session机制,原理

用了这么久,token和session的区别,你真的清楚了吗?

session是服务端存储的一个对象,主要用来存储所有访问过该服务端的客户端的用户信息(也可以存储其他信息),从而实现保持用户会话状态。但是服务器重启时,内存会被销毁,存储的用户信息也就消失了。

不同的用户访问服务端的时候会在session对象中存储键值对,“键”用来存储开启这个用户信息的“钥匙”,在登录成功后,“钥匙”通过 cookie 返回给客户端,客户端存储为sessionId记录在cookie中。当客户端再次访问时,会默认携带cookie中的sessionId来实现会话机制。

session是基于cookie的。

  • cookie的数据4k左右
  • cookie存储数据的格式: 字符串 key=value
  • cookie存储有效期:可以自行通过expires进行具体的日期设置,如果没设置,默认是关闭浏览器时失效。
  • cookie有效范围:当前域名下有效。所以session这种会话存储方式方式只适用于客户端代码和服务端代码运行在同一台服务器上(前后端项目协议、域名、 端口号 都一致,即在一个项目下)

session 持久化

用于解决重启服务器后session就消失的问题。在数据库中存储session,而不是存储在内存中。通过包:express-mysql-session

其它

当客户端存储的cookie失效后,服务端的session不会立即销毁,会有一个延时,服务端会定期清理无效session,不会造成无效数据占用存储空间的问题。

二、token机制,原理

用了这么久,token和session的区别,你真的清楚了吗?

适用于项目级的前后端分离(前后端代码运行在不同的服务器下)

请求登录时,token和sessionId原理相同,是对key和key对应的用户信息进行加密后的加密字符,登录成功后,会在响应主体中将{token:’字符串’}返回给客户端。客户端通过cookie、sessionStorage、localStorage都可以进行存储。再次请求时不会默认携带,需要在请求拦截器位置给请求头中添加认证字段Authorization携带token信息,服务器端就可以通过token信息查找用户登录状态。

文章来源:智云一二三科技

文章标题:用了这么久,token和session的区别,你真的清楚了吗?

文章地址:https://www.zhihuclub.com/80536.shtml

关于作者: 智云科技

热门文章

评论已关闭

39条评论

  1. Patients receiving anastrozole had an increase in the incidence of all fractures specifically fractures of spine, hip and wrist 315 10 compared with patients receiving Tamoxifen Citrate Actavis tamoxifen citrate 209 7

  2. Physical and occupational therapies provide education and techniques for minimizing a variety of physical disabilities that, if not corrected, could result in significant change in appearance and in modification of activities of daily living

  3. Primordial follicles begin to decrease in number from early stages in the embryonic period until definitive menopause Antoine AhwbUFSvtIO 5 20 2022

  4. The USPSTF stresses that the medication should be considered only for women whose odds of developing breast cancer are higher than average but determining those odds is not always easy Shih EJ, Lee WJ, Hsu JL, Wang SJ, Fuh JL

  5. It refers to the military line between Indian and Pakistani controlled areas of the state of Jammu and Kashmir

  6. Generic Name Nilotinib Manufacturer Novartis Pharmaceuticals Corporation 2007, 16 Suppl 2 105 113

  7. lamisil zyprexa dosage forms Just a few days before the SAT was administered, Jain took the AP Calculus BC exam, a course usually offered to the most mathematically advanced high school seniors

  8. Hormone receptor proteins allow a cell to recognize and respond to signals from the estrogen and progesterone hormones Psychooncology 10 6 479 89, 2001 Nov Dec

  9. The driving force for the K Cl cotransporter was estimated on the basis of the Nernst equation given the calculated Cl i and K o from Table 1 Parenteral 10 to 40 mEq Cloreto De Potassio for injection diluted in an appropriate amount and type of solution to be intravenously infused once at a rate not to exceed 40 mEq hour

  10. While the development of antibiotics has lengthened our lifespans, our excessive and inappropriate use of these drugs may be causing serious long term consequences we are only now becoming fully aware of

  11. Owing to the rarity of BcaFad cases, clinicians in individual medical institutions have very little experience in treating these patients

  12. cialis bisoprolol nombres comerciales en mexico Brazos County Attorney Rod Anderson says Manziel was ordered to pay a 2, 000 fine, about 230 in court costs and was credited with the overnight jail time he served following his arrest

  13. To directly compare the responsiveness to, and requirement for, supplemental estrogen of HCI 013 vs

  14. Conditions that increase cellular K concentration, decrease luminal K concentration, or render the lumen more electronegative will increase the rate of K secretion The instrument was used to inquire respondents on how true each statement was for the past 7 days and rate them

  15. These studies indicate that many breast cancer patients whose underarm lymph nodes show no signs of cancer known as node negative may benefit from chemotherapy or hormonal therapy after primary treatment Reproductive health encompasses the beginning of menstruation for women, choosing the right birth control method for you and your partner, preventing contracting sexually transmitted diseases STDs, and for women, ending with the menopausal transition

  16. Secondary ion mass spectrometry SIMS is an important tool for investigating isotopic composition in the chemical and materials sciences, but its use in biology has been limited by technical considerations

  17. Chemically, it is a substituted benzamide, closely related to metoclopramide She described sexual dysfunction related to aromatase inhibitors in a talk at the 2017 Miami Breast Cancer Conference

  18. Walking seems to help The ratios of Annexin V negative and PI negative Annexin V PI, Annexin V positive and PI negative Annexin V PI, Annexin V positive and PI positive Annexin V PI, and Annexin V negative and PI positive Annexin V PI cells were analyzed right

  19. Determination of minimum inhibitory concentration that inhibited growth of 99 of the inoculated cells drafted manuscript; F

  20. Asthenozoospermia Abnormal sperm motility low of motile sperm In 1998, use of Indian Health Service facilities is no longer treated as insurance

网站地图