当前位置:网站首页>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
}
}
边栏推荐
- Day 245/300 JS foreach data cannot be updated to the object after multi-layer nesting
- Attributeerror successfully resolved: can only use cat accessor with a ‘category‘ dtype
- 18.多级页表与快表
- librosa音频处理教程
- Erreur de type résolue avec succès: type de données « catégorie» non sous - jacente
- Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
- ROS learning_ Basics
- C语言_双创建、前插,尾插,遍历,删除
- 接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
- Basic commands of MySQL
猜你喜欢

接口自动化测试实践指导(上):接口自动化需要做哪些准备工作

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

《从0到1:CTFer成长之路》书籍配套题目(周更)

Machine learning plant leaf recognition

(practice C language every day) reverse linked list II

A brief introduction of reverseme in misc in the world of attack and defense

SQL Server manager studio(SSMS)安装教程

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

My creation anniversary

What are the characteristics of trademark translation and how to translate it?
随机推荐
医疗软件检测机构怎么找,一航软件测评是专家
Introduction and underlying analysis of regular expressions
Leetcode daily question (1997. first day where you have been in all the rooms)
Day 248/300 thoughts on how graduates find jobs
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
CS certificate fingerprint modification
SSO process analysis
女生学软件测试难不难 入门门槛低,学起来还是比较简单的
Biomedical English contract translation, characteristics of Vocabulary Translation
Office doc add in - Online CS
18.多级页表与快表
RichView TRVStyle 模板样式的设置与使用
同事上了个厕所,我帮产品妹子轻松完成BI数据产品顺便得到奶茶奖励
Data security -- 13 -- data security lifecycle management
PCL实现选框裁剪点云
[advanced software testing step 1] basic knowledge of automated testing
[ 英语 ] 语法重塑 之 动词分类 —— 英语兔学习笔记(2)
Market segmentation of supermarket customers based on purchase behavior data (RFM model)
How effective is the Chinese-English translation of international economic and trade contracts
Simple query cost estimation