From b0430a610332aca416ce8fa735b623650dfdac6c Mon Sep 17 00:00:00 2001
From: houzhongjian <houzhongyi@126.com>
Date: 星期五, 14 三月 2025 15:50:58 +0800
Subject: [PATCH] sdk密码授权模式修改为客户端模式,只需要用户名和客户端id、secret

---
 iailab-plat-sdk/src/main/java/com/iailab/sdk/IailabPlatSdkMain.java                    |    1 -
 iailab-plat-sdk/src/main/java/com/iailab/sdk/util/http/IailabHttpUtils.java            |   14 +++++++-------
 iailab-plat-sdk/src/main/java/com/iailab/sdk/auth/client/IailabClient.java             |   16 ++++++----------
 iailab-plat-sdk/src/main/java/com/iailab/sdk/auth/constants/SdkErrorCodeConstants.java |    1 -
 iailab-plat-sdk/src/main/java/com/iailab/sdk/auth/config/SdkAutoConfiguration.java     |    6 ------
 iailab-plat-sdk/src/test/java/com/iailab/sdk/IailabClientTest.java                     |   12 +++++-------
 6 files changed, 18 insertions(+), 32 deletions(-)

diff --git a/iailab-plat-sdk/src/main/java/com/iailab/sdk/IailabPlatSdkMain.java b/iailab-plat-sdk/src/main/java/com/iailab/sdk/IailabPlatSdkMain.java
index 3d9be18..a0b8200 100644
--- a/iailab-plat-sdk/src/main/java/com/iailab/sdk/IailabPlatSdkMain.java
+++ b/iailab-plat-sdk/src/main/java/com/iailab/sdk/IailabPlatSdkMain.java
@@ -1,6 +1,5 @@
 package com.iailab.sdk;
 
-import org.springframework.boot.autoconfigure.SpringBootApplication;
 
 /**
  * 项目的主类
diff --git a/iailab-plat-sdk/src/main/java/com/iailab/sdk/auth/client/IailabAuthClient.java b/iailab-plat-sdk/src/main/java/com/iailab/sdk/auth/client/IailabClient.java
similarity index 95%
rename from iailab-plat-sdk/src/main/java/com/iailab/sdk/auth/client/IailabAuthClient.java
rename to iailab-plat-sdk/src/main/java/com/iailab/sdk/auth/client/IailabClient.java
index 5635425..2c06c3c 100644
--- a/iailab-plat-sdk/src/main/java/com/iailab/sdk/auth/client/IailabAuthClient.java
+++ b/iailab-plat-sdk/src/main/java/com/iailab/sdk/auth/client/IailabClient.java
@@ -31,7 +31,7 @@
  * @Description
  * @createTime 2025年02月18日
  */
