当前位置:网站首页>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 239/300 注册密码长度为8~14个字母数字以及标点符号至少包含2种校验
- Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
- 因高额网络费用,Arbitrum 奥德赛活动暂停,Nitro 发行迫在眉睫
- 26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...
- [ 英语 ] 语法重塑 之 英语学习的核心框架 —— 英语兔学习笔记(1)
- 同事上了个厕所,我帮产品妹子轻松完成BI数据产品顺便得到奶茶奖励
- Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
- AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
- CS passed (cdn+ certificate) PowerShell online detailed version
- 18.多级页表与快表
猜你喜欢
How to find a medical software testing institution? First flight software evaluation is an expert
Office doc add in - Online CS
SAP SD发货流程中托盘的管理
18.多级页表与快表
女生学软件测试难不难 入门门槛低,学起来还是比较简单的
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
SQL Server manager studio(SSMS)安装教程
[brush questions] how can we correctly meet the interview?
L'Ia dans les nuages rend la recherche géoscientifique plus facile
随机推荐
指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
Day 245/300 JS foreach data cannot be updated to the object after multi-layer nesting
Basic commands of MySQL
What are the characteristics of trademark translation and how to translate it?
基于PyTorch和Fast RCNN快速实现目标识别
Day 245/300 JS forEach 多层嵌套后数据无法更新到对象中
漏了监控:Zabbix对Eureka instance状态监控
Attributeerror successfully resolved: can only use cat accessor with a ‘category‘ dtype
Biomedical English contract translation, characteristics of Vocabulary Translation
中青看点阅读新闻
ROS learning_ Basics
LeetCode - 152 乘积最大子数组
After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用Shap值对XGBoost模型实现可解释性案例之详细攻略
My seven years with NLP
How effective is the Chinese-English translation of international economic and trade contracts
Reflex WMS中阶系列3:显示已发货可换组
【Hot100】739. Daily temperature
Automated test environment configuration
Every API has its foundation when a building rises from the ground