当前位置:网站首页>JWT快速入门
JWT快速入门
2022-07-26 07:28:00 【STATICHIT静砸】
· 什么是JWT?
JSON Web Token。通过数字签名的方式,以JSON对象为载体,在不同的服务器终端之间安全的传输信息。
· JWT有什么用?
JWT最常见的场景就是授权认证,一旦用户登录,后续每个请求都将包含JWT,系统在每次处理用户请求的之前,都要先进行JWT安全校验,通过之后再进行处理。
· JWT的组成:
JWT由3部分组成,并用 . 拼接
例如:

这三部分分别是(例如):
· Header

· Payload

· Signatrue

首先,要实现jwt还需要导入一些包(下面省略了json)
<!--JWT-->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>实践:创建token和提取token中的数据

import io.jsonwebtoken.*;
import java.util.Date;
import java.util.UUID;
public class JWT {
private long time = 1000*60*60*24;
private String signature = "admin";
@org.junit.Test
public void makeJWT(){
JwtBuilder jwtBuilder = Jwts.builder();
String jwtToken = jwtBuilder
//header
.setHeaderParam("typ","JWT")
.setHeaderParam("alg","HS256")
//payload
.claim("username","tom")
.claim("role","admin")
.setSubject("admin-test")
.setExpiration(new Date(System.currentTimeMillis()+time))
.setId(UUID.randomUUID().toString())
//signature
.signWith(SignatureAlgorithm.HS256,signature)
.compact();
System.out.println(jwtToken);
}
@org.junit.Test
public void parse(){
String token ="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6InRvbSIsInJvbGUiOiJhZG1pbiIsInN1YiI6ImFkbWluLXRlc3QiLCJleHAiOjE2NTg4MjMyODEsImp0aSI6ImE1Y2M3Y2IzLTM4NTItNGJmYS05NjAyLTllYjdjNTY2MjY5NCJ9.WOP5_VomcD_Ds1fgRlydAGPWIHBWtoiPqPJLxR93Q6o";
JwtParser jwtParser = Jwts.parser();
Jws<Claims> claimsJws = jwtParser.setSigningKey(signature).parseClaimsJws(token);
Claims claims = claimsJws.getBody();
System.out.println(claims.get("username"));
System.out.println(claims.get("role"));
System.out.println(claims.getId());
System.out.println(claims.getSubject());
System.out.println(claims.getExpiration());
}
}
第一个和第二个测试分别得到的结果是:
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6InRvbSIsInJvbGUiOiJhZG1
pbiIsInN1YiI6ImFkbWluLXRlc3QiLCJleHAiOjE2NTg4MjMyODEsImp0aSI6ImE1Y2M3Y2IzL
TM4NTItNGJmYS05NjAyLTllYjdjNTY2MjY5NCJ9.WOP5_VomcD_Ds1fgRlydAGPWIHBWto
iPqPJLxR93Q6o

边栏推荐
猜你喜欢

Pycharm的相关配置:改字体样式和大小、更改图片背景、更改控制台输出的字体颜色

NFT数字藏品开发:数字藏品助力企业发展

Configure flask

In July, glassnode data showed that the open position of eth perpetual futures contract on deribit had just reached a one month high of $237959827.

Kdd2022 | uncover the mystery of Kwai short video recommendation re ranking, and recommend the new SOTA

PostgreSQL sequence create alter nextval Curval numerical interval gap

WCF 入门教程二

程序环境和预处理

WCF introductory tutorial II

深度学习模型部署
随机推荐
NFT数字藏品系统开发:NFT数藏 的最佳数字营销策略有哪些
NFT数字藏品开发:数字藏品与NFT的六大区别
NFT数字藏品系统开发:企业如何开发属于自己的数藏平台
PXE高效批量网络装机
NFT数字藏品开发:数字艺术藏品赋能公益平台
China Unicom transformed the Apache dolphin scheduler resource center to realize the one-stop access of cross cluster call and data script of billing environment
6、MySQL数据库的备份与恢复
DADNN: Multi-Scene CTR Prediction via Domain-Aware Deep Neural Network
PXE efficient batch network installation
3.0.0 alpha blockbuster release! Nine new functions and new UI unlock new capabilities of dispatching system
PG operation and maintenance -- logical backup and physical backup practice
成为 Apache 贡献者,So easy!
Pycharm common shortcut keys
NFT digital collection system development: Huawei releases the first collector's digital collection
PR subtitle production
C# 使用Log4Net记录日志(基础篇)
Unity3d asynchronous loading of scenes and progress bar loading
Compose Canvas line chart
Network Trimming: A Data-Driven Neuron Pruning Approach towards Efficient Deep Architectures论文翻译/笔记
【推荐系统经典论文(十)】阿里SDM模型