当前位置:网站首页>JWT quick start
JWT quick start
2022-07-26 07:29:00 【Statichit static smash】
· What is? JWT?
JSON Web Token. By means of digital signature , With JSON The object is the carrier , Secure transmission of information between different server terminals .
· JWT What's the usage? ?
JWT The most common scenario is authorization , Once the user logs in , Each subsequent request will contain JWT, Before each user request is processed by the system , You have to do it first JWT Security check , Process after passing .
· JWT The composition of :
JWT from 3 Part of it is made up of , And use . Splicing
for example :

These are the three parts ( for example ):
· Header

· Payload

· Signatrue

First , To achieve jwt You also need to import some packages ( The following is omitted 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>practice : establish token And extraction token Data in

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());
}
}
The results of the first and second tests are :
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6InRvbSIsInJvbGUiOiJhZG1
pbiIsInN1YiI6ImFkbWluLXRlc3QiLCJleHAiOjE2NTg4MjMyODEsImp0aSI6ImE1Y2M3Y2IzL
TM4NTItNGJmYS05NjAyLTllYjdjNTY2MjY5NCJ9.WOP5_VomcD_Ds1fgRlydAGPWIHBWto
iPqPJLxR93Q6o

边栏推荐
- Compose canvas custom circular progress bar
- OVSDB
- C51 and MDK coexist keil5 installation tutorial
- 模型剪枝三:Learning Structured Sparsity in Deep Neural Networks
- 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
- mysql语法(二)(纯语法)
- TensorFlow学习日记之tflearn
- MySQL installation tutorial - hands on installation
- WCF 部署在IIS上
- Singles cup web WP
猜你喜欢

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

NFT digital collection development: digital collections help enterprise development

Taishan office lecture: word error about inconsistent values of page margins

Machine learning related competition website

dcn(deep cross network)三部曲

Anaconda 中安装 百度飞浆Paddle 深度学习框架 教程

MMOE multi-objective modeling

Speech at 2021 global machine learning conference
![[C language] do you really know printf? (printf is typically error prone, and collection is strongly recommended)](/img/59/cf43b7dd16c203b4f31c1591615955.jpg)
[C language] do you really know printf? (printf is typically error prone, and collection is strongly recommended)

Upgrade ecological proposition: what has Alibaba cloud brought to thousands of businesses?
随机推荐
6、组合数据类型
NFT数字藏品开发:数字藏品助力企业发展
College degree sales career, from the third tier 4K to the first tier 20k+, I am very satisfied with myself
Open source management system based on ThinkPHP
机器学习相关比赛网站
2021全球机器学习大会演讲稿
Machine learning related competition website
基于Thinkphp的开源管理系统
[keras entry log (3)] sequential model and functional model in keras
QT: list box, table, tree control
Apache dolphin scheduler & tidb joint meetup | focus on application development capabilities under the development of open source ecosystem
July training (day 18) - tree
[200 opencv routines] 231. Gray level co-occurrence matrix (GLCM) for feature description
Uncover the mystery of cloud native data management: operation level
TensorFlow学习日记之tflearn
4、数据的完整性
Typora免费版下载安装
6、MySQL数据库的备份与恢复
Model pruning 3: learning structured sparsity in deep neural networks
「论文笔记」Next-item Recommendations in Short Sessions