TokenService.java
1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.zteits.park.job.datahub;
import com.alibaba.fastjson.JSONObject;
import com.clouds.common.constants.RedisKeyEnum;
import com.clouds.common.redis.RedisCacheFactory;
import com.jtjt.upload.client.common.TokenUtil;
import com.jtjt.upload.client.vo.ClientDetails;
import com.jtjt.upload.client.vo.TokenInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 获取数据归集平台token
* @since 2025-08-17
*/
@Component
public class TokenService {
@Autowired
private HubConfig hubConfig;
private static final Logger logger = LoggerFactory.getLogger(TokenService.class);
private static final String TOKEN_KEY = "data_hub_token:";
/**
* 获取token
* @param forceRefresh 是否强制刷新
* @return
*/
public String getToken(boolean forceRefresh) {
//获取token
if (forceRefresh || RedisCacheFactory.getRedisCacheUtil().get(TOKEN_KEY) == null) {
String urlPrefix = hubConfig.getUrlPrefix();
ClientDetails clientDetails = new ClientDetails();
clientDetails.setClient_id(hubConfig.getClientId());
clientDetails.setClient_secret(hubConfig.getClientSecret());
clientDetails.setScope(hubConfig.getScope());
clientDetails.setGrant_type(hubConfig.getGrantType());
logger.info("查询token:{},{}",urlPrefix, JSONObject.toJSONString(clientDetails));
TokenInfo token = TokenUtil.getSingleInstance().getTokenInfo(urlPrefix, clientDetails);
logger.info("查询token结果:{}",JSONObject.toJSONString(token));
//放入Redis
RedisCacheFactory.getRedisCacheUtil().put(TOKEN_KEY, token.getAccess_token(),Long.parseLong(token.getExpires_in()) - 5);
return token.getAccess_token();
}
return RedisCacheFactory.getRedisCacheUtil().get(TOKEN_KEY);
}
}