From 3216ed3fb2d9dc9cf61da21bda3175e66ec46426 Mon Sep 17 00:00:00 2001 From: dengzedong <dengzedong@email> Date: 星期五, 06 十二月 2024 17:04:11 +0800 Subject: [PATCH] IailabClient 配置 --- shasteel-biz/src/main/java/com/iailab/module/shasteel/util/token/IailabClient.java | 71 ++++++++++++++--------------------- 1 files changed, 29 insertions(+), 42 deletions(-) diff --git a/shasteel-biz/src/main/java/com/iailab/module/shasteel/util/token/IailabClient.java b/shasteel-biz/src/main/java/com/iailab/module/shasteel/util/token/IailabClient.java index fe3fcc5..8c0a0a1 100644 --- a/shasteel-biz/src/main/java/com/iailab/module/shasteel/util/token/IailabClient.java +++ b/shasteel-biz/src/main/java/com/iailab/module/shasteel/util/token/IailabClient.java @@ -1,11 +1,13 @@ package com.iailab.module.shasteel.util.token; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Component; import org.springframework.util.Assert; import org.springframework.util.Base64Utils; import org.springframework.util.ObjectUtils; @@ -20,25 +22,32 @@ * @createTime 2024年11月21日 */ @Slf4j +@Component public class IailabClient { /** * 平台地址 */ - public static String BASE_URL; + @Value("${iailab.token.base-url}") + private String BASE_URL; /** * 租户编号 */ - public static Long TENANT_ID; + @Value("${iailab.token.tenant-id}") + private String TENANT_ID; /** * 客户端信息 */ - private static String CLIENT_ID; - private static String CLIENT_SECRET; - private static String USERNAME; - private static String PASSWORD; + @Value("${iailab.token.client-id}") + private String CLIENT_ID; + @Value("${iailab.token.client-secret}") + private String CLIENT_SECRET; + @Value("${iailab.token.username}") + private String USERNAME; + @Value("${iailab.token.password}") + private String PASSWORD; private static final String GRAND_TYPE = "password"; private static final String SCOPE = "user.read user.write"; @@ -46,38 +55,16 @@ private static final RestTemplate restTemplate = new RestTemplate(); // 鉴权token - public static String accessToken; + private String accessToken; // 刷新token - public static String refreshToken; + private String refreshToken; // 鉴权token过期时间 - public static Long expireTime; - - private static final IailabClient iailabClient = new IailabClient(); - - private IailabClient() { -// BASE_URL = PlatApplicationContext.getProperty("iailab.baseUrl"); -// TENANT_ID = Long.parseLong(PlatApplicationContext.getProperty("iailab.tenantId")); -// CLIENT_ID = PlatApplicationContext.getProperty("iailab.clientId"); -// CLIENT_SECRET = PlatApplicationContext.getProperty("iailab.clientSecret"); -// USERNAME = PlatApplicationContext.getProperty("iailab.username"); -// PASSWORD = PlatApplicationContext.getProperty("iailab.password"); - - BASE_URL = "http://127.0.0.1:48080/admin-api/system"; - TENANT_ID = 172L; - CLIENT_ID = "shasteel"; - CLIENT_SECRET = "shasteel111111111111111"; - USERNAME = "shasteel"; - PASSWORD = "123456"; - } - - public static IailabClient getInstance() { - return iailabClient; - } + private Long expireTime; /** * 用户名密码方式获取平台token */ - private static synchronized void authenticate() { + private synchronized void authenticate() { log.info("获取平台token"); // 1.1 构建请求头 HttpHeaders headers = new HttpHeaders(); @@ -104,7 +91,7 @@ expireTime = Long.valueOf(authMap.get("expires_time").toString()); } - private static synchronized void refreshToken() { + private synchronized void refreshToken() { log.info("刷新token"); // 1.1 构建请求头 HttpHeaders headers = new HttpHeaders(); @@ -133,26 +120,26 @@ } } - private static void addClientHeader(HttpHeaders headers) { + private void addClientHeader(HttpHeaders headers) { // client 拼接,需要 BASE64 编码 String client = CLIENT_ID + ":" + CLIENT_SECRET; client = Base64Utils.encodeToString(client.getBytes(StandardCharsets.UTF_8)); headers.add("Authorization", "Basic " + client); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); - headers.set("tenant-id", TENANT_ID.toString()); + headers.set("tenant-id", getTenantId().toString()); } - public static String getToken() { + public String getToken() { //第一次请求或者token过期,需要重新获取token - if(ObjectUtils.isEmpty(IailabClient.accessToken)) { - IailabClient.authenticate(); - } else if (IailabClient.expireTime < System.currentTimeMillis() / 1000) { - IailabClient.refreshToken(); + if(ObjectUtils.isEmpty(accessToken)) { + authenticate(); + } else if (expireTime < System.currentTimeMillis() / 1000) { + refreshToken(); } return accessToken; } - public static Long getTenantId() { - return TENANT_ID; + public Long getTenantId() { + return Long.valueOf(TENANT_ID); } } \ No newline at end of file -- Gitblit v1.9.3