当前位置:网站首页>Session&Cookie&token
Session&Cookie&token
2022-07-27 04:33:00 【Yellow mud Sichuan water monkey】
One .Session
What is? Session?
A special object created by the server to save user state .
When the browser first accesses the server , The server creates a session object ( The object has a unique id, Generally known as sessionId), The server will sessionId With cookie Send it to the browser .
When the browser accesses the server again , Will sessionId Send it , The server is based on sessionId You can find the corresponding session object .
The difference between stateful and stateless
Basic concepts :
A stateful It has the function of data storage . A stateful object (Stateful Bean), It's an object with instance variables , Can save data , It's not thread safe . Leave no state between different method calls .
No state It's an operation , Can't save data . Stateless objects (Stateless Bean), Objects without instance variables . Can't save data , Is an immutable class , It's thread safe . Stateless services do not record service status , There is also no relationship between different requests ;http Protocol is stateless
- Stateless service ( common JWT)
- Stateful service ( common redis simulation session Store information )
Session The shortcomings of :
- When the server traffic increases , There will be many Session, If no timeout or destruction is set , It is easy to cause server crash and other conditions .
- When the server is clustered or distributed , Users log on to one of the servers , Will session Save to the memory of the server , But when users access other servers , Will not be able to access , Cache consistency technology is usually used to ensure that it can be shared , Or use a third-party cache to save session, inconvenient .
Two .cookie
1. Cookie How it works
cookie It is a small part of text information saved by the server in the browser , Every cookie No more than 4kb. Every time the browser makes a request to the server , This information will be automatically attached .
(1) The browser sends the request to the server for the first time
(2) Server side creation Cookie, The Cookie Contains information about the user , Then the Cookie Send to browser side
(3) When the browser side visits the server side again, it will carry the Cookie
(4) Server side through Cookie The data carried in can distinguish different users 
Difference and contrast
(1)cookie The data is stored on the client's browser ,session Data on the server
(2)cookie Not very safe , Others can analyze the local COOKIE And carry on COOKIE cheating , If the main consideration is safety, we should use session
(3)session It will be saved on the server for a certain period of time . When visits increase , It will take up the performance of your server , If the main consideration is to reduce server performance , Should be used COOKIE
(4) Single cookie The limit on the client side is 3K, That is to say, a site is stored in the client COOKIE You can't 3K.
(5) therefore : Store the login information and other important information as SESSION; Other information if needed to be retained , Can be placed in COOKIE in
3、 ... and .token
1.token The principle,
It's a computer term : token , Token is a special frame that can control the media occupied by the site , To distinguish between data frames and other control frames .token In fact, the more popular point can be called the code , Before some data is transmitted , We need to check the code first , Different code is authorized to operate different data . be based on Token Authentication method for
2.token Usage flow
Using a Token Authentication method for , The server does not need to store the user's login record . The general process is like this
- Client requests login with user name and password
- The server receives the request , To verify the user name and password
- After successful verification , The server will issue a Token, Put this again. Token Send to client
- Client received Token You can store it later , For example Cookie Or Local Storage in
- Each time the client requests resources from the server, it needs to bring the Token
- The server receives the request , Then go to verify the client request Token, If the validation is successful , Return the requested data to the client
Four .JWT

jwt and token The difference between
Conclusion :
The most intuitionistic :token Database verification is required token Whether it works , and JWT No or less inventory checking , Check directly on the server , And you don't have to check the library .
Because the user's information and encrypted information are in the second part payload And the third part of the visa has been generated , Just check on the server , And the verification is also JWT Self realized .
Reference resources :token and JWT difference
5、 ... and .localStorage
stay HTML5 in , Added a new localStorage characteristic , This feature is mainly used as local storage , It's solved cookie Lack of storage space (cookie Each of them cookie The storage space of is 4k),localStorage The general browser support in 5M size , This is in different browsers localStorage It will be different .
边栏推荐
- xxx is not in the sudoers file. This incident will be reported
- Deep analysis - dynamic memory management
- 网工知识角|只需四个步骤,教会你使用SecureCRT连接到eNSP,常用工具操作指南必看
- Pinia入门到精通,Pinia使用全流程,包含state,actions,getters,以及如何解构,进行响应,actions使用的多种方法
- 匿名命名管道, 共享内存的进程间通信理解与使用
- Worship the 321 page PDF of the core technology of Internet entrepreneurship that Alibaba is pushing internally. It's really kneeling
- Brightcove appoints Dan Freund as chief revenue Officer
- How to set user-defined display for Jiaming Watch
- Wechat applet rotation map
- playwright网络爬虫实战案例分享
猜你喜欢

JMeter学习笔记004-CSV文件行数控制循环次数

Rust:axum learning notes (1) Hello World

HEAD detached from origin/...导致push失败

Ant JD Sina 10 architects 424 page masterpiece in-depth distributed cache from principle to practice pdf

佳明手表怎么设置用户定制显示

Deep analysis - dynamic memory management
![[final review of software engineering] knowledge points + detailed explanation of major problems (E-R diagram, data flow diagram, N-S box diagram, state diagram, activity diagram, use case diagram...)](/img/f4/70634556c4ae8fc3b087084e1e27b3.png)
[final review of software engineering] knowledge points + detailed explanation of major problems (E-R diagram, data flow diagram, N-S box diagram, state diagram, activity diagram, use case diagram...)

匿名命名管道, 共享内存的进程间通信理解与使用

法解析的外部符号 “public: virtual __cdecl nvinfer1::YoloLayerPlugin::~YoloLayerPlugin(void)“ “public: virtua
![Shell的正则表达式入门、常规匹配、特殊字符:^、$、.、*、字符区间(中括号):[ ]、特殊字符:\、匹配手机号](/img/31/ed0d8c1a5327059f2de7493bec1c6c.png)
Shell的正则表达式入门、常规匹配、特殊字符:^、$、.、*、字符区间(中括号):[ ]、特殊字符:\、匹配手机号
随机推荐
佳明手表怎么设置用户定制显示
js三种遍历数组的方法:map、forEach、filter
STM32基于HAL库的串口接受中断和空闲中断
els 兼容性DC、传递图片到窗口
[small sample segmentation] msanet: multi similarity and attention guidance for boosting few shot segmentation
How CentOS installs mysqldump
Playwright web crawler actual battle case sharing
BigDecimal pit summary & Best Practices
Word/excel has a fixed table size. When filling in the content, the table does not change with the cell content
Ribbon load balancing strategy and configuration, lazy loading and hungry loading of ribbon
Navicat exports Mysql to table structure and field description
Standard C language 13
项目参数做成可配置项,@ConfigurationProperties注解的使用
微服务的feign调用header头被丢弃两种解决方案(附源码)
Do you know about wechat merchant billing?
Hash (hash)
[day02] Introduction to data type conversion, operators and methods
[leetcode] day104 no overlapping interval
电商分账系统重要吗,平台应该如何选择分账服务商呢?
2022-07-26: what is the output of the following go language code? A:5; B:hello; C: Compilation error; D: Running error. package main import ( “fmt“ ) type integer in