提交 | 用户 | 时间
|
cb8c71
|
1 |
package com.iailab.sdk.auth.client; |
H |
2 |
|
|
3 |
import com.fasterxml.jackson.databind.ObjectMapper; |
|
4 |
import com.iailab.sdk.auth.client.dto.TokenDTO; |
|
5 |
import com.iailab.sdk.auth.client.vo.AuthLoginReqVO; |
|
6 |
import org.springframework.core.ParameterizedTypeReference; |
|
7 |
import org.springframework.http.*; |
|
8 |
import org.springframework.stereotype.Component; |
|
9 |
import org.springframework.stereotype.Service; |
|
10 |
import org.springframework.util.Assert; |
|
11 |
import org.springframework.web.client.RestTemplate; |
|
12 |
|
|
13 |
import java.util.*; |
|
14 |
|
|
15 |
/** |
|
16 |
* @author Houzhongjian |
|
17 |
* @Description |
|
18 |
* @createTime 2025年02月18日 |
|
19 |
*/ |
|
20 |
@Component |
|
21 |
@Service |
|
22 |
public class IailabAuthClient { |
|
23 |
|
|
24 |
/** |
|
25 |
* 平台地址 |
|
26 |
*/ |
|
27 |
public static String BASE_URL = "http://172.16.8.100:48080/admin-api"; |
|
28 |
|
|
29 |
/** |
|
30 |
* 租户编号 |
|
31 |
*/ |
|
32 |
public static String TENANT_ID = "1"; |
|
33 |
|
|
34 |
private static final RestTemplate restTemplate = new RestTemplate(); |
|
35 |
|
|
36 |
/** |
|
37 |
* 用户名密码方式获取平台token |
|
38 |
*/ |
|
39 |
public static synchronized TokenDTO login(AuthLoginReqVO loginReqVO) throws Exception { |
|
40 |
System.out.println("登录获取平台token"); |
|
41 |
ObjectMapper objectMapper = new ObjectMapper(); |
|
42 |
String paramString = objectMapper.writeValueAsString(loginReqVO); |
|
43 |
// 1.1 构建请求头 |
|
44 |
HttpHeaders headers = new HttpHeaders(); |
|
45 |
addClientHeader(headers); |
|
46 |
headers.setContentType(MediaType.APPLICATION_JSON_UTF8); |
|
47 |
// 1.2 构建authenticate请求URL |
|
48 |
String authenticateUrl = BASE_URL + "/system/auth/login"; |
|
49 |
// 2. 执行请求 |
|
50 |
ResponseEntity<Map<String, Object>> exchange = restTemplate.exchange( |
|
51 |
authenticateUrl, |
|
52 |
HttpMethod.POST, |
|
53 |
new HttpEntity<>(paramString, headers), |
|
54 |
new ParameterizedTypeReference<Map<String, Object>>() { |
|
55 |
}); |
|
56 |
return handleResponse(exchange); |
|
57 |
} |
|
58 |
|
|
59 |
public static synchronized TokenDTO refreshToken(String refreshToken) { |
|
60 |
System.out.println("刷新token"); |
|
61 |
// 1.1 构建请求头 |
|
62 |
HttpHeaders headers = new HttpHeaders(); |
|
63 |
addClientHeader(headers); |
|
64 |
// 1.2 构建authenticate请求URL |
|
65 |
String authenticateUrl = BASE_URL + "/system/auth/refresh-token?refreshToken=" + refreshToken; |
|
66 |
// 2. 执行请求 |
|
67 |
ResponseEntity<Map<String, Object>> exchange = restTemplate.exchange( |
|
68 |
authenticateUrl, |
|
69 |
HttpMethod.POST, |
|
70 |
new HttpEntity<>(headers), |
|
71 |
new ParameterizedTypeReference<Map<String, Object>>() { |
|
72 |
}); |
|
73 |
return handleResponse(exchange); |
|
74 |
} |
|
75 |
|
|
76 |
private static void addClientHeader(HttpHeaders headers) { |
|
77 |
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
|
78 |
headers.set("tenant-id", TENANT_ID); |
|
79 |
} |
|
80 |
|
|
81 |
// 统一处理响应 |
|
82 |
private static <T> TokenDTO handleResponse(ResponseEntity<T> response) { |
|
83 |
Assert.isTrue(response.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功"); |
|
84 |
System.out.println(response); |
|
85 |
TokenDTO authTokenDTO = new TokenDTO(); |
|
86 |
Map<String, Object> authMap = (Map<String, Object>)response.getBody(); |
|
87 |
Map<String, Object> tokenData = (Map<String, Object>)authMap.get("data"); |
|
88 |
authTokenDTO.setAccessToken(tokenData.get("accessToken").toString()); |
|
89 |
authTokenDTO.setRefreshToken(tokenData.get("refreshToken").toString()); |
|
90 |
authTokenDTO.setExpiresTime(Long.valueOf(tokenData.get("expiresTime").toString())); |
|
91 |
return authTokenDTO; |
|
92 |
} |
|
93 |
} |