潘志宝
2025-02-28 9882627244a4758df2025721ab13e33a035a5088
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.iailab.sdk.auth.client;
 
import com.fasterxml.jackson.databind.ObjectMapper;
import com.iailab.sdk.auth.client.dto.TokenDTO;
import com.iailab.sdk.auth.client.vo.AuthLoginReqVO;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.client.RestTemplate;
 
import java.util.*;
 
/**
 * @author Houzhongjian
 * @Description
 * @createTime 2025年02月18日
 */
@Component
@Service
public class IailabAuthClient {
 
    /**
     * 平台地址
     */
    public static String BASE_URL = "http://172.16.8.100:48080/admin-api";
 
    /**
     * 租户编号
     */
    public static String TENANT_ID = "1";
 
    private static final RestTemplate restTemplate = new RestTemplate();
 
    /**
     * 用户名密码方式获取平台token
     */
    public static synchronized TokenDTO login(AuthLoginReqVO loginReqVO) throws Exception {
        System.out.println("登录获取平台token");
        ObjectMapper objectMapper = new ObjectMapper();
        String paramString = objectMapper.writeValueAsString(loginReqVO);
        // 1.1 构建请求头
        HttpHeaders headers = new HttpHeaders();
        addClientHeader(headers);
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        // 1.2 构建authenticate请求URL
        String authenticateUrl = BASE_URL + "/system/auth/login";
        // 2. 执行请求
        ResponseEntity<Map<String, Object>> exchange = restTemplate.exchange(
                authenticateUrl,
                HttpMethod.POST,
                new HttpEntity<>(paramString, headers),
                new ParameterizedTypeReference<Map<String, Object>>() {
                });
        return handleResponse(exchange);
    }
 
    public static synchronized TokenDTO refreshToken(String refreshToken) {
        System.out.println("刷新token");
        // 1.1 构建请求头
        HttpHeaders headers = new HttpHeaders();
        addClientHeader(headers);
        // 1.2 构建authenticate请求URL
        String authenticateUrl = BASE_URL + "/system/auth/refresh-token?refreshToken=" + refreshToken;
        // 2. 执行请求
        ResponseEntity<Map<String, Object>> exchange = restTemplate.exchange(
                authenticateUrl,
                HttpMethod.POST,
                new HttpEntity<>(headers),
                new ParameterizedTypeReference<Map<String, Object>>() {
                });
        return handleResponse(exchange);
    }
 
    private static void addClientHeader(HttpHeaders headers) {
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.set("tenant-id", TENANT_ID);
    }
 
    // 统一处理响应
    private static <T> TokenDTO handleResponse(ResponseEntity<T> response) {
        Assert.isTrue(response.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功");
        System.out.println(response);
        TokenDTO authTokenDTO = new TokenDTO();
        Map<String, Object> authMap = (Map<String, Object>)response.getBody();
        Map<String, Object> tokenData = (Map<String, Object>)authMap.get("data");
        authTokenDTO.setAccessToken(tokenData.get("accessToken").toString());
        authTokenDTO.setRefreshToken(tokenData.get("refreshToken").toString());
        authTokenDTO.setExpiresTime(Long.valueOf(tokenData.get("expiresTime").toString()));
        return authTokenDTO;
    }
}