当前位置:网站首页>Talk about the principle of QR code scanning login
Talk about the principle of QR code scanning login
2022-06-11 14:57:00 【Ant】
Talk about the principle of QR code scanning and login
In daily life, there are a lot of scenes of scanning QR code , Recently, I happened to see videos and articles about this , Learn about the technology and logic behind the QR code .
One of the most commonly used scenarios for two-dimensional code is scanning through mobile applications PC perhaps WEB The end of the QR code , To log in to the same system .
For example, mobile wechat scan code login PC End wechat , Mobile Taobao scan code login PC Taobao . So let's take a look at , How does QR code login work !
The essence of QR code login
In essence, QR code login is also a way of login authentication .
There are two things you need to do for regular login authentication :
- Tell the system who I am
- Prove to the system who I am
By scanning the QR code , Transfer the account information of mobile phone to PC End , Prove to the system who I am .
Mobile terminal APP It's already logged in , That is to say, the mobile terminal has passed the login authentication . as long as Scan the code to confirm that the mobile phone is operated by this account , In fact, it can indirectly prove who I am .
QR code
First, let's get to know the QR code , Before we know two-dimensional code, let's take a look at one-dimensional code , Also known as bar code : One dimensional bar code
One dimensional code , That's the bar code , Bar codes in supermarkets – I believe everyone is very familiar with this , A bar code is actually a string of numbers , It stores the serial number of the product .
Two dimensional code is similar to bar code , It's just that it doesn't store numbers , It can also be any string , You can think , It's just another representation of strings .
System authentication mechanism
For the sake of safety , It will not store your login password on the mobile terminal . But in daily use , We should have noticed , Only after your app is downloaded , The first time I log in , You need to log in with an account and password , After that Even if the application process is killed , Or the phone restarts , There is no need to re-enter the account password , It can log in automatically .
be based on token Authentication mechanism of :
- Account password login , The client will pass the device information to the server ,
- If the account and password pass the verification , The server will bind the account to the device , In a data structure , This data structure contains accounts ID, equipment ID, Equipment type, etc
const token = {
acountid:' account number ID',
deviceid:' Login device ID',
deviceType:' Device type , Such as iso,android,pc......',
}Then the server will generate a token, Use it to map data structures , This token It's actually a string of strings with a special meaning , The point of it is , You can find the corresponding account and device information through it .
- The client gets this token after , You need to do a local save , Every time you visit the system API Take them with you token And device information .
- The server can use token Find the account and device information bound to it , Then compare the bound device information with the device information sent by the client each time , If the same , So the verification passed , return AP Interface response data , If different , That is, the verification fails and access is denied
The client doesn't and doesn't need to save your password , contrary , It's preserved token. Maybe some students will think , This token So important , What if someone else knows . actually , It doesn't matter if I know , Because device information is unique , As long as your device information is unknown to others , Other people use other devices to access , The verification also failed .
The purpose of client login , That is to get what belongs to you token.
Scan QR code login general steps :
- Before scanning code , The mobile app is logged in ,PC There's a QR code on the end , Waiting for the scan
- Open the app on the mobile terminal , scanning PC The end of the QR code , After scanning , Will prompt ” Scanned , Please click "confirm" on the mobile phone ”
- The user clicks "confirm" on the mobile terminal , After the confirmation PC The end login is successful
You can see , The QR code has three states in the middle , To be scanned , Scanned for confirmation , Confirmed .
- There must be a uniqueness behind the two-dimensional code ID, When the QR code is generated , This ID It's also generated together , And bound PC The device information of the terminal
- Mobile phone to scan this QR code
- The QR code switches to Status scanned for confirmation , At this point, the account information will be associated with this ID binding
- When the mobile terminal confirms the login , It will generate PC End for login token, And return it to PC End
The specific process :
QR code preparation
According to different states of QR code , The first is waiting for the scan state , User opens PC End , When switching to the QR code login interface .
- PC The client sends a request to the server , Tell the server , I want to generate the QR code of user login , And the PC The end device information is also transmitted to the server
- After the server receives the request , It generates a two-dimensional code ID, And the QR code ID And PC The end device information is bound
- And then put the QR code ID Return to PC End
- PC The terminal receives the QR code ID after , Generate qr code ( The QR code must contain ID)
- In order to know the status of the QR code in time , After the client shows the QR code ,PC The server keeps polling the server , Like polling every second , Request the server to tell the status and related information of the current QR code
Scan state switch
- Users use their mobile phones to scan PC The end of the QR code , Get the QR code from the QR code ID
- Then call the server API Combine the identity information of mobile terminal with two-dimensional code ID Send it to the server together
- After the server receives it , It can combine identity information with two-dimensional code ID Binding , Generate temporary token. And then back to the phone
- because PC The client has been polling the QR code status , So at this time, the state of the QR code changes , It can update the QR code status to scanned on the interface
temporary token For the next operation of the mobile terminal , You can use it as a voucher . To make sure you scan the code , The two steps of login are sent from the same mobile terminal .
Status confirmation
- The mobile terminal receives the temporary token After that, the login confirmation interface will pop up , When the user clicks confirm , Mobile phones carry temporary token The interface used to call the server , Tell the server , I have confirmed
- After the server receives the confirmation , According to the QR code ID Bound device information and account information , Generate users PC End login token
- Now PC The polling interface at the end , It can tell that the state of the QR code has become ” Confirmed ”. And you can get the login information from the server token
- Come here , Login is successful , Back end PC You can use token To access the resources of the server
summary
We trigger from the nature of landing , Explore how QR code scanning login works
- Tell the system who I am
- Prove to the system who I am
In the process , Let's talk about two premises first ,
- One is the principle of two-dimensional code ,
- One is based on
tokenAuthentication mechanism of .
And then we take the QR code status as the axis , The logic behind this is analyzed : adopt token Authentication mechanism and two-dimensional code state change to achieve code scanning login .
It's important to point out that , The login process mentioned above , It applies to the same system PC End ,WEB End , Mobile .
Reference material
- TikTok Two sides :“ Talk about the principle of QR code scanning and login ”.
- Classmate Dagu
- Geek time, a QR code video
Sharing plans
Blog content will be synchronized to Tencent cloud + Community , Invite everyone to join us :https://cloud.tencent.com/
license agreement
In this paper A signature - Noncommercial use - Share in the same way 4.0 The international license agreement , Reprint please indicate the source .
边栏推荐
- 高通WLAN框架学习(29)-- 6GHz 概述
- Why can redis be so fast?
- 回溯法/解空间树 排列树
- Live800: several ways for intelligent customer service to improve customer experience
- 2021 年 CNCF 调查:Kubernetes 跨越鸿沟的一年
- 基于 GateWay 和 Nacos 实现微服务架构灰度发布方案
- 社交软件Soul撤回IPO申请:上市只差临门一脚 腾讯是大股东
- C # - how to add and read appsetting in the console application JSON file
- [process blocks and methods of SystemVerilog] ~ domain, always process block, initial process block, function, task, life cycle
- 回溯法/活动安排 最大兼容活动
猜你喜欢

Summary of some classic embedded C interview questions

深度剖析「圈组」关系系统设计 | 「圈组」技术系列文章

Did you break the rules?

深度剖析「圈組」關系系統設計 | 「圈組」技術系列文章

漫画:有趣的 “切蛋糕“ 问题

【SystemVerilog 之 过程块和方法】~ 域、always过程块、initial过程块、函数 function、任务 task、生命周期

腾讯面试官分享面试经验,如何考察面试者技术及个人综合素质,给正在面试的你一点建议

Illustration of tiger international quarterly report: revenue of USD 52.63 million continued to be internationalized

C language simple webserver

高数_第6章无穷级数__马克劳林级数
随机推荐
B站高管解读财报:疫情对公司长期发展无影响 视频化趋势不可阻挡
In depth research and analysis report on global and Chinese liquid malt extract products market
In depth research and analysis report on global and Chinese hydrogen fuel station market
19. Insertion et suppression d'un arbre de recherche binaire
2022 Hunan Provincial Safety officer-c certificate examination practice questions and online simulation examination
one hundred and twenty-three thousand four hundred and sixty-five
以 Log4j 为例,如何评估和划分安全风险
【SystemVerilog 之 接口】~ Interface
2021 go developer survey
在微服务架构中管理技术债务
Raspberry school literacy
Dynamically set the layoutinflater of the layout
uniapp设置页面跳转效果 - navigateTo切换效果 - 全局animationType动画
Analyse approfondie de la conception du système relationnel du Groupe de cercles
Hamad application layout scheme of hashicopy 01
Learn more about and use ThreadLocal
Why can redis be so fast?
In depth research and analysis report on global and Chinese gas monitor market
Why do I need the public static void main (string[] args) method?
A former employee of Baidu was awarded 1.07 million yuan for job hopping; Apple, Google and Microsoft plan to "kill" the password; It is said that Geely has acquired Meizu | Q information