|
@@ -3,6 +3,7 @@ package com.fs.erp.service.impl;
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.TypeReference;
|
|
|
+import com.fs.common.utils.StringUtils;
|
|
|
import com.fs.erp.dto.GetInitTokenRequestDTO;
|
|
|
import com.fs.erp.dto.GetInitTokenResponseDTO;
|
|
|
import com.fs.erp.dto.RefreshTokenRequestDTO;
|
|
@@ -43,20 +44,27 @@ public class JstTokenService {
|
|
|
@Autowired
|
|
|
private StringRedisTemplate redisTemplate;
|
|
|
|
|
|
+ private final Object tokenLock = new Object();
|
|
|
+
|
|
|
/**
|
|
|
* 获取访问令牌,如果缓存中存在有效令牌则直接返回,否则重新获取
|
|
|
* @return 有效的访问令牌
|
|
|
*/
|
|
|
public String getAccessToken() {
|
|
|
String accessToken = redisTemplate.opsForValue().get(JST_TOKEN_KEY);
|
|
|
-
|
|
|
- if (accessToken != null && !accessToken.isEmpty()) {
|
|
|
+ if (StringUtils.isNotEmpty(accessToken)) {
|
|
|
log.debug("从缓存中获取到有效的access_token");
|
|
|
return accessToken;
|
|
|
}
|
|
|
-
|
|
|
- log.info("缓存中没有有效的access_token,重新获取");
|
|
|
- return refreshAccessToken();
|
|
|
+ synchronized (tokenLock){
|
|
|
+ accessToken = redisTemplate.opsForValue().get(JST_TOKEN_KEY);
|
|
|
+ if (StringUtils.isNotEmpty(accessToken)) {
|
|
|
+ log.debug("从缓存中获取到有效的access_token");
|
|
|
+ return accessToken;
|
|
|
+ }
|
|
|
+ log.info("缓存中没有有效的access_token,重新获取");
|
|
|
+ return refreshAccessToken();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -75,7 +83,6 @@ public class JstTokenService {
|
|
|
log.error("使用refresh_token刷新失败,尝试重新初始化", e);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
// 如果没有刷新令牌或刷新失败,则重新初始化
|
|
|
return initToken();
|
|
|
}
|