这个是springBoot项目, 如果不是springboot可以不加 springboot的相关依赖(第一个web和第二个test)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl -->
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-core -->
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.activation/activation -->
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
一下是 代码:
package coom.hjjt.jwt.jwt;
import io.jsonwebtoken.*;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
@SpringBootTest
public class TestJwt {
long time = 1000 * 60 * 60 * 24; //当前的时间撮,加上一天的时间,设置为一天的有效期
private String signature = "admin"; //自定签名的加密 盐
@Test
public void jwt(){
JwtBuilder jwtBuilder = Jwts.builder();
//有三部分组成,
//头信息 Header{'type': 'JWT', 'alg': 'HS256'}
//载荷 Payload {'sub':'123123123','name':'john','admin': true}
//签名 Signature: var encodedString = base64UrlEncode(header) +"."+base64UrlEncode(payload)
// var sigantur = HMACSHA256(encodeedString, 'secret')
String jwtToken = jwtBuilder
//头信息
.setHeaderParam("type", "HJJT")
.setHeaderParam("alg", "HS256")
//payload 载荷
.claim("username", "eam")
.claim("role", "admin")
.setSubject("admin-hjjt") //加默认信息 主题 和 有效时间(可以自定义)
.setExpiration(new Date(System.currentTimeMillis() + time )) //当前的时间撮,加上一天的时间,设置为一天的有效期
.setId(UUID.randomUUID().toString())
//签名 signature
.signWith(SignatureAlgorithm.HS256, signature)
.compact();
System.out.println(jwtToken);
}
@Test
public void parse(){
// String token = "eyJ0eXBlIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJ1c2VybmFtZSI6InRvbSIsInJvbGUiOiJhZG1pbiIsInN1YiI6ImFkbWluLXRlc3QiLCJleHAiOjE2NDczMTE5NjcsImp0aSI6ImQxYTkyMmRlLTlhOTItNGZiYy1iZDkyLTAxNTJlMmQ1YzM4OCJ9.0X4ypA0xCJ7nN2-p46G_CguZIc1BzhXU8mXNy8SWUx8";
// String token = "eyJ0eXBlIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJ1c2VybmFtZSI6InRvbSIsInJvbGUiOiJhZG1pbiIsInN1YiI6ImFkbWluLXRlc3QiLCJleHAiOjE2NDczMTI1NDAsImp0aSI6IjU2ZjdhMzVhLTY4YWUtNDcwMC05NjllLTY2ZjY4YTk4OGM3MyJ9.k6aLo_zJGqAX0oF85aeCsb4zuF6GWXt4eFTsEutomLY";
String token = "eyJ0eXBlIjoiSEpKVCIsImFsZyI6IkhTMjU2In0.eyJ1c2VybmFtZSI6ImVhbSIsInJvbGUiOiJhZG1pbiIsInN1YiI6ImFkbWluLWhqanQiLCJleHAiOjE2NDczMTI3MzQsImp0aSI6IjJiM2RiMTViLWVhODYtNGZlOC1iYTVjLWMyNWE2MWQ4OTg4ZiJ9.DJhPC32NqXAPQYW-Wvb0Bcc_ASIOiOaPMTiYk4KRadU";
JwtParser jwtParser = Jwts.parser();
Jws<Claims> claimsJws = jwtParser.setSigningKey(signature).parseClaimsJws(token);
Claims claims = claimsJws.getBody();
System.out.println("用户名:" + claims.get("username"));
System.out.println("角色:" + claims.get("role"));
System.out.println(claims.getId() + "是ID");
System.out.println(claims.getSubject() + "是签名");
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(claims.getExpiration())+ "是有效时间");
}
}