-public class IailabAuthClient {
+public class IailabClient {
 
     private static final RestTemplate restTemplate = new RestTemplate();
 
@@ -52,18 +52,17 @@
     private static String CLIENT_ID = SdkAutoConfiguration.CLIENT_ID;
     private static String CLIENT_SECRET = SdkAutoConfiguration.CLIENT_SECRET;
     private static String USERNAME = SdkAutoConfiguration.USERNAME;
-    private static String PASSWORD = SdkAutoConfiguration.PASSWORD;
 
-    private static final String GRAND_TYPE = "password";
+    private static final String GRAND_TYPE = "client_credentials";
 
     private static final String SCOPE = "user.read user.write";
 
     private static final String CHARSET = "utf-8";
 
-    private static final IailabAuthClient iailabAuthClient = new IailabAuthClient();
+    private static final IailabClient iailabClient = new IailabClient();
 
-    public static IailabAuthClient getInstance() {
-        return iailabAuthClient;
+    public static IailabClient getInstance() {
+        return iailabClient;
     }
 
 
@@ -89,10 +88,9 @@
         headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
         // 1.2 构建authenticate请求URL
         String authenticateUrl = BASE_URL + "/system/oauth2/token?"
-                // 密码模式的参数
+                // 客户端模式的参数
                 + "&grant_type=" + GRAND_TYPE
                 + "&username=" + USERNAME
-                + "&password=" + PASSWORD
                 + "&scope=" + SCOPE;
         // 2. 执行请求
         ResponseEntity<Map<String, Object>> exchange = restTemplate.exchange(
@@ -106,8 +104,6 @@
         Object code = authMap.get("code");
         if (SdkErrorCodeConstants.AUTH_BAD_CREDENTIALS.getCode().equals(code)) {
             throw exception(SdkErrorCodeConstants.AUTH_BAD_CREDENTIALS);
-        } else if (SdkErrorCodeConstants.AUTH_LOGIN_BAD_CREDENTIALS.getCode().equals(code)) {
-            throw exception(SdkErrorCodeConstants.AUTH_LOGIN_BAD_CREDENTIALS);
         } else if (SdkErrorCodeConstants.OAUTH2_CLIENT_REDIRECT_URI_NOT_MATCH.getCode().equals(code)) {
             throw exception(SdkErrorCodeConstants.OAUTH2_CLIENT_REDIRECT_URI_NOT_MATCH);
         } else if (SdkErrorCodeConstants.OAUTH2_CLIENT_CLIENT_SECRET_ERROR.getCode().equals(code)) {
diff --git a/iailab-plat-sdk/src/main/java/com/iailab/sdk/auth/config/SdkAutoConfiguration.java b/iailab-plat-sdk/src/main/java/com/iailab/sdk/auth/config/SdkAutoConfiguration.java
index 7338330..d8811da 100644
--- a/iailab-plat-sdk/src/main/java/com/iailab/sdk/auth/config/SdkAutoConfiguration.java
+++ b/iailab-plat-sdk/src/main/java/com/iailab/sdk/auth/config/SdkAutoConfiguration.java
@@ -32,9 +32,6 @@
     @Value("${iailab.token.username}")
     public String username;
 
-    @Value("${iailab.token.password}")
-    public String password;
-
     public static String BASE_URL;
 
     public static Long TENANT_ID;
@@ -45,8 +42,6 @@
 
     public static String USERNAME;
 
-    public static String PASSWORD;
-
     @PostConstruct
     public void initUrl() {
         BASE_URL = this.baseUrl;
@@ -54,6 +49,5 @@
         CLIENT_ID = this.clientId;
         CLIENT_SECRET = this.clientSecret;
         USERNAME = this.username;
-        PASSWORD = this.password;
     }
 }
diff --git a/iailab-plat-sdk/src/main/java/com/iailab/sdk/auth/constants/SdkErrorCodeConstants.java b/iailab-plat-sdk/src/main/java/com/iailab/sdk/auth/constants/SdkErrorCodeConstants.java
index dbbdfea..cd22bec 100644
--- a/iailab-plat-sdk/src/main/java/com/iailab/sdk/auth/constants/SdkErrorCodeConstants.java
+++ b/iailab-plat-sdk/src/main/java/com/iailab/sdk/auth/constants/SdkErrorCodeConstants.java
@@ -5,7 +5,6 @@
 public class SdkErrorCodeConstants {
 
     // ========== AUTH 模块 1-002-000-000 ==========
-    public static final ErrorCode AUTH_LOGIN_BAD_CREDENTIALS = new ErrorCode(1_002_000_000, "登录失败,账号密码不正确");
     public static final ErrorCode AUTH_BAD_CREDENTIALS = new ErrorCode(400, "client_id 或 client_secret 未正确传递");
     public static final ErrorCode AUTH_REFRESH_TOKEN_ERROR = new ErrorCode(1_002_000_001, "刷新token失败");
     public static final ErrorCode OAUTH2_CLIENT_NOT_EXISTS = new ErrorCode(1_002_020_000, "OAuth2 客户端不存在");
diff --git a/iailab-plat-sdk/src/main/java/com/iailab/sdk/util/http/IailabHttpUtils.java b/iailab-plat-sdk/src/main/java/com/iailab/sdk/util/http/IailabHttpUtils.java
index ad149fb..2a66286 100644
--- a/iailab-plat-sdk/src/main/java/com/iailab/sdk/util/http/IailabHttpUtils.java
+++ b/iailab-plat-sdk/src/main/java/com/iailab/sdk/util/http/IailabHttpUtils.java
@@ -1,6 +1,6 @@
 package com.iailab.sdk.util.http;
 
-import com.iailab.sdk.auth.client.IailabAuthClient;
+import com.iailab.sdk.auth.client.IailabClient;
 import com.iailab.sdk.auth.config.SdkAutoConfiguration;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpResponse;
@@ -54,7 +54,7 @@
             //设置参数
             httpGet.addHeader("Accept", "application/json");
             httpGet.addHeader("Content-Type", "application/json;charset=UTF-8");
-            httpGet.addHeader("Authorization", "Bearer " + IailabAuthClient.accessToken);
+            httpGet.addHeader("Authorization", "Bearer " + IailabClient.accessToken);
             httpGet.addHeader("Tenant-Id", SdkAutoConfiguration.TENANT_ID.toString());
             HttpResponse response = httpClient.execute(httpGet);
             if (response != null) {
@@ -88,7 +88,7 @@
             //设置参数
             httpPost.addHeader("Accept", "application/json");
             httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
-            httpPost.addHeader("Authorization", "Bearer " + IailabAuthClient.accessToken);
+            httpPost.addHeader("Authorization", "Bearer " + IailabClient.accessToken);
             httpPost.addHeader("Tenant-Id", SdkAutoConfiguration.TENANT_ID.toString());
             StringEntity stringEntity = new StringEntity(json);
             stringEntity.setContentEncoding("UTF-8");
@@ -109,10 +109,10 @@
 
     private static void checkToken() throws Exception {
         //第一次请求或者token过期,需要重新获取token
-        if(ObjectUtils.isEmpty(IailabAuthClient.accessToken)) {
-            IailabAuthClient.authenticate();
-        } else if (IailabAuthClient.expireTime < System.currentTimeMillis() / 1000) {
-            IailabAuthClient.refreshToken();
+        if(ObjectUtils.isEmpty(IailabClient.accessToken)) {
+            IailabClient.authenticate();
+        } else if (IailabClient.expireTime < System.currentTimeMillis() / 1000) {
+            IailabClient.refreshToken();
         }
     }
 
diff --git a/iailab-plat-sdk/src/test/java/com/iailab/sdk/IailabClientTest.java b/iailab-plat-sdk/src/test/java/com/iailab/sdk/IailabClientTest.java
index 00021aa..13fc2ca 100644
--- a/iailab-plat-sdk/src/test/java/com/iailab/sdk/IailabClientTest.java
+++ b/iailab-plat-sdk/src/test/java/com/iailab/sdk/IailabClientTest.java
@@ -1,14 +1,12 @@
 package com.iailab.sdk;
 
-import com.iailab.sdk.auth.client.IailabAuthClient;
+import com.iailab.sdk.auth.client.IailabClient;
 import com.iailab.sdk.auth.client.common.pojo.CommonResult;
-import com.iailab.sdk.auth.client.dto.ApiDataQueryDTO;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.junit4.SpringRunner;
 
-import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -26,13 +24,13 @@
 
     @Test
     public void testLogin() {
-        IailabAuthClient.authenticate();
+        IailabClient.authenticate();
     }
 
     @Test
     public void testRefreshToken() throws Exception {
         // 准备参数
-        IailabAuthClient.refreshToken();
+        IailabClient.refreshToken();
     }
 
     @Test
@@ -44,7 +42,7 @@
         itemNos.add("P0000100010");
         params.put("itemNos", itemNos);
         // 准备参数
-        String s = IailabAuthClient.doHttp("POST", "http://172.16.8.100/admin-api/data/api/query-plans/chart", params);
+        String s = IailabClient.doHttp("POST", "http://172.16.8.100/admin-api/data/api/query-plans/chart", params);
         System.out.println(s);
     }
 
@@ -52,7 +50,7 @@
     public void testQueryPointsRealValue() {
         List<String> pointNos = new ArrayList<>();
         pointNos.add("111");
-        CommonResult<Map<String, Object>> stringObjectMap = IailabAuthClient.getInstance().queryPointsRealValue(pointNos);
+        CommonResult<Map<String, Object>> stringObjectMap = IailabClient.getInstance().queryPointsRealValue(pointNos);
         System.out.println(stringObjectMap);
     }
 

--
Gitblit v1.9.3