ソースを参照

fix:修复token过期后无法重新登录的问题

wangzaijun 6 ヶ月 前
コミット
ee1f1d8819

+ 10 - 1
service-base/src/main/java/com/simuwang/shiro/core/jwt/JwtContext.java

@@ -6,6 +6,7 @@ import com.github.benmanes.caffeine.cache.Cache;
 import com.github.benmanes.caffeine.cache.Caffeine;
 import com.simuwang.base.config.DaqProperties;
 import io.jsonwebtoken.Claims;
+import io.jsonwebtoken.ExpiredJwtException;
 import io.jsonwebtoken.Jwts;
 import io.jsonwebtoken.security.Keys;
 import org.springframework.stereotype.Component;
@@ -38,7 +39,7 @@ public class JwtContext {
         if (MapUtil.isEmpty(tokenMap)) {
             tokenMap = MapUtil.newConcurrentHashMap(16);
         }
-        tokenMap.putIfAbsent(requestIP, token);
+        tokenMap.put(requestIP, token);
         USER_TOKEN_CACHE.put(username, tokenMap);
     }
 
@@ -75,6 +76,14 @@ public class JwtContext {
      */
     public synchronized String generateToken(String username, String requestIp) {
         String token = this.getUserCache(username, requestIp);
+        if (StrUtil.isNotBlank(token)) {
+            try {
+                // 如果token过期则重新生成
+                this.getClaimsByToken(token);
+            } catch (ExpiredJwtException e) {
+                token = null;
+            }
+        }
         if (StrUtil.isBlank(token)) {
             SecretKey signingKey = Keys.hmacShaKeyFor(this.properties.getTokenSecret().getBytes(StandardCharsets.UTF_8));
             //过期时间