当前位置:网站首页>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

边栏推荐
- Learning Efficient Convolutional Networks Through Network Slimming
- PXE高效批量网络装机
- How to convert multi row data into multi column data in MySQL
- It's another summer of open source. 12000 project bonuses are waiting for you!
- MMOE多目标建模
- MySQL installation tutorial - hands on installation
- NLP自然语言处理-机器学习和自然语言处理介绍(三)
- 此章节用于补充
- Compose text and icon splicing to realize drawableleft or drawableright
- ModuleNotFoundError: No module named ‘pip‘解决办法
猜你喜欢

PXE efficient batch network installation

3.0.0 alpha 重磅发布!九大新功能、全新 UI 解锁调度系统新能力

7月消息,Glassnode数据显示,Deribit上ETH永续期货合约未平仓头寸刚刚达到一个月高点237,959,827美元。

VScode无法启动问题解决思路

NFT digital collection system development: digital collections give new vitality to brands

正则表达式规则以及常用的正则表达式

Compose Canvas line chart

NLP自然语言处理-机器学习和自然语言处理介绍(三)

6、MySQL数据库的备份与恢复

基于Thinkphp的开源管理系统
随机推荐
6、MySQL数据库的备份与恢复
DevExpress.XtraEditors.DataNavigator用法
Apache dolphin scheduler & tidb joint meetup | focus on application development capabilities under the development of open source ecosystem
QT: list box, table, tree control
NFT digital collection development: digital collections help enterprise development
keras学习之:获取神经网络中间层的输出结果
IDEA快捷键
4. Data integrity
hot100 哈希
【Keras入门日志(3)】Keras中的序贯(Sequential)模型与函数式(Functional)模型
HCIP---BGP综合实验
WCF introductory tutorial II
ShardingSphere数据分片
Examples of financial tasks: real-time and offline approval of three scenarios and five optimizations of Apache dolphin scheduler in Xinwang bank
TensorFlow学习日记之tflearn
系统架构&微服务
tensorflow2.x中的量化感知训练以及tflite的x86端测评
程序环境和预处理
Compose canvas custom circular progress bar
Regular expression rules and common regular expressions