当前位置:网站首页>JWT actively checks whether the Token has expired
JWT actively checks whether the Token has expired
2022-08-04 21:08:00 【Jen Sir (Open Source Byte)】
JSON Web Token (JWT for short) is currently the most popular cross-domain authentication solution
Why do I need user authentication when the front-end and back-end are separated for development?The reason is that the HTTP protocol is stateless, which means that when we authenticate a user with an account and password, it will forget the previous data when the next request is made.So our program doesn't know who is who, and we have to verify it again.So in order to ensure system security, we need to verify whether the user is logged in.
JWT composition
JWT consists of three parts: Header, Payload, Signature, and finally spliced by .
JWT verification principle
Through the jwt generation rules explained earlier, the first two parts of jwt are the base64 encoding of the header and payload.When the server receives the token from the client, it parses the first two parts to get the header and payload, and uses the algorithm in the header to sign with the server's local private secret to determine whether it is consistent with the signature carried in jwt.
Active verification is expired
In some business scenarios of form submission, it will check whether the token is valid. If the token has expired at this time, the front end will prompt the user to log in again.For example, the Open Source Byte rental applet submits housing listings.This operation mode will cause the data input by the user to be lost, resulting in a very bad user experience. Therefore, in the rental applet, we have implemented the function of actively verifying the token. When we open the form, we will verify the token. At this time, the user does notEnter any data.In this way, the user's experience is improved.
/*** Verify that the token has expired*/public boolean isExpiration(String token) {try {Claims claims = parseToken(token);String userKey = getTokenKey(claims.get(Constants.LOGIN_USER_KEY).toString());LoginUser loginUser = redisCache.getCacheObject(userKey);long expireTime = loginUser.getExpireTime();long currentTime = System.currentTimeMillis();if (expireTime - currentTime <= 0){return true;}} catch (Exception e) {return true;}return false;}/*** Get data claim from token** @param token token* @return data declaration*/private Claims parseToken(String token){return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();}
The above shared content is free in Open Source Byte Low Code PlatformGet, go to the crowd
If reprinted, please indicate the source: Open Source Byte https://sourcebyte.cn/article/212.html
边栏推荐
- DSPE-PEG-Aldehyde,DSPE-PEG-CHO,磷脂-聚乙二醇-醛基一种疏水18碳磷脂
- 三种方式设置特定设备UWP XAML view
- Cryptography Series: PEM and PKCS7, PKCS8, PKCS12
- win10 uwp modify picture quality compress picture
- 【TypeScript】深入学习TypeScript枚举
- SAP ABAP OData 服务如何支持 $select 有选择性地仅读取部分模型字段值试读版
- js的new Function()常用方法
- 机器学习_02
- Oreo域名授权验证系统v1.0.6公益开源版本网站源码
- 3、IO流之字节流和字符流
猜你喜欢
Uniapp微信雪糕刺客单页小程序源码
【debug】postgres数据存储错乱
【C语言】指针和数组的深入理解(第三期)
Oreo domain name authorization verification system v1.0.6 public open source version website source code
Comic | Two weeks after the boss laid me off, he hired me back and doubled my salary!
使用百度EasyDL实现森林火灾预警识别
for 循环中的 ++i 与 i++
遇到MapStruct后,再也不手写PO,DTO,VO对象之间的转换了
构建Buildroot根文件系统(I.MX6ULL)
Spss-系统聚类软件实操
随机推荐
Oreo域名授权验证系统v1.0.6公益开源版本网站源码
web漏洞扫描器-awvs
密码学系列之:PEM和PKCS7,PKCS8,PKCS12
MySQL stored procedure introduction, creation, case, delete, view "recommended collection"
Common methods of js's new Function()
adb控制常用命令
【2022杭电多校5 1012题 Buy Figurines】STL的运用
Configure laravel queue method using fort app manager
构建Buildroot根文件系统(I.MX6ULL)
【1403. 非递增顺序的最小子序列】
如何用好建造者模式
dotnet enables JIT multi-core compilation to improve startup performance
Big capital has begun to flee the crypto space?
【2022牛客多校5 A题 Don‘t Starve】DP
vim clear last search highlighting
js数据类型、节流/防抖、点击事件委派优化、过渡动画
Three ways to set a specific device UWP XAML view
数据仓库(1)什么是数据仓库,数仓有什么特点
dotnet compress Stream or file using lz4net
How to make good use of builder mode