当前位置:网站首页>谷歌零碎笔记之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;
}
边栏推荐
- 10道面试常问JVM题
- SSH免密登录-两台虚拟机建立免密通道 双向信任
- 【技能积累】presentation实用技巧积累,常用句式
- Shallow reading of reentrantlock source code of abstractqueuedsynchronizer (AQS)
- 王树尧老师运筹学课程笔记 07 线性规划与单纯形法(标准型、基、基解、基可行解、可行基)
- Teacher Wu Enda's machine learning course notes 03 review of linear algebra
- CVPR2022Oral专题系列(一):低光增强
- Invalid access control
- 【论文阅读 | cryoET】Gum-Net:快速准确的3D Subtomo图像对齐和平均的无监督几何匹配
- Idea cannot find a database solution
猜你喜欢

王树尧老师运筹学课程笔记 07 线性规划与单纯形法(标准型、基、基解、基可行解、可行基)

C language memory stack and heap usage

Understanding of access, hybrid and trunk modes

基于噪声伪标签和对抗性学习的医学图像分割注释有效学习

vscode通过remotessh结合xdebug远程调试php解决方案

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

Let the computer run only one program setting

联邦学习后门攻击总结(2019-2022)

Embedding understanding + code

10 frequently asked JVM questions in interviews
随机推荐
MySql基础知识(高频面试题)
The core of openresty and cosocket
【技能积累】presentation实用技巧积累,常用句式
Simulation volume leetcode [normal] 061. rotating linked list
Teacher Wu Enda's machine learning course notes 04 multiple linear regression
Teacher wangshuyao's notes on operations research course 10 linear programming and simplex method (discussion on detection number and degradation)
Summary of 2022 SQL classic interview questions (with analysis)
吴恩达老师机器学习课程笔记 01 引言
模拟卷Leetcode【普通】222. 完全二叉树的节点个数
吴恩达老师机器学习课程笔记 04 多元线性回归
Loss function -- cross entropy loss function
王树尧老师运筹学课程笔记 00 写在前面
数据库系统概述
Best example of amortized cost
DBAsql面试题
数仓建模,什么是宽表?如何设计?好处与不足
【备忘】关于ssh为什么会失败的原因总结?下次记得来找。
Teacher wangshuyao's notes on operations research 04 fundamentals of linear algebra
'function VTable for error: undefined reference to... 'cause and solution of the problem
数据单位:位、字节、字、字长