当前位置:网站首页>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
}
}
边栏推荐
- 【软件测试进阶第1步】自动化测试基础知识
- [unity] how to export FBX in untiy
- L'Ia dans les nuages rend la recherche géoscientifique plus facile
- Briefly describe the differences between indexes, primary keys, unique indexes, and joint indexes in mysql, and how they affect the performance of the database (in terms of reading and writing)
- Day 245/300 JS forEach 多层嵌套后数据无法更新到对象中
- Automated test environment configuration
- [daily question] 729 My schedule I
- 攻防世界 MISC中reverseMe简述
- Windows Server 2016 standard installing Oracle
- CS certificate fingerprint modification
猜你喜欢
Do you really know the use of idea?
E-book CHM online CS
《从0到1:CTFer成长之路》书籍配套题目(周更)
Fedora/REHL 安装 semanage
C language_ Double create, pre insert, post insert, traverse, delete
LeetCode每日一题(971. Flip Binary Tree To Match Preorder Traversal)
Misc of BUU (update from time to time)
A brief introduction of reverseme in misc in the world of attack and defense
机器学习植物叶片识别
mysql的基础命令
随机推荐
Office doc add in - Online CS
Day 245/300 JS forEach 多层嵌套后数据无法更新到对象中
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm
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
Day 248/300 thoughts on how graduates find jobs
Lesson 7 tensorflow realizes convolutional neural network
E-book CHM online CS
Day 248/300 关于毕业生如何找工作的思考
机器学习植物叶片识别
The registration password of day 239/300 is 8~14 alphanumeric and punctuation, and at least 2 checks are included
Biomedical English contract translation, characteristics of Vocabulary Translation
Monotonic stack
[Yu Yue education] flower cultivation reference materials of Weifang Vocational College
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
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
我的创作纪念日
Simple query cost estimation
Due to high network costs, arbitrum Odyssey activities are suspended, and nitro release is imminent
19. Actual memory management of segment page combination