首先在pom文件中引入json依赖:
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
public class Test {
private Long time = 1000*60*60*24 ;
private String signature = "admin";
@org. junit.Test
public void jwt(){
JwtBuilder jwtBuilder = Jwts . builder();
String jwtToken = jwtBuilder
/ /header
. setHeaderParam( s: "typ", 0: "JWT")
. setHeaderParam( s: "alg", o: "HS256")
/ /payload
.claim( s: "username", 0: "tom" )
.cLaim( S: "role", o: "admin")
. setSubject("admin-test")
. setExpiration(new Date(System . currentTimeMillis()+time))
. setId(UUID. randomUUID(). toString())
/ / signature
. signWith (S ignatureAlgorithm . HS256, signature)
, compact();
System . out. printLn(jwtToken);
<dependency>
<groupId>javax . xml. bind</groupId>
<artifactId> jaxb-api</artifactId>
<version>2.3.0</ version>
</ dependency>
<dependency>
<groupId>com . sun. xmL. bind< /groupId>
<artifactId> jaxb- impl</artifactId>
<version>2.3.0</version>
</ dependency>
<dependency>
<groupId>com . sun. xmZ. bind< /groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
</ dependency>
<dependency>
<groupId> javax . activation< /groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</ dependency>