软件工程项目实训 叁
山东大学软件工程项目实训(2021-4)
实现过滤器,联系前端进行调试
一、本阶段工作进展
1.修改token工具类
加入快过期更新
抛弃状态,直接抛出错误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// 判断 token 是否 有效0/快过期1/过期2/无效3
public static String checkToken(String token) {
String result;
// 判null
if (token == null) {
throw new RuntimeException("500 (Error: token is null in checkToken)");
}
// 获取 token 中的信息 ( id ExpireTime )
String content;
try {
content = JWT.decode(token).getAudience().get(0);
} catch (JWTDecodeException j) {
throw new RuntimeException("500 (jwt decode error)");
}
String secret = secretKey + content.split(";;")[0];
// 验证 token
JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(secret)).build();
try {
jwtVerifier.verify(token);
} catch (JWTVerificationException e) {
throw new RuntimeException("500 (jwt verify error)");
}
// 查看 token 的 有效期
long expireTime = Long.parseLong(content.split(";;")[1]);
long currentTime = System.currentTimeMillis();
// 已过期
if (currentTime > expireTime){
throw new RuntimeException("500 (token expire)");
}else {
// 快过期
if (expireTime - currentTime < tokenNearExpireTime){
long newExpireTime = System.currentTimeMillis() + tokenExpireTime*60*1000L;
String newToken= JWT.create().withAudience(content.split(";;")[0] +";;"+ newExpireTime)
.sign(Algorithm.HMAC256(secret));
result = newToken;
}else {
// 正常有效
result = token;
}
}
return result;
}
原修改类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// public static Map<String,String> checkToken(String token) {
// Map<String, String> res = new HashMap<String, String>();
// // 判null
// if (token == null) {
// res.put("state","invalid");
// res.put("token",null);
// throw new RuntimeException("Error: token is null in checkToken ");
// }
// // 获取 token 中的信息 ( id ExpireTime )
// String content;
// try {
// content = JWT.decode(token).getAudience().get(0);
// } catch (JWTDecodeException j) {
// res.put("state","invalid");
// res.put("token",null);
// throw new RuntimeException("401 (jwt decode error)");
// }
// String secret = secretKey + content.split(";;")[0];
// // 验证 token 如果验证失败应该返回FALSE???
// JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(secret)).build();
// try {
// jwtVerifier.verify(token);
// } catch (JWTVerificationException e) {
// res.put("state","invalid");
// res.put("token",null);
// throw new RuntimeException("401 (jwt verify error)");
// }
// // 验证成功
// // 查看 token 的 有效期
// long expireTime = Long.parseLong(content.split(";;")[1]);
// long currentTime = System.currentTimeMillis();
// // 已过期
// if (currentTime > expireTime){
// res.put("state","expire");
// res.put("token",null);
// }else {
// // 快过期
// if (expireTime - currentTime < 5*60*1000){
// res.put("state","invalid");
// long newExpireTime = System.currentTimeMillis() + tokenExpireTime*60*1000L;
// String newToken= JWT.create().withAudience(content.split(";;")[0] +";;"+ newExpireTime)
// .sign(Algorithm.HMAC256(secret));
// res.put("token",newToken);
// }else {
// // 正常有效
// res.put("state","valid");
// res.put("token",token);
// }
// }
// // 有效
// return res;
// }
进行测试1
2
3
4
5
6
7
8
9
10
11
12/** test checkToken() **/
String res = checkToken(token);
System.out.println("|--有效: \t\t\t"+res);
System.out.println("|------");
TimeUnit.MINUTES.sleep(2);//分
res = checkToken(token);
System.out.println("|--快过期: \t\t\t"+res);
System.out.println("|------");
TimeUnit.MINUTES.sleep(2);//分
res = checkToken(token);
System.out.println("|--过期: \t\t\t"+res);
System.out.println("|------");
测试结果
2.编写过滤器
1 |
|
未完待续 …







