当前位置:网站首页>Advantages of token mechanism over cookie mechanism
Advantages of token mechanism over cookie mechanism
2022-06-23 20:43:00 【Evan Technology】
When the client frequently requests data from the server , The server will frequently go to the database to query the user name and password and compare them , Judge whether the user name and password are correct or not , And give corresponding prompt , In this context Token It came into being .
Simply speaking ,Token Is a string generated by the server , As a token for the client to make the 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 , The purpose is to reduce the pressure on the server , Reduce frequent database queries , Make the server more robust .
Generate Token Data encryption in the process
The client requests the server to generate token In the process of , There are two main situations in which data needs to be encrypted . First, you need to pass the user name and password when logging in for the first time , The password cannot be transmitted in clear text , need RSA To encrypt , Send it to the server and decrypt it through the private key . Second, the server transmits for the first time token When giving to the client, you can token Conduct RSA encryption , The client decrypts through the private key , Here's the picture :
Have a brief understanding of Token The formation process and action of , Let's discuss the common authentication mechanism , There are mainly HTTP Basic Auth(HTTP Basic Authentication )、OAuth( Open licensing )、Cookie Auth、Token Auth Four mechanisms .
HTTP Basic Auth
HTTP Basic Auth(HTTP Basic Authentication ), Simply put, every time request API Provide users with username and password, in short ,Basic Auth It's cooperation RESTful API The simplest way to use authentication , Just provide the user name and password , But because of the risk of exposing the user name and password to a third-party client , Less and less is used in the production environment . therefore , In the process of opening up to the outside world RESTful API when , Try to avoid HTTP Basic Auth
OAuth
OAuth( Open licensing ) It's an open authorization standard , Allow users to let third-party applications access the user in a web Private resources stored on services ( Such as photos , video , contact list ), Instead of providing a user name and password to a third-party application .
OAuth Allows the user to provide a token , Instead of user names and passwords to access what they store in a particular service provider data . Each token authorizes a specific third-party system ( for example , Video editing sites ) At certain times ( for example , Next 2 Within hours ) To access specific resources within ( For example, it's just a video in an album ). such ,OAuth Allow users to authorize third-party websites to access certain specific information they store with other service providers , Not everything Here is OAuth2.0 The process of :
This is based on OAuth The authentication mechanism of is applicable to the Internet products of individual consumers , Such as social APP And so on , However, it is not suitable for enterprise applications with self certification authority management .
Cookie Auth
Cookie( Data stored on the user's local terminal ) The authentication mechanism is to create a server for one request authentication Session object , At the same time, a Cookie object ; Bring it up through the client Cookie Object to communicate with the server side session Object matching to achieve state management . default , When we close the browser ,cookie Will be deleted . But it can be modified cookie Of expire time send cookie It works for a certain period of time ;
Cookie It is mainly used in the following three aspects :
1、 Session state management , Such as user login status 、 The shopping cart 、 Game score or other information to be recorded ;
2、 Personalization , Such as user-defined settings 、 Theme, etc ;
3、 Browser behavior tracking .
Token Auth
Token Auth The advantages of
Token The mechanism is relative to Cookie What are the benefits of the mechanism ?
1. Support cross domain access :Cookie Is not allowed to cross domain access , That's right Token There is no mechanism , The premise is that the transmitted user authentication information passes HTTP Head transmission .
2. No state ( Also known as : Server extensible line ):Token The mechanism does not need storage on the server session Information , because Token It contains the information of all users who log in , Just on the client side cookie Or local media storage status information .
3. More suitable CDN: You can request all the data of your server through the content distribution network ( Such as :javascript,HTML, Pictures, etc ), And your server only provides API that will do .
4. decoupling : There is no need to bind to a specific authentication scheme .Token It can be generated anywhere , Just in your API When called , You can do Token Generate the call .
5. More suitable for mobile applications : When your client is a native platform (iOS, Android,Windows 8 etc. ) when ,Cookie It's not supported ( You need to go through Cookie Container for handling ), Use Token The authentication mechanism will be much simpler .
6.CSRF: Because it no longer depends on Cookie, So you don't have to think about it CSRF( Cross-site request forgery ) Prevention of .
7. performance : A round trip time of network ( Query through database session Information ) It's always a comparison HMACSHA256 Calculation Of Token Validation and parsing take a lot of time .
8. There is no need to do special handling for the login page : If you use Protractor When doing a function test , No longer need to do special handling for login page .
9. Based on Standardization : Yours API Standardized... Can be used JSON Web Token (JWT), There are already multiple back-end libraries for this standard (NET, Ruby, Java,Python, PHP) And support from multiple companies ( Such as :Firebase,Google, Microsoft).
边栏推荐
- Use of the vs2022scanf function. An error is reported when using scanf - the return value is ignored: Solutions
- . Net cloud native architect training camp (rgca four step architecture method) -- learning notes
- ntpupdate. tencentyun. Com has been eliminated
- 重庆 奉节耀奎塔,建成后当地连中五名进士,是川江航运的安全塔
- Teach you how to use web pages to develop apps
- 【Golang】快速复习指南QuickReview(四)——函数
- 徽商期货交易软件正规吗?如何安全下载?
- Is it possible to transfer files on the fortress server? How to operate?
- Which is better for securities companies? I don't understand. Is it safe to open an account online?
- How to dispose of the words on the picture? How do I add text to a picture?
猜你喜欢

Syntax of SQL union query (inline, left, right, and full)

Interpreting the 2022 agile coaching industry status report

FPGA based electromagnetic ultrasonic pulse compression detection system paper + source file

Ugeek's theory 𞓜 application and design of observable hyperfusion storage system

Eight misunderstandings, broken one by one (final): the cloud is difficult to expand, the customization is poor, and the administrator will lose control?

3000 frame animation illustrating why MySQL needs binlog, redo log and undo log

Kubernetes 资源拓扑感知调度优化

SQL聯合查詢(內聯、左聯、右聯、全聯)的語法

vs2022scanf函数的使用,使用scanf的报错-返回值被忽略:解决·方法

JS高级程序设计第 4 版:生成器的学习
随机推荐
Interpreting the 2022 agile coaching industry status report
[golang] quick review guide quickreview (x) -- goroutine pool
Technology sharing | wvp+zlmediakit realizes streaming playback of camera gb28181
券商选哪个比较好尼?本人小白不懂,在线开户安全么?
[golang] quick review guide quickreview (VIII) -- goroutine
How to open a domestic futures account? Which futures company is safer to open an account?
徽商期货交易软件正规吗?如何安全下载?
How to make a material identification sheet
Elastricearch's fragmentation principle of the second bullet
Application of MySQL time function, simple problem
Importance and purpose of test
December 29, 2021: the elimination rules of a subsequence are as follows: 1. In a subsequence
Script tag attributes and & lt; noscript> label
5 月最大的 GameFi 崩溃受害者能否在熊市中生存?| May Monthly Report
How do I close and restore ports 135, 139 and 445?
How do I open an account? Is it safe to open an account in Guohai Securities? What do you need to bring?
How to deal with product pictures? How to select mapping software?
Configure two databases in master-slave database mode (master and slave)
Zabbix监控- Aruba AP运行数据
What cloud disk types does Tencent cloud provide? What are the characteristics of cloud disk service?