|
|
@@ -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.model.config.LoginUserContext;
|
|
|
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 lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
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.TimeUnit;
|
|
|
|
|
|
+@Slf4j
|
|
|
@Component
|
|
|
@ConfigurationProperties(prefix = "author.interceptor")
|
|
|
public class JwtInterceptor implements HandlerInterceptor {
|
|
|
@@ -40,6 +43,9 @@ public class JwtInterceptor implements HandlerInterceptor {
|
|
|
@Autowired
|
|
|
private RedisUtils redisUtils;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private ContentPlatformAccountService accountService;
|
|
|
+
|
|
|
@Override
|
|
|
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) {
|
|
|
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);
|
|
|
}
|
|
|
});
|
|
|
}
|