当前位置:网站首页>JWT expiration processing - single token scheme
JWT expiration processing - single token scheme
2022-06-30 09:37:00 【Heavenly Master wuyanzu】
For projects with front and rear ends separated jwt As an interface, the security mechanism will encounter jwt Overdue questions .
jwt The expiration time can be set in , Even if it is set to one month , But maybe the user is still using the last second , Next second jwt Expired and called to log in again , This is unacceptable , So it needs to be handled jwt Expiration mechanism .
On this issue, the commonly used method is to use double token——access token and refresh token To deal with it ,access token User authorization ,refresh token It is used to get new after the former expires access token.
here .
I'm here to record my single token programme The idea of .
- Generated when the user logs in token And set expiration time ( Here you can set a shorter time , Such as 30 minute ), Put this token Return and carry it in the next request for authentication . At the same time we are redis Save the token Refresh flag for ,key by "jwt_token:userId",value For the generated token, And set expiration time ( Here you can set a longer time , Such as 7 God , As the maximum unused time of the user , More than, you need to log in again ).
- The back end receives the front end request , If you carry token Not expired , Then visit normally .
- The back end receives the front end request , If you carry token Be overdue , Then access is denied and an jwt Expired exception .
- Front end awareness token After expiration, you need to bring expired token visit token Refresh interface , Back end query redis,1)value And token identical : Generate a new token, Cover value, And refresh value The expiration time of , Back to front ;2)value And token inequality , Refuse to refresh , Users log in again ;3)key non-existent , Refuse to refresh , Users log in again .
- When the user logs in again , Generate a new token, And put redis The corresponding records in , When the user logs off, the redis Delete records in , old token Will expire in a short time and cannot be refreshed .
Compared with double token The plan , single token Method is access token again refresh token, This has the advantage of being insensitive to the front end , The front end only needs to manage token, After the expiration, bring the expired token Recapture .redis The expiration time maintained in is how long the user has not used the time when he must log in .
There is still room for improvement in this method , If in key Add the equipment number field in the , In this way, the coverage problem of multi device login can be solved , You can also manually control logout on different devices .
There may be some thoughtless places , I hope you can give me some advice .
边栏推荐
- Terminal -- Zsh of terminal three swordsmen
- What kind of experience is it to develop a "grandson" who will call himself "Grandpa"?
- Redis docker 主从模式与哨兵sentinel
- I once met a girl whom I most wanted to take care of all my life. Later... No later
- ReturnJson,让返回数据多一些自定义数据或类名
- Cronexpression expression explanation and cases
- Talk about how the kotlin collaboration process establishes structured concurrency
- Deep Learning with Pytorch- A 60 Minute Blitz
- 【新书推荐】Cleaning Data for Effective Data Science
- JVM notes (III): analysis of JVM object creation and memory allocation mechanism
猜你喜欢

布隆过滤器

机器学习笔记 九:预测模型优化(防止欠拟合和过拟合问题发生)

MySQL knowledge summary (useful for thieves)

Solution to the eighth training competition of 2020 Provincial Games

MySQL explain

训练一个图像分类器demo in PyTorch【学习笔记】

Tutorial for beginners of small programs day01

Express の post request
Recommend a very easy-to-use network communication framework HP socket

Small program learning path 1 - getting to know small programs
随机推荐
Electron, which can wrap web page programs into desktop applications
Deeply understand the working principle of kotlin collaboration suspend (beginners can also understand it)
Express の Hello World
Solution to the sixth training competition of 2020 provincial competition
Design of mfc+mysql document data management system based on VS2010
Pass anonymous function to simplification principle
Pytorch for former Torch users - Tensors
ES6 learning path (IV) operator extension
【新书推荐】Deno Web Development
Tablet PC based ink handwriting recognition input method
Reading notes of "Introduction to deep learning: pytoch"
Small program learning path 1 - getting to know small programs
Script summary
Do you want the dialog box that pops up from the click?
MySQL index optimization miscellaneous
[shutter] solve failed assertion: line 5142 POS 12: '_ debugLocked‘: is not true.
Find the number that appears only once in the array
Talking about the difference between kotlin collaboration and thread
【Ubuntu-redis安装】
Express file upload