实践-jjwt单元测试

JJWT

之前有介绍过JWT的基本了解,本章内容主要是用来实际运行下jjwt的api,完成相应的单元测试。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<properties>
<junit.version>4.12</junit.version>
<jjwt.version>0.10.5</jjwt.version>
<lmbok.version>1.18.4</lmbok.version>
<h2.version>1.4.197</h2.version>
<slf4j.version>1.7.25</slf4j.version>
<logback.version>1.2.3</logback.version>
<!-- pom文件需要指定打包编码集,[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent! -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>${jjwt.version}</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>${jjwt.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>${jjwt.version}</version>
<scope>runtime</scope>
</dependency>
<!--log -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<!-- 测试框架 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>

测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package cn.z201.java.test.jjwt;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import javax.crypto.SecretKey;
import java.time.Clock;
import java.util.Date;
import java.util.UUID;

/**
* @author z201.coding@gmail.com
**/
@Slf4j
public class JwtExampleTest {

private String createJWT(String id, String issuer, String subject, long ttlMillis, SecretKey key) {

Clock clock = Clock.systemDefaultZone();
Date now = new Date(clock.millis());
//Let's set the JWT Claims
JwtBuilder builder = Jwts.builder()
.setId(id)
.setIssuedAt(now)
.setSubject(subject)
.setIssuer(issuer)
.signWith(key);

// 设置过期时间
if (ttlMillis >= 0) {
long expMillis = clock.millis() + ttlMillis;
Date exp = new Date(expMillis);
builder.setExpiration(exp);
}

// 返回编码之后的字符串
return builder.compact();
}

private void parseJWT(String jwt, SecretKey key) {
Claims claims = Jwts.parser()
.setSigningKey(key)
.parseClaimsJws(jwt).getBody();
log.info("ID: " + claims.getId());
log.info("Subject: " + claims.getSubject());
log.info("Issuer: " + claims.getIssuer());
log.info("Expiration: " + claims.getExpiration());
}

@Test
public void testJwt() {
//The JWT signature algorithm we will be using to sign the token
SecretKey key = Keys.secretKeyFor(SignatureAlgorithm.HS256);
String jwt = createJWT(UUID.randomUUID().toString() , "issuer" , "subject" , 10000 ,key);
log.info("createJWT {} " , jwt );
parseJWT(jwt,key);
}

}

输出

22:18:48.641 [main] INFO cn.z201.java.test.jjwt.JwtExampleTest - createJWT eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJjNjFmNzNmZi02OTM4LTQ2MDItYTNkNC1hY2FlZmI3N2Q2YTEiLCJpYXQiOjE1NTI3NDU5MjgsInN1YiI6InN1YmplY3QiLCJpc3MiOiJpc3N1ZXIiLCJleHAiOjE1NTI3NDU5Mzh9.t_ra42L21PMVzto1r2x1fSxCRZFkuk0hSzl2k-b-VWA
22:18:48.694 [main] INFO cn.z201.java.test.jjwt.JwtExampleTest - ID: c61f73ff-6938-4602-a3d4-acaefb77d6a1
22:18:48.694 [main] INFO cn.z201.java.test.jjwt.JwtExampleTest - Subject: subject
22:18:48.694 [main] INFO cn.z201.java.test.jjwt.JwtExampleTest - Issuer: issuer
22:18:48.696 [main] INFO cn.z201.java.test.jjwt.JwtExampleTest - Expiration: Sat Mar 16 22:18:58 CST 2019