提交 | 用户 | 时间
|
4d3533
|
1 |
package com.iailab.module.data.common.utils; |
潘 |
2 |
|
|
3 |
import com.auth0.jwt.JWT; |
|
4 |
import com.auth0.jwt.JWTVerifier; |
|
5 |
import com.auth0.jwt.algorithms.Algorithm; |
|
6 |
import com.auth0.jwt.interfaces.DecodedJWT; |
|
7 |
|
|
8 |
import java.util.Date; |
|
9 |
|
|
10 |
/** |
|
11 |
* @author PanZhibao |
|
12 |
* @Description |
|
13 |
* @createTime 2022年07月20日 11:47:00 |
|
14 |
*/ |
|
15 |
public class JwtUtils { |
|
16 |
private static final String USER_INFO_KEY = "username"; |
|
17 |
|
|
18 |
/**Token有效期为12小时*/ |
|
19 |
public static final long EXPIRE_TIME = 60 * 60 * 1000 * 12; |
|
20 |
|
|
21 |
/** |
|
22 |
* 生成Token |
|
23 |
* @param username 用户标识(唯一) |
|
24 |
* @param secretKey 签名算法以及密匙 |
|
25 |
* @return |
|
26 |
*/ |
|
27 |
public static String generateToken(String username, String secretKey) { |
|
28 |
Algorithm algorithm = Algorithm.HMAC256(secretKey); |
|
29 |
|
|
30 |
Date now = new Date(); |
|
31 |
|
|
32 |
Date expireTime = new Date(now.getTime() + EXPIRE_TIME); |
|
33 |
|
|
34 |
String token = JWT.create() |
|
35 |
.withClaim(USER_INFO_KEY, username) |
|
36 |
.withExpiresAt(expireTime) |
|
37 |
.sign(algorithm); |
|
38 |
|
|
39 |
return token; |
|
40 |
} |
|
41 |
|
|
42 |
/** |
|
43 |
* 校验token是否正确 |
|
44 |
* |
|
45 |
* @param token 密钥 |
|
46 |
* @param secret 用户的密码 |
|
47 |
* @return 是否正确 |
|
48 |
*/ |
|
49 |
public static boolean verify(String token, String username, String secret) { |
|
50 |
try { |
|
51 |
// 根据密码生成JWT效验器 |
|
52 |
Algorithm algorithm = Algorithm.HMAC256(secret); |
|
53 |
JWTVerifier verifier = JWT.require(algorithm).withClaim("username", username).build(); |
|
54 |
// 效验TOKEN |
|
55 |
DecodedJWT jwt = verifier.verify(token); |
|
56 |
return true; |
|
57 |
} catch (Exception exception) { |
|
58 |
return false; |
|
59 |
} |
|
60 |
} |
|
61 |
|
|
62 |
/** |
|
63 |
* 校验token是否正确 |
|
64 |
* |
|
65 |
* @param token |
|
66 |
* @param secret |
|
67 |
* @return |
|
68 |
*/ |
|
69 |
public static boolean verify(String token, String secret) { |
|
70 |
try { |
|
71 |
Algorithm algorithm = Algorithm.HMAC256(secret); |
|
72 |
JWTVerifier verifier = JWT.require(algorithm).build(); |
|
73 |
// 效验TOKEN |
|
74 |
DecodedJWT jwt = verifier.verify(token); |
|
75 |
return true; |
|
76 |
} catch (Exception exception) { |
|
77 |
exception.printStackTrace(); |
|
78 |
return false; |
|
79 |
} |
|
80 |
} |
|
81 |
|
|
82 |
/** |
|
83 |
* 从Token中提取用户信息 |
|
84 |
* @param token |
|
85 |
* @return |
|
86 |
*/ |
|
87 |
public static String getUserInfo(String token) { |
|
88 |
DecodedJWT decodedJWT = JWT.decode(token); |
|
89 |
String username = decodedJWT.getClaim(USER_INFO_KEY).asString(); |
|
90 |
return username; |
|
91 |
} |
|
92 |
|
|
93 |
/** |
|
94 |
* 从Token中提取信息 |
|
95 |
* @param token |
|
96 |
* @param key |
|
97 |
* @return |
|
98 |
*/ |
|
99 |
public static String getInfo(String token, String key) { |
|
100 |
DecodedJWT decodedJWT = JWT.decode(token); |
|
101 |
String username = decodedJWT.getClaim(USER_INFO_KEY).asString(); |
|
102 |
return username; |
|
103 |
} |
|
104 |
} |