当前位置:网站首页>谷歌零碎笔记之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;
}
边栏推荐
- 模拟卷Leetcode【普通】081. 搜索旋转排序数组 II
- The difference between pairs and ipairs
- 剑指 Offer II 115:重建序列
- Shallow reading of condition object source code
- 模拟卷Leetcode【普通】172. 阶乘后的零
- 【技能积累】写邮件时的常用表达
- Jetpack Compose 中的键盘处理
- C language memory stack and heap usage
- [CF1054H] Epic Convolution——数论,卷积,任意模数NTT
- Talk about tcp/ip protocol? And the role of each layer?
猜你喜欢

vim文本编辑器的一些使用小技巧

Recurrent neural network RNN

CNN convolutional neural network

线程同步—— 生产者与消费者、龟兔赛跑、双线程打印

阿里一面,给了几条SQL,问需要执行几次树搜索操作?

Software definition boundary SDP

10道面试常问JVM题

Ali gave several SQL messages and asked how many tree search operations need to be performed?

【flask入门系列】Flask-SQLAlchemy的安装与配置

Neuralcf neural collaborative filtering network
随机推荐
SDN topology discovery principle
【笔记】The art of research(明白问题的重要性)
王树尧老师运筹学课程笔记 08 线性规划与单纯形法(单纯形法)
Unity免费元素特效推荐
Teacher wangshuyao's notes on operations research 05 linear programming and simplex method (concept, modeling, standard type)
吴恩达老师机器学习课程笔记 03 线性代数回顾
vscode通过remotessh结合xdebug远程调试php解决方案
【干货备忘】50种Matplotlib科研论文绘图合集,含代码实现
Shallow reading of reentrantlock source code of abstractqueuedsynchronizer (AQS)
【flask入门系列】Flask-SQLAlchemy的安装与配置
Teacher Wu Enda machine learning course notes 05 octave tutorial
数据单位:位、字节、字、字长
2022年SQL经典面试题总结(带解析)
MySQL:当你CRUD时BufferPool中发生了什么?十张图就能说清楚
崔雪婷老师最优化理论与方法课程笔记 00 写在前面
【冷冻电镜入门】加州理工公开课课程笔记 Part 3: Image Formation
Federal learning backdoor attack summary (2019-2022)
Dbasql interview questions
Enterprise manager cannot connect to the database instance in Oracle10g solution
循环神经网络RNN