当前位置:网站首页>Simple use of JWT
Simple use of JWT
2022-07-06 06:53:00 【sky~】
rely on
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.18.2</version>
</dependency>
Use
package com.sky;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Calendar;
import java.util.HashMap;
@SpringBootTest
class DemoApplicationTests {
@Test
void contextLoads() {
HashMap<String,Object> map = new HashMap<>(); // The head is HashMap type
// Set expiration time
Calendar instance = Calendar.getInstance();
instance.add(Calendar.SECOND,1000);// Set expiration time to 1000 second Calendar.SECOND second Calendar.DATE God ...
// form JWT
JWTCreator.Builder builder= JWT.create();
builder.withHeader(map) // head header Can not add , There will be one by default MAP
.withClaim("userId",1) //payload , Multiple groups can be used , If there are more than one , You can deposit it first MAP aggregate , Last foreach Traversal read
.withClaim("userName","test") //payload
.withExpiresAt(instance.getTime()); // Set token expiration time
String token =builder.sign(Algorithm.HMAC256("[email protected]##$$%@!")); // Signature
System.out.println(token); // Get the generated Token
}
@Test
public void test(){
JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256("[email protected]##$$%@!")).build(); // Based on the signature JWT Verify the object
// Verify the generated above Token, Here, if you only verify whether it is legal , You can no longer obtain the following information after execution if it is illegal , There will be anomalies
DecodedJWT verify = jwtVerifier.verify("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyTmFtZSI6InRlc3QiLCJleHAiOjE2NDQ3MTc5NDYsInVzZXJJZCI6MX0.r8JiKm10KU7IMfe17JU5-2N_cEwdUQhwl2l84zP7oO4");
// Get some verified information
System.out.println(verify.getExpiresAt()); // Get expiration time
System.out.println(verify.getClaim("userId").asInt());// obtain payload Note that the type corresponds to the above one by one
System.out.println(verify.getClaim("userName").asString());// obtain payload
}
}
边栏推荐
- Facebook AI & Oxford proposed a video transformer with "track attention" to perform SOTA in video action recognition tasks
- Automated test environment configuration
- 基于PyTorch和Fast RCNN快速实现目标识别
- C语言_双创建、前插,尾插,遍历,删除
- How to convert flv file to MP4 file? A simple solution
- The difference between get and post request types
- 【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
- Do you really know the use of idea?
- How effective is the Chinese-English translation of international economic and trade contracts
- How to translate professional papers and write English abstracts better
猜你喜欢

云上有AI,让地球科学研究更省力

How to convert flv file to MP4 file? A simple solution

Apache dolphin scheduler source code analysis (super detailed)

Pallet management in SAP SD delivery process

雲上有AI,讓地球科學研究更省力

What are the commonly used English words and sentences about COVID-19?

SQL Server Manager studio (SSMS) installation tutorial

Due to high network costs, arbitrum Odyssey activities are suspended, and nitro release is imminent

Leetcode daily question (971. flip binary tree to match preorder traversal)

【每日一题】729. 我的日程安排表 I
随机推荐
接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
What are the commonly used English words and sentences about COVID-19?
At the age of 26, I changed my career from finance to software testing. After four years of precipitation, I have been a 25K Test Development Engineer
成功解决TypeError: data type ‘category‘ not understood
How effective is the Chinese-English translation of international economic and trade contracts
Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
基于PyTorch和Fast RCNN快速实现目标识别
UDP攻击是什么意思?UDP攻击防范措施
Use shortcut LNK online CS
Map of mL: Based on the adult census income two classification prediction data set (whether the predicted annual income exceeds 50K), use the map value to realize the interpretable case of xgboost mod
《从0到1:CTFer成长之路》书籍配套题目(周更)
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
My creation anniversary
Attributeerror successfully resolved: can only use cat accessor with a ‘category‘ dtype
简单描述 MySQL 中,索引,主键,唯一索引,联合索引 的区别,对数据库的性能有什么影响(从读写两方面)
【Hot100】739. 每日溫度
E-book CHM online CS
Redis Foundation
Bitcoinwin (BCW): the lending platform Celsius conceals losses of 35000 eth or insolvency