当前位置:网站首页>Detailed explanation and application principle of token
Detailed explanation and application principle of token
2022-04-23 06:45:00 【Front end more】
One 、 Let's explain Token The meaning of
1、Token The introduction of :
Token It is in the client side that frequently requests data from the server , The server frequently goes to the database to query the user name and password and compares them , Judge whether the user name and password are correct or not , And give corresponding prompt , In this context ,Token It came into being .
2、Token The definition of :
Token Is a string generated by the server , As a token for the client to request , After the first login , The server generates a Token This is what we call it Token Return to the client , In the future, the client only needs to bring this Token Just come and ask for data , No need to bring user name and password again .
3、 Use Token Purpose :
Token The goal is to reduce the pressure on the server , Reduce frequent database queries , Make the server more robust .
4.Token The advantages of :
More scalable , It's safer , Very suitable for Web On apps or mobile apps .Token Chinese translation of “ token ”, I think it's good , It means , You take this token , To pass some checkpoints .
5.Token Generally used in three places :
① Prevent forms from being submitted repeatedly
②anti csrf attack ( Cross-site request forgery )
③ Authentication ( Single sign on )
I understand Token After the meaning of , We will know more clearly why we should use him .
Two 、 How to use Token?
This is the focus of this article , Here I will introduce two common ways .
1、 Equipment No / equipment mac Address as Token( recommend )
client : The client obtains the device number of the device when logging in /mac Address , And pass it as a parameter to the server .
Server side : After the server receives the parameter , We use a variable to receive it as Token Save in database , And put the Token Set to session in , Every time the client requests, it needs to intercept , And deliver the token And the server side session Medium token Contrast , If the same, release , If it's different, refuse to . analysis : At this moment, the client and the server have a unique identity Token, It also ensures that each device has a unique session . The disadvantage of this method is that the client needs to bring the device number /mac The address is passed as a parameter , And the server needs to save ; The advantage is that the client does not need to log in again , Just log in once and use it all the time , As for the timeout problem, the server has to deal with it , How to deal with it ? If the server's Token After a timeout , The server only needs to deliver the Token Query the database , At the same time, it is assigned to the variable Token, such ,Token Time out and re time .
2、 use session Value as Token
client : The client only needs to login with user name and password .
client : The client receives the user name and password and judges , If it is correct, it will be obtained locally sessionID As Token Return to the client , The client only needs to bring the request data .
analysis : The advantage of using this method is convenience , No need to store data , But the disadvantage is to be session After expired , The client must log in again to access the data .
3、 ... and 、 Problems and solutions in the use process ?
We just introduced Token Two ways of using , But in the use process, we also have various problems ,Token In the first method, we hide a problem that will lead to repeated data submission when the network is not good or concurrent requests occur .
The solution to the problem : take session and Token To paraphrase , This will solve the problem , How to apply it ? Take a look at this explanation :
session Is a single operator in the whole operation process , The unique identification information that maintains communication with the server . In multiple requests from the same operator ,session Always make sure it's the same object , Not multiple objects , Because it can be locked . When multiple requests from the same operator enter , Can pass session Restrictions on one-way traffic
This article is through the use of session And in session Add token, To verify whether the same operator has made concurrent and repeated requests , When the next request comes , Use session Medium token Verify... In the request token Is it consistent , When it's inconsistent , Is considered a duplicate submission , Will not be allowed to pass .
This is the solution to duplicate submission .
Four 、 be based on Token Authentication method for
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
版权声明
本文为[Front end more]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230550117381.html
边栏推荐
猜你喜欢
【UDS统一诊断服务】一、诊断概述(2)— 主要诊断协议(K线和CAN)
C语言的浪漫
【UDS统一诊断服务】(补充)五、ECU bootloader开发要点详解 (2)
Class inheritance and derivation
[UDS unified diagnostic service] i. overview of diagnosis (4) - basic concepts and terms
[UDS unified diagnosis service] i. diagnosis overview (1) - diagnosis overview
C [document operation] PDF files and pictures are converted to each other
C语言实用小技巧合集(持续更新)
QT add qserialport class to realize serial port operation
copy constructor
随机推荐
逻辑回归原理及代码实现
软件工程中的十三种文档
C语言进阶要点笔记3
声明为全局变量
在MFC中使用printf
汇编基础代码示例
信息学一本通-小球
Shell脚本 &&和||的使用
HDU-Memory Control
函数的调用过程
Initialization of classes and objects (constructors and destructors)
TP download folder, compress folder and download
圆整 round 的一点点小细节
C语言代码规范
搭建jpress个人博客
[UDS unified diagnosis service] IV. typical diagnosis service (3) - read fault information function unit (storage data transmission function unit)
文件查看命令和用户管理命令
Understanding of SSH public key and private key
[learn] HF net training
ARM常用汇编指令