提交 | 用户 | 时间
|
94c44e
|
1 |
package com.iailab.module.shasteel.util.token; |
D |
2 |
|
|
3 |
import lombok.extern.slf4j.Slf4j; |
7664fa
|
4 |
import org.springframework.beans.factory.annotation.Value; |
94c44e
|
5 |
import org.springframework.core.ParameterizedTypeReference; |
D |
6 |
import org.springframework.http.HttpHeaders; |
|
7 |
import org.springframework.http.HttpMethod; |
|
8 |
import org.springframework.http.MediaType; |
|
9 |
import org.springframework.http.ResponseEntity; |
7664fa
|
10 |
import org.springframework.stereotype.Component; |
94c44e
|
11 |
import org.springframework.util.Assert; |
D |
12 |
import org.springframework.util.Base64Utils; |
|
13 |
import org.springframework.util.ObjectUtils; |
|
14 |
import org.springframework.web.client.RestTemplate; |
|
15 |
|
|
16 |
import java.nio.charset.StandardCharsets; |
|
17 |
import java.util.Map; |
|
18 |
|
|
19 |
/** |
|
20 |
* @author PanZhibao |
|
21 |
* @Description |
|
22 |
* @createTime 2024年11月21日 |
|
23 |
*/ |
|
24 |
@Slf4j |
7664fa
|
25 |
@Component |
94c44e
|
26 |
public class IailabClient { |
D |
27 |
|
|
28 |
/** |
|
29 |
* 平台地址 |
|
30 |
*/ |
7664fa
|
31 |
@Value("${iailab.token.base-url}") |
D |
32 |
private String BASE_URL; |
94c44e
|
33 |
|
D |
34 |
/** |
|
35 |
* 租户编号 |
|
36 |
*/ |
7664fa
|
37 |
@Value("${iailab.token.tenant-id}") |
D |
38 |
private String TENANT_ID; |
94c44e
|
39 |
|
D |
40 |
/** |
|
41 |
* 客户端信息 |
|
42 |
*/ |
7664fa
|
43 |
@Value("${iailab.token.client-id}") |
D |
44 |
private String CLIENT_ID; |
|
45 |
@Value("${iailab.token.client-secret}") |
|
46 |
private String CLIENT_SECRET; |
|
47 |
@Value("${iailab.token.username}") |
|
48 |
private String USERNAME; |
|
49 |
@Value("${iailab.token.password}") |
|
50 |
private String PASSWORD; |
94c44e
|
51 |
private static final String GRAND_TYPE = "password"; |
D |
52 |
private static final String SCOPE = "user.read user.write"; |
|
53 |
|
|
54 |
|
|
55 |
private static final RestTemplate restTemplate = new RestTemplate(); |
|
56 |
|
|
57 |
// 鉴权token |
7664fa
|
58 |
private String accessToken; |
94c44e
|
59 |
// 刷新token |
7664fa
|
60 |
private String refreshToken; |
94c44e
|
61 |
// 鉴权token过期时间 |
7664fa
|
62 |
private Long expireTime; |
94c44e
|
63 |
|
D |
64 |
/** |
|
65 |
* 用户名密码方式获取平台token |
|
66 |
*/ |
7664fa
|
67 |
private synchronized void authenticate() { |
94c44e
|
68 |
log.info("获取平台token"); |
D |
69 |
// 1.1 构建请求头 |
|
70 |
HttpHeaders headers = new HttpHeaders(); |
|
71 |
addClientHeader(headers); |
|
72 |
// 1.2 构建authenticate请求URL |
|
73 |
String authenticateUrl = BASE_URL + "/oauth2/token?" |
|
74 |
// 密码模式的参数 |
|
75 |
+ "grant_type=" + GRAND_TYPE |
|
76 |
+ "&username=" + USERNAME |
|
77 |
+ "&password=" + PASSWORD |
|
78 |
+ "&scope=" + SCOPE; |
|
79 |
// 2. 执行请求 |
|
80 |
ResponseEntity<Map<String, Object>> exchange = restTemplate.exchange( |
|
81 |
authenticateUrl, |
|
82 |
HttpMethod.POST, |
|
83 |
new org.springframework.http.HttpEntity<>(headers), |
|
84 |
new ParameterizedTypeReference<Map<String, Object>>() { |
|
85 |
}); |
|
86 |
Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功"); |
|
87 |
log.info(exchange.toString()); |
|
88 |
Map<String, Object> authMap = exchange.getBody(); |
|
89 |
accessToken = authMap.get("access_token").toString(); |
|
90 |
refreshToken = authMap.get("refresh_token").toString(); |
|
91 |
expireTime = Long.valueOf(authMap.get("expires_time").toString()); |
|
92 |
} |
|
93 |
|
7664fa
|
94 |
private synchronized void refreshToken() { |
94c44e
|
95 |
log.info("刷新token"); |
D |
96 |
// 1.1 构建请求头 |
|
97 |
HttpHeaders headers = new HttpHeaders(); |
|
98 |
addClientHeader(headers); |
|
99 |
// 1.2 构建authenticate请求URL |
|
100 |
String authenticateUrl = BASE_URL + "/system/auth/client-refresh-token?refreshToken=" + refreshToken |
|
101 |
+ "&clientId=" + CLIENT_ID; |
|
102 |
// 2. 执行请求 |
|
103 |
ResponseEntity<Map<String, Object>> exchange = restTemplate.exchange( |
|
104 |
authenticateUrl, |
|
105 |
HttpMethod.POST, |
|
106 |
new org.springframework.http.HttpEntity<>(headers), |
|
107 |
new ParameterizedTypeReference<Map<String, Object>>() { |
|
108 |
}); |
|
109 |
Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功"); |
|
110 |
Map<String, Object> authMap = exchange.getBody(); |
|
111 |
//刷新token过期,重新获取token |
|
112 |
if (!ObjectUtils.isEmpty(authMap.get("code"))) { |
|
113 |
Integer code = Integer.valueOf(authMap.get("code").toString()); |
|
114 |
if (code == 401) { |
|
115 |
authenticate(); |
|
116 |
} |
|
117 |
} else { |
|
118 |
accessToken = authMap.get("access_token").toString(); |
|
119 |
expireTime = Long.valueOf(authMap.get("expires_time").toString()); |
|
120 |
} |
|
121 |
} |
|
122 |
|
7664fa
|
123 |
private void addClientHeader(HttpHeaders headers) { |
94c44e
|
124 |
// client 拼接,需要 BASE64 编码 |
D |
125 |
String client = CLIENT_ID + ":" + CLIENT_SECRET; |
|
126 |
client = Base64Utils.encodeToString(client.getBytes(StandardCharsets.UTF_8)); |
|
127 |
headers.add("Authorization", "Basic " + client); |
|
128 |
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
7664fa
|
129 |
headers.set("tenant-id", getTenantId().toString()); |
94c44e
|
130 |
} |
D |
131 |
|
7664fa
|
132 |
public String getToken() { |
94c44e
|
133 |
//第一次请求或者token过期,需要重新获取token |
7664fa
|
134 |
if(ObjectUtils.isEmpty(accessToken)) { |
D |
135 |
authenticate(); |
|
136 |
} else if (expireTime < System.currentTimeMillis() / 1000) { |
|
137 |
refreshToken(); |
94c44e
|
138 |
} |
D |
139 |
|
|
140 |
return accessToken; |
|
141 |
} |
7664fa
|
142 |
public Long getTenantId() { |
D |
143 |
return Long.valueOf(TENANT_ID); |
94c44e
|
144 |
} |
D |
145 |
} |