当前位置:网站首页>谷歌零碎笔记之JWT(草稿)
谷歌零碎笔记之JWT(草稿)
2022-07-29 05:47:00 【菜鸟谷歌的笔记】
引入依赖
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
直接上代码
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
private String getJWT(UserModel po,long ttlMillis){
// 指定签名的时候使用的签名算法,也就是header那部分,jjwt已经将这部分内容封装好了。
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
// 生成JWT的时间
long nowMillis = System.currentTimeMillis();
Date now = new Date(nowMillis);
// 创建payload的私有声明(根据特定的业务需要添加,如果要拿这个做验证,一般是需要和jwt的接收方提前沟通好验证方式的)
Map<String, Object> claims = new HashMap<String, Object>();
claims.put("username", po.getUserName());
claims.put("password", po.getPassword());
//生成签名的时候使用的秘钥secret,这个方法本地封装了的,一般可以从本地配置文件中读取,切记这个秘钥不能外露哦。它就是你服务端的私钥,在任何场景都不应该流露出去。一旦客户端得知这个secret, 那就意味着客户端是可以自我签发jwt了。
String key = "mySecret";
//生成签发人
String subject = "guy";
//下面就是在为payload添加各种标准声明和私有声明了
String compactJWT = Jwts.builder()
// header头
.setHeaderParam("type", "JWT")
.setHeaderParam("alg", "HS256")
// payload荷载
.claim("userName", po.getUserName())
.claim("role", "admin")
.setSubject("title")
.setExpiration(new Date(nowMillis + ttlMillis))
.setId(UUID.randomUUID().toString())
// signature签名
.signWith(signatureAlgorithm, sugnature)
.compact();
return compactJWT;
}
边栏推荐
- Not so simple singleton mode
- Embedding understanding + code
- The core of openresty and cosocket
- Salesforce中过滤器Filter使用的相对日期
- Share some tips for better code, smooth coding and improve efficiency
- Teacher wangshuyao's notes on operations research course 10 linear programming and simplex method (discussion on detection number and degradation)
- Simulation volume leetcode [normal] 081. Search rotation sort array II
- 【CryoEM】FSC, Fourier Shell Correlation简介
- LDAP brief description and unified authentication description
- C language memory stack and heap usage
猜你喜欢
【论文阅读 | cryoET】Gum-Net:快速准确的3D Subtomo图像对齐和平均的无监督几何匹配
线程同步—— 生产者与消费者、龟兔赛跑、双线程打印
Hongke shares | testing and verifying complex FPGA design (2) -- how to perform global oriented simulation in IP core
Understanding of access, hybrid and trunk modes
数据单位:位、字节、字、字长
NeuralCF-神经协同过滤网络
Embedding understanding + code
Teacher wangshuyao's operations research course notes 07 linear programming and simplex method (standard form, base, base solution, base feasible solution, feasible base)
【经验】通过跳板机远程连接内网服务器的相关配置
Idea cannot find a database solution
随机推荐
Federal learning backdoor attack summary (2019-2022)
CNN convolutional neural network
The difference between pairs and ipairs
二次元卡通渲染——进阶技巧
Windows 上 php 7.4 连接 oracle 配置
微信小程序的反编译
Summary of 2022 SQL classic interview questions (with analysis)
Teacher wangshuyao's notes on operations research 02 fundamentals of advanced mathematics
模拟卷Leetcode【普通】222. 完全二叉树的节点个数
vim文本编辑器的一些使用小技巧
数据库使用psql及jdbc进行远程连接,不定时自动断开的解决办法
Instruction rearrangement under multithreading concurrency
Teacher wangshuyao's notes on operations research 01 guidance and introduction
JMM memory model concept
CVPR2022Oral专题系列(一):低光增强
'function VTable for error: undefined reference to... 'cause and solution of the problem
Shallow reading of condition object source code
王树尧老师运筹学课程笔记 06 线性规划与单纯形法(几何意义)
2022年SQL经典面试题总结(带解析)
Ping principle