JwtTokenUtil.java
6.8 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package com.aigeo.common.security;
import io.jsonwebtoken.*;
import io.jsonwebtoken.security.Keys;
import io.jsonwebtoken.security.SignatureException;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.crypto.SecretKey;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
/**
* JWT工具类
*
* @author AIGEO Team
* @since 1.0.0
*/
@Component
public class JwtTokenUtil {
private static final Logger log = LoggerFactory.getLogger(JwtTokenUtil.class);
@Value("${app.jwt.secret:aigeo-jwt-secret-key-for-development-only-change-in-production}")
private String secret;
@Value("${app.jwt.expiration:86400}")
private Long expiration;
/**
* 获取签名密钥
*/
private SecretKey getSigningKey() {
byte[] keyBytes = secret.getBytes();
return Keys.hmacShaKeyFor(keyBytes);
}
/**
* 生成JWT令牌
*/
public String generateToken(String username, Integer userId, Integer companyId) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime expirationTime = now.plusSeconds(expiration);
return Jwts.builder()
.setSubject(username)
.claim("userId", userId)
.claim("companyId", companyId)
.setIssuedAt(Date.from(now.atZone(ZoneId.systemDefault()).toInstant()))
.setExpiration(Date.from(expirationTime.atZone(ZoneId.systemDefault()).toInstant()))
.setIssuer("aigeo")
.signWith(getSigningKey())
.compact();
}
/**
* 从令牌中获取用户名
*/
public String getUsernameFromToken(String token) {
try {
Claims claims = getClaimsFromToken(token);
return claims.getSubject();
} catch (Exception e) {
log.error("获取用户名失败: {}", e.getMessage());
return null;
}
}
/**
* 从令牌中获取用户ID
*/
public Integer getUserIdFromToken(String token) {
try {
Claims claims = getClaimsFromToken(token);
return claims.get("userId", Integer.class);
} catch (Exception e) {
log.error("获取用户ID失败: {}", e.getMessage());
return null;
}
}
/**
* 从令牌中获取公司ID
*/
public Integer getCompanyIdFromToken(String token) {
try {
Claims claims = getClaimsFromToken(token);
return claims.get("companyId", Integer.class);
} catch (Exception e) {
log.error("获取公司ID失败: {}", e.getMessage());
return null;
}
}
/**
* 验证令牌是否过期
*/
public Boolean isTokenExpired(String token) {
try {
Claims claims = getClaimsFromToken(token);
return claims.getExpiration().before(new Date());
} catch (Exception e) {
return true;
}
}
/**
* 验证令牌有效性
*/
public Boolean validateToken(String token, String username) {
try {
String tokenUsername = getUsernameFromToken(token);
return tokenUsername != null && tokenUsername.equals(username) && !isTokenExpired(token);
} catch (Exception e) {
log.error("令牌验证失败: {}", e.getMessage());
return false;
}
}
/**
* 验证令牌有效性(不校验用户名)
*/
public Boolean validateToken(String token) {
try {
getClaimsFromToken(token);
return !isTokenExpired(token);
} catch (Exception e) {
log.error("令牌验证失败: {}", e.getMessage());
return false;
}
}
/**
* 为User对象生成令牌
*/
public String generateToken(com.aigeo.company.entity.User user) {
return generateToken(user.getUsername(), user.getId(), user.getCompanyId());
}
/**
* 获取令牌有效期(秒)
*/
public Long getTokenValidityInSeconds() {
return expiration;
}
/**
* 获取令牌过期时间戳
*/
public long getTokenExpirationTime(String token) {
try {
Claims claims = getClaimsFromToken(token);
return claims.getExpiration().getTime();
} catch (Exception e) {
log.error("获取令牌过期时间失败: {}", e.getMessage());
return 0;
}
}
/**
* 刷新令牌
*/
public String refreshToken(String token) {
try {
Claims claims = getClaimsFromToken(token);
String username = claims.getSubject();
Integer userId = claims.get("userId", Integer.class);
Integer companyId = claims.get("companyId", Integer.class);
return generateToken(username, userId, companyId);
} catch (Exception e) {
log.error("刷新令牌失败: {}", e.getMessage());
return null;
}
}
/**
* 从令牌中获取声明
*/
private Claims getClaimsFromToken(String token) {
try {
return Jwts.parserBuilder()
.setSigningKey(getSigningKey())
.build()
.parseClaimsJws(token)
.getBody();
} catch (ExpiredJwtException e) {
log.warn("JWT令牌已过期: {}", e.getMessage());
throw e;
} catch (UnsupportedJwtException e) {
log.error("不支持的JWT令牌: {}", e.getMessage());
throw e;
} catch (MalformedJwtException e) {
log.error("JWT令牌格式错误: {}", e.getMessage());
throw e;
} catch (SignatureException e) {
log.error("JWT签名验证失败: {}", e.getMessage());
throw e;
} catch (IllegalArgumentException e) {
log.error("JWT令牌参数错误: {}", e.getMessage());
throw e;
}
}
/**
* 获取令牌过期时间
*/
public Date getExpirationDateFromToken(String token) {
try {
Claims claims = getClaimsFromToken(token);
return claims.getExpiration();
} catch (Exception e) {
log.error("获取令牌过期时间失败: {}", e.getMessage());
return null;
}
}
/**
* 获取令牌发布时间
*/
public Date getIssuedAtDateFromToken(String token) {
try {
Claims claims = getClaimsFromToken(token);
return claims.getIssuedAt();
} catch (Exception e) {
log.error("获取令牌发布时间失败: {}", e.getMessage());
return null;
}
}
}