Просмотр исходного кода

合作平台 token续期 重新获取用户信息

wangyunpeng 1 день назад
Родитель
Сommit
b76befa3f0
1 измененных файлов с 26 добавлено и 4 удалено
  1. 26 4
      api-module/src/main/java/com/tzld/piaoquan/api/config/JwtInterceptor.java

+ 26 - 4
api-module/src/main/java/com/tzld/piaoquan/api/config/JwtInterceptor.java

@@ -7,7 +7,9 @@ import com.tzld.piaoquan.api.common.enums.ExceptionEnum;
 import com.tzld.piaoquan.api.common.exception.CommonException;
 import com.tzld.piaoquan.api.common.exception.CommonException;
 import com.tzld.piaoquan.api.model.config.LoginUserContext;
 import com.tzld.piaoquan.api.model.config.LoginUserContext;
 import com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformAccount;
 import com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformAccount;
+import com.tzld.piaoquan.api.service.contentplatform.ContentPlatformAccountService;
 import com.tzld.piaoquan.growth.common.utils.RedisUtils;
 import com.tzld.piaoquan.growth.common.utils.RedisUtils;
+import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -24,6 +26,7 @@ import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeUnit;
 
 
+@Slf4j
 @Component
 @Component
 @ConfigurationProperties(prefix = "author.interceptor")
 @ConfigurationProperties(prefix = "author.interceptor")
 public class JwtInterceptor implements HandlerInterceptor {
 public class JwtInterceptor implements HandlerInterceptor {
@@ -40,6 +43,9 @@ public class JwtInterceptor implements HandlerInterceptor {
     @Autowired
     @Autowired
     private RedisUtils redisUtils;
     private RedisUtils redisUtils;
 
 
+    @Autowired
+    private ContentPlatformAccountService accountService;
+
     @Override
     @Override
     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
 
 
@@ -136,10 +142,26 @@ public class JwtInterceptor implements HandlerInterceptor {
 
 
     private void renewalToken(String token) {
     private void renewalToken(String token) {
         executor.execute(() -> {
         executor.execute(() -> {
-            String key = TOKEN_PREFIX.replace("{token}", token);
-            Long seconds = redisUtils.getKeyExpire(key);
-            if (seconds < 24 * 60 * 60) {
-                redisUtils.expire(key, RedisUtils.DEFAULT_EXPIRE_TIME);
+            try {
+                String key = TOKEN_PREFIX.replace("{token}", token);
+                String loginUserString = redisUtils.getString(key);
+                if (StringUtils.isBlank(loginUserString)) {
+                    return;
+                }
+                ContentPlatformAccount account = JSON.parseObject(loginUserString, ContentPlatformAccount.class);
+                Long seconds = redisUtils.getKeyExpire(key);
+                if (seconds < 24 * 60 * 60) {
+                    // 续期时重新从数据库获取最新账户信息并更新缓存
+                    if (Objects.nonNull(account) && Objects.nonNull(account.getId())) {
+                        ContentPlatformAccount latestAccount = accountService.getAccountById(account.getId());
+                        if (Objects.nonNull(latestAccount)) {
+                            redisUtils.setValueWithExpire(key, JSON.toJSONString(latestAccount), RedisUtils.DEFAULT_EXPIRE_TIME);
+                        }
+                    }
+                }
+            } catch (Exception e) {
+                // 续期失败不影响正常请求,仅记录日志
+                log.error("renewalToken error, token={}", token, e);
             }
             }
         });
         });
     }
     }