houzhongjian
2025-03-03 7434c4fd851bc6c8782f050478d96ed6699ec089
sdk/src/main/java/com/iailab/sdk/auth/client/IailabAuthClient.java
@@ -1,6 +1,7 @@
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 com.iailab.sdk.auth.config.AuthProperties;
import org.springframework.core.ParameterizedTypeReference;
@@ -50,7 +51,7 @@
    /**
     * 用户名密码方式获取平台token
     */
    public static synchronized void authenticate() throws Exception {
    public static synchronized TokenDTO authenticate() throws Exception {
        System.out.println("登录获取平台token");
        // 1.1 构建请求头
        HttpHeaders headers = new HttpHeaders();
@@ -81,9 +82,10 @@
        accessToken = authMap.get("access_token").toString();
        refreshToken = authMap.get("refresh_token").toString();
        expireTime = Long.valueOf(authMap.get("expires_time").toString());
        return handleResponse(exchange);
    }
    public static synchronized void refreshToken() throws Exception {
    public static synchronized TokenDTO refreshToken() throws Exception {
        System.out.println("刷新token");
        // 1.1 构建请求头
        HttpHeaders headers = new HttpHeaders();
@@ -111,6 +113,7 @@
            accessToken = authMap.get("access_token").toString();
            expireTime = Long.valueOf(authMap.get("expires_time").toString());
        }
        return handleResponse(exchange);
    }
    private static void addClientHeader(HttpHeaders headers) {
@@ -122,4 +125,17 @@
        headers.set("tenant-id", authProperties.getTenantId());
    }
    // 统一处理响应
    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;
    }
}