当前位置:网站首页>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
}
}
边栏推荐
- Monotonic stack
- Office doc add in - Online CS
- [ 英语 ] 语法重塑 之 动词分类 —— 英语兔学习笔记(2)
- Pymongo gets a list of data
- Attributeerror successfully resolved: can only use cat accessor with a ‘category‘ dtype
- 成功解决TypeError: data type ‘category‘ not understood
- ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用Shap值对XGBoost模型实现可解释性案例之详细攻略
- Due to high network costs, arbitrum Odyssey activities are suspended, and nitro release is imminent
- Do you really know the use of idea?
- hydra常用命令
猜你喜欢
What is the difference between int (1) and int (10)? Senior developers can't tell!
Lesson 7 tensorflow realizes convolutional neural network
Windows Server 2016 standard installing Oracle
Leetcode daily question (971. flip binary tree to match preorder traversal)
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm
Simple use of MySQL database: add, delete, modify and query
医疗软件检测机构怎么找,一航软件测评是专家
机器人类专业不同层次院校课程差异性简述-ROS1/ROS2-
My seven years with NLP
Attributeerror: can 't get attribute' sppf 'on < module' models. Common 'from' / home / yolov5 / Models / comm
随机推荐
pymongo获取一列数据
雲上有AI,讓地球科學研究更省力
A method to measure the similarity of time series: from Euclidean distance to DTW and its variants
《从0到1:CTFer成长之路》书籍配套题目(周更)
C language_ Double create, pre insert, post insert, traverse, delete
Pallet management in SAP SD delivery process
What are the commonly used English words and sentences about COVID-19?
编译,连接 -- 笔记 -2
(practice C language every day) reverse linked list II
Machine learning plant leaf recognition
攻防世界 MISC中reverseMe简述
CS certificate fingerprint modification
Facebook AI & Oxford proposed a video transformer with "track attention" to perform SOTA in video action recognition tasks
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
PCL realizes frame selection and clipping point cloud
中青看点阅读新闻
SAP SD发货流程中托盘的管理
【软件测试进阶第1步】自动化测试基础知识
P5706 [deep foundation 2. Example 8] redistributing fat house water -- February 13, 2022
Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation