当前位置:网站首页>Google fragmented notes JWT (Draft)
Google fragmented notes JWT (Draft)
2022-07-29 07:00:00 【Novice Google notes】
Introduce dependencies
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
Go straight to the code
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
private String getJWT(UserModel po,long ttlMillis){
// Specify the signature algorithm to use when signing , That is to say header The part ,jjwt This part has been encapsulated .
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
// Generate JWT Time for
long nowMillis = System.currentTimeMillis();
Date now = new Date(nowMillis);
// establish payload Private statement of ( Add... Based on specific business needs , If you want to verify this , It's usually need and jwt The receiver has communicated the verification method well in advance )
Map<String, Object> claims = new HashMap<String, Object>();
claims.put("username", po.getUserName());
claims.put("password", po.getPassword());
// The secret key used when generating the signature secret, This method is encapsulated locally , Generally, it can be read from the local configuration file , Remember that this secret key cannot be exposed . It is the private key of your server , It should not be revealed in any scene . Once the client knows this secret, That means that the client can issue itself jwt 了 .
String key = "mySecret";
// Generate signer
String subject = "guy";
// Here's why payload Added various standard and private statements
String compactJWT = Jwts.builder()
// header head
.setHeaderParam("type", "JWT")
.setHeaderParam("alg", "HS256")
// payload load
.claim("userName", po.getUserName())
.claim("role", "admin")
.setSubject("title")
.setExpiration(new Date(nowMillis + ttlMillis))
.setId(UUID.randomUUID().toString())
// signature Signature
.signWith(signatureAlgorithm, sugnature)
.compact();
return compactJWT;
}
边栏推荐
- JVM之垃圾回收机制(GC)
- 关于SQL Server语句入门级应用阶段性学习——找工作必备(一)
- N2 interface of 5g control plane protocol
- 微信小程序的反编译
- 王树尧老师运筹学课程笔记 04 线性代数基础
- SDN topology discovery principle
- Database multi table query joint query add delete modify query
- vim文本编辑器的一些使用小技巧
- Leetcode-592: fraction addition and subtraction
- Teacher wangshuyao's notes on operations research 05 linear programming and simplex method (concept, modeling, standard type)
猜你喜欢

Shallow reading of reentrantlock source code of abstractqueuedsynchronizer (AQS)

竣达技术 | 适用于”日月元”品牌UPS微信云监控卡

【冷冻电镜】RELION4.0之subtomogram对位功能源码分析(自用)

JVM之垃圾回收机制(GC)

N2 interface of 5g control plane protocol

Teacher wangshuyao's notes on operations research 04 fundamentals of linear algebra

IO流 - File - properties

Teacher wangshuyao's operations research course notes 07 linear programming and simplex method (standard form, base, base solution, base feasible solution, feasible base)

Actual combat! Talk about how to solve the deep paging problem of MySQL

Apisik health check test
随机推荐
Relative date used by filter in salesforce
【冷冻电镜】RELION4.0 pipeline命令总结(自用)
基于Matlab解决线性规划问题
量子机器学习中的安全性问题
Federal learning backdoor attack summary (2019-2022)
王树尧老师运筹学课程笔记 04 线性代数基础
leetcode-592:分数加减运算
Jetpack Compose 中的键盘处理
【笔记】The art of research - (讲好故事和论点)
Unity免费元素特效推荐
leetcode-1331:数组序号转换
mysql可以定时导出表格吗?
王树尧老师运筹学课程笔记 08 线性规划与单纯形法(单纯形法)
Teacher Wu Enda's machine learning course notes 04 multiple linear regression
HJ37 统计每个月兔子的总数 斐波那契数列
模拟卷Leetcode【普通】093. 复原 IP 地址
[CF1054H] Epic Convolution——数论,卷积,任意模数NTT
1172. 餐盘栈 有序列表+栈
Simulation volume leetcode [ordinary] 172. Zero after factorial
Junda technology | applicable to "riyueyuan" brand ups wechat cloud monitoring card