当前位置:网站首页>How to automatically renew a token after it expires?
How to automatically renew a token after it expires?
2022-06-30 05:36:00 【hello-java-maker】
Recommended today
Reduce try-catch , This is grace !
An addictive new generation of development artifacts , Say goodbye to Controller、Service、Dao Other methods
SpringBoot Realize face recognition function
believe me , Use Stream Can really make the code more elegant !
The most detailed thread pool in the whole network ThreadPoolExecutor Reading !
Batch splitting with multithreading List Import database , Efficiency bars !JWT token Of payload Part of it is a json strand , Is a set of declarations to pass data , These statements are JWT The standard is called claims.
JWT The standard defined in the standard claim Include :
iss(Issuser):JWT The issuing subject of ;sub(Subject):JWT Owner ;aud(Audience):JWT The recipient of ;exp(Expiration time):JWT The expiration time of ;nbf(Not Before):JWT Effective start time of ;iat(Issued at):JWT Issued on ;jti(JWT ID): yes JWT Unique identification of .
In addition to the above standard statement , We can also customize the declaration . With com.auth0 For example , The following code snippet implements the generation of a with expiration time token.
String token = JWT.create()
.withIssuer(ISSUER)
.withIssuedAt(new Date(currentTime))// The issuance of time
.withExpiresAt(new Date(currentTime + EXPIRES_IN * 1000 * 60))// Expiration time stamp
.withClaim("username", username)// Custom parameters
.sign(Algorithm.HMAC256(user.getPassword()));among :
withIssuer()Set the issuing entity ;withIssuedAt()Set issuing time ;withExpiresAt()Set expiration time stamp , The expiration time is EXPIRES_IN ( Unit second );withClaim()Set custom parameters .
JWT After setting the expiration time , It must be more than , Then the interface cannot be accessed , The user needs to log in again to get token. If you often need users to log in again , Obviously, this experience is not very good , Therefore, many applications will adopt token The scheme of automatic renewal after expiration , Only under certain conditions will users be allowed to log in again .
token Expired renewal scheme
solve token There are many different solutions to the expired renewal problem , Here are some representative examples . First, let's look at a list token programme , This scheme can be realized in addition to token Beyond renewal , It can also realize forced re login under some conditions .
single token programme

take token The expiration time is set to 15 minute ;
The front end initiates a request , Back-end verification token Is it overdue ; If expired , The front end initiates a refresh token request , The back end returns a new... For the front end token;
The front end is new token Initiate request , The request is successful ;
If you want to achieve 72 Hours , You must log in again , The back-end needs to record the login time of each user ; Every time a user requests , Check the last login date of the user , Exceed 72 Hours , Then refuse to refresh token Request , request was aborted , Go to the login page .
In addition, the back-end can also refresh records token The number of times , For example, refresh at most 50 Time , If it reaches 50 Time , Refresh is no longer allowed , User reauthorization required .
The list introduced above token The principle of the scheme is relatively simple . Now let's look at a double token programme .
double token programme
After successful login , Backend return
access_tokenandrefresh_token, The client caches these two types of data token;Use
access_tokenRequest interface resources , Successful call ; If token Overtime , The client carriesrefresh_tokencall token Refresh interface to get new dataaccess_token;The backend accepts the refresh token After request , Check
refresh_tokenIs it overdue . If expired , Refuse to refresh , After the client receives the status , Go to the landing page ; If not expired , Generate a newaccess_tokenReturn to the client .The client carries new
access_tokenCall the above resource interface again .After the client logs out or changes the password , Write off old token, send
access_tokenandrefresh_tokeninvalid , At the same time, clear the clientaccess_tokenandrefresh_toke.
Wechat website authorization is through OAuth2.0 Realized by mechanism , Double... Is also used token programme .

Wechat web page authorization scheme
After the user completes wechat authorization on the web page of the third-party application , Third party applications can obtain code( Authorization code ).code The timeout for is 10 minute , One code You can only exchange success for access_token I.e. failure .
Third party applications through code Get web authorization credentials access_token And refresh credentials refresh_token.
access_token Is the calling voucher for calling the authorization relationship interface , because access_token The period of validity (2 Hours ) Shorter , When access_token After a timeout , have access to refresh_token refresh .
refresh_token Has a long validity period (30 God ), When refresh_token After failure , User reauthorization required .
The backend implementation token Expired can also be used Redis To store token, Set up redis Expiration time of key value pair . If you find that redis Does not exist in the token The record of , explain token It's overdue .
source :toutiao.com/article/6995179162675790350
Last , Recommend a planet of my knowledge , Join now , front 100 name , It only needs 25 element that will do , Very favorable .
边栏推荐
- VFPBS在IIS下调用EXCEL遇到的Access is denied
- [note] usage model tree of the unity resource tree structure virtualizingtreeview
- SSL证书续费相关问题详解
- On line assignment of financial cost management in the 22nd spring of Western Polytechnic University [Full Score answer]
- Idea of capturing mobile terminal variant combination
- 图扑软件基于钻孔数据的三维地质模型可视化
- Unity- the camera follows the player
- [notes] unity Scrollview button page turning
- 《谁动了我的奶酪》读后感
- pytorch中常用损失函数总结
猜你喜欢

Vfpbs uploads excel and saves MSSQL to the database

9. naive Bayes

VFPBS上传EXCEL并保存MSSQL到数据库中

Use the code cloud publicholiday project to determine whether a day is a working day
![[notes] unity webgl input Chinese](/img/f7/805f510ff691227b4c2b529cc1099a.jpg)
[notes] unity webgl input Chinese
![[Motrix] download Baidu cloud files using Motrix](/img/d3/f3d29468367cf5011781f20f27a5c8.jpg)
[Motrix] download Baidu cloud files using Motrix

Pytorch的安装以及入门使用

旋转框目标检测mmrotate v0.3.1入门

Bev instance prediction based on monocular camera (iccv 2021)

Unity publishing /build settings
随机推荐
El table lazy load refresh
mmcv常用API介绍
Golden code of programmer interview
9. naive Bayes
You don't know how to deduce the location where HashSet stores elements?
Use the code cloud publicholiday project to determine whether a day is a working day
Responsive layout
[typescript] defines the return value type of promise
Summary of common loss functions in pytorch
[chestnut sugar GIS] global mapper - how to assign the elevation value of the grid to the point
Sword finger offer 22 The penultimate node in the linked list
Xi'an Jiaotong 21st autumn economics online homework answer sheet (III) [standard answer]
AI大模型落地大考,浪潮交出了怎样的答卷?
Intellj idea jars projects containing external lib to other project reference methods - jars
Unity C trigonometric function, right triangle corner calculation
Set a plane to camera viewport
D. Big Brush
E: Topic focus
Rotation, translation and scaling of unity VR objects
Unity- the camera follows the player