对比新文件 |
| | |
| | | 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; |
| | | } |
| | | } |