|
|
@@ -8,6 +8,7 @@ import io.jsonwebtoken.SignatureAlgorithm;
|
|
|
import io.jsonwebtoken.UnsupportedJwtException;
|
|
|
import io.jsonwebtoken.security.Keys;
|
|
|
import io.jsonwebtoken.security.SignatureException;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
@@ -23,6 +24,7 @@ import java.util.stream.Collectors;
|
|
|
* JWT Token工具类
|
|
|
* 专门用于第三方接口的Token生成和验证
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
@Component
|
|
|
public class JwtTokenUtil {
|
|
|
|
|
|
@@ -53,20 +55,16 @@ public class JwtTokenUtil {
|
|
|
if (existingToken != null) {
|
|
|
return existingToken;
|
|
|
}
|
|
|
-
|
|
|
Date now = new Date();
|
|
|
Date expiryDate = new Date(now.getTime() + expireSeconds * 1000);
|
|
|
-
|
|
|
String token = Jwts.builder()
|
|
|
.setSubject(operatorId)
|
|
|
.setIssuedAt(now)
|
|
|
.setExpiration(expiryDate)
|
|
|
.signWith(secretKey, SignatureAlgorithm.HS512)
|
|
|
.compact();
|
|
|
-
|
|
|
// 存储到Redis,支持Token主动撤销
|
|
|
storeTokenInRedis(token, operatorId);
|
|
|
-
|
|
|
return token;
|
|
|
}
|
|
|
|
|
|
@@ -79,27 +77,23 @@ public class JwtTokenUtil {
|
|
|
if (!isTokenInRedis(token)) {
|
|
|
return false;
|
|
|
}
|
|
|
-
|
|
|
// 验证JWT签名和过期时间
|
|
|
Jwts.parserBuilder()
|
|
|
.setSigningKey(secretKey)
|
|
|
.build()
|
|
|
.parseClaimsJws(token);
|
|
|
-
|
|
|
return true;
|
|
|
-
|
|
|
} catch (ExpiredJwtException e) {
|
|
|
- System.out.println("Token已过期: " + e.getMessage());
|
|
|
+ log.error("Token已过期: {}" , e.getMessage());
|
|
|
} catch (UnsupportedJwtException e) {
|
|
|
- System.out.println("不支持的Token格式: " + e.getMessage());
|
|
|
+ log.error("不支持的Token格式: {}" , e.getMessage());
|
|
|
} catch (MalformedJwtException e) {
|
|
|
- System.out.println("Token格式错误: " + e.getMessage());
|
|
|
+ log.error("Token格式错误: {}" , e.getMessage());
|
|
|
} catch (SignatureException e) {
|
|
|
- System.out.println("Token签名验证失败: " + e.getMessage());
|
|
|
+ log.error("Token签名验证失败: {}" , e.getMessage());
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
- System.out.println("Token参数错误: " + e.getMessage());
|
|
|
+ log.error("Token参数错误: {}" , e.getMessage());
|
|
|
}
|
|
|
-
|
|
|
return false;
|
|
|
}
|
|
|
|