当前位置:网站首页>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 .
边栏推荐
- How to use js to control the scroll bar of moving div
- 9. naive Bayes
- 剑指 Offer 22. 链表中倒数第k个节点
- Unity shader flat shadow
- 14x1.5cm vertical label is a little difficult, VFP calls bartender to print
- Sound network, standing in the "soil" of the Internet of things
- 如何制作CSR(Certificate Signing Request)文件?
- Remote sensing image /uda:curriculum style local to global adaptation for cross domain remote sensing image segmentation
- 剑指 Offer 18. 删除链表的节点
- 旋转框目标检测mmrotate v0.3.1 学习配置
猜你喜欢

Sword finger offer 22 The penultimate node in the linked list
![[chestnut sugar GIS] global mapper - how to assign the elevation value of the grid to the point](/img/bb/ea0e78065ba54ff253995faeeb6901.png)
[chestnut sugar GIS] global mapper - how to assign the elevation value of the grid to the point

如何制作CSR(Certificate Signing Request)文件?

Solidity - 安全 - 重入攻击(Reentrancy)

Rotating box target detection mmrotate v0.3.1 getting started

86. 分隔链表
![[typescript] cannot redeclare block range variables](/img/52/2fd3071ca9e3c5023c6b65961e2cf7.jpg)
[typescript] cannot redeclare block range variables

VFPBS在IIS下调用EXCEL遇到的Access is denied

Unityshader learning notes - Basic Attributes

Delete the repeating elements in the sorting list (simple questions)
随机推荐
旋转框目标检测mmrotate v0.3.1 学习配置
E: Topic focus
Bessel curve with n control points
Introduction to mmcv common APIs
Remote sensing image /uda:curriculum style local to global adaptation for cross domain remote sensing image segmentation
Xi'an Jiaotong automation control theory test simulation question [standard answer]
Detailed explanation of the loss module of mmdet
如何制作CSR(Certificate Signing Request)文件?
强烈推荐十几款IDEA开发必备的插件
[chestnut sugar GIS] global mapper - how to assign the elevation value of the grid to the point
Set a plane to camera viewport
Codeforces Round #390 (Div. 2) D. Fedor and coupons
Question mark (?) in Cron expression Use of
3D rotation album
终端便捷ssh(免密)连接
PWN Introduction (2) stack overflow Foundation
Display steerable 3D model in front of unity UI
[note] usage model tree of the unity resource tree structure virtualizingtreeview
How to create a CSR (certificate signing request) file?
Redistemplate common method summary