当前位置:网站首页>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
}
}
边栏推荐
- Every API has its foundation when a building rises from the ground
- SSO process analysis
- hydra常用命令
- What are the commonly used English words and sentences about COVID-19?
- Leetcode daily question (1870. minimum speed to arrive on time)
- 雲上有AI,讓地球科學研究更省力
- 【每日一题】729. 我的日程安排表 I
- Machine learning plant leaf recognition
- Redis Foundation
- Fedora/rehl installation semanage
猜你喜欢
Facebook AI & Oxford proposed a video transformer with "track attention" to perform SOTA in video action recognition tasks
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
[English] Grammar remodeling: the core framework of English Learning -- English rabbit learning notes (1)
漏了监控:Zabbix对Eureka instance状态监控
CS certificate fingerprint modification
Leetcode daily question (971. flip binary tree to match preorder traversal)
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
机器学习植物叶片识别
Pallet management in SAP SD delivery process
Bitcoinwin (BCW): the lending platform Celsius conceals losses of 35000 eth or insolvency
随机推荐
[English] Verb Classification of grammatical reconstruction -- English rabbit learning notes (2)
漏了监控:Zabbix对Eureka instance状态监控
18. Multi level page table and fast table
Windows Server 2016 standard installing Oracle
基于购买行为数据对超市顾客进行市场细分(RFM模型)
mysql的基础命令
After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious
Development of entity developer database application
C语言_双创建、前插,尾插,遍历,删除
Chapter 7 - thread pool of shared model
Phishing & filename inversion & Office remote template
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm
ROS学习_基础
前缀和数组系列
The registration password of day 239/300 is 8~14 alphanumeric and punctuation, and at least 2 checks are included
[brush questions] how can we correctly meet the interview?
Data security -- 13 -- data security lifecycle management
The difference between get and post request types
When my colleague went to the bathroom, I helped my product sister easily complete the BI data product and got a milk tea reward
【每日一题】729. 我的日程安排表 I