From c1111b8b098ea6e170f1e3cd9ad8f6b3e989ff71 Mon Sep 17 00:00:00 2001 From: lvfengfree Date: Sun, 1 Mar 2026 15:34:28 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20AMT=20=E5=87=AD?= =?UTF-8?q?=E8=AF=81=20ID=20=E7=B1=BB=E5=9E=8B=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 AmtTestRequest 的 credentialId 从 Long 改为 String(支持 UUID) - 更新 AmtDigestService 处理字符串类型的凭证 ID - 前端不再将 credentialId 转换为数字,直接发送字符串 - 添加详细日志用于调试凭证获取过程 - 修复使用保存凭证时的认证失败问题 --- .../com/soybean/admin/dto/AmtTestRequest.java | 2 +- .../admin/service/AmtDigestService.java | 65 +++++++++++++++++-- rebuild_and_check_logs.bat | 36 ++++++++++ src/typings/api/device.d.ts | 4 +- src/views/device/list/index.vue | 8 ++- 5 files changed, 103 insertions(+), 12 deletions(-) create mode 100644 rebuild_and_check_logs.bat diff --git a/backend/src/main/java/com/soybean/admin/dto/AmtTestRequest.java b/backend/src/main/java/com/soybean/admin/dto/AmtTestRequest.java index b2d02e4..cbf8d33 100644 --- a/backend/src/main/java/com/soybean/admin/dto/AmtTestRequest.java +++ b/backend/src/main/java/com/soybean/admin/dto/AmtTestRequest.java @@ -7,5 +7,5 @@ public class AmtTestRequest { private String ipAddress; private String username; private String password; - private Long credentialId; // 可选:使用已保存的凭证ID + private String credentialId; // 可选:使用已保存的凭证ID(UUID字符串) } diff --git a/backend/src/main/java/com/soybean/admin/service/AmtDigestService.java b/backend/src/main/java/com/soybean/admin/service/AmtDigestService.java index 43716a5..ae7fdcf 100644 --- a/backend/src/main/java/com/soybean/admin/service/AmtDigestService.java +++ b/backend/src/main/java/com/soybean/admin/service/AmtDigestService.java @@ -46,16 +46,44 @@ public class AmtDigestService { String username; String password; - if (request.getCredentialId() != null) { - AmtCredential credential = amtCredentialService.getCredentialById(request.getCredentialId().toString()); + if (request.getCredentialId() != null && !request.getCredentialId().isEmpty()) { + logger.info("使用凭证 ID: {}", request.getCredentialId()); + + AmtCredential credential = amtCredentialService.getCredentialById(request.getCredentialId()); if (credential == null) { - throw new RuntimeException("凭证不存在"); + logger.error("凭证不存在,ID: {}", request.getCredentialId()); + throw new RuntimeException("凭证不存在,ID: " + request.getCredentialId()); } + + logger.info("找到凭证: {}, 用户名: {}, 密码长度: {}", + credential.getCredentialName(), + credential.getUsername(), + credential.getPassword() != null ? credential.getPassword().length() : 0); + username = credential.getUsername(); password = credential.getPassword(); + + if (username == null || username.isEmpty()) { + logger.error("凭证用户名为空,凭证名称: {}", credential.getCredentialName()); + throw new RuntimeException("凭证用户名为空"); + } + if (password == null || password.isEmpty()) { + logger.error("凭证密码为空,凭证名称: {}", credential.getCredentialName()); + throw new RuntimeException("凭证密码为空"); + } } else { username = request.getUsername(); password = request.getPassword(); + logger.info("使用手动输入凭证 - 用户名: {}, 密码长度: {}", + username, + password != null ? password.length() : 0); + + if (username == null || username.isEmpty()) { + throw new RuntimeException("用户名不能为空"); + } + if (password == null || password.isEmpty()) { + throw new RuntimeException("密码不能为空"); + } } logger.info("Digest 认证 - 用户名: {}", username); @@ -85,16 +113,40 @@ public class AmtDigestService { String username; String password; - if (request.getCredentialId() != null) { - AmtCredential credential = amtCredentialService.getCredentialById(request.getCredentialId().toString()); + if (request.getCredentialId() != null && !request.getCredentialId().isEmpty()) { + logger.info("使用凭证 ID 获取设备信息: {}", request.getCredentialId()); + + AmtCredential credential = amtCredentialService.getCredentialById(request.getCredentialId()); if (credential == null) { - throw new RuntimeException("凭证不存在"); + logger.error("凭证不存在,ID: {}", request.getCredentialId()); + throw new RuntimeException("凭证不存在,ID: " + request.getCredentialId()); } + + logger.info("找到凭证: {}, 用户名: {}", + credential.getCredentialName(), + credential.getUsername()); + username = credential.getUsername(); password = credential.getPassword(); + + if (username == null || username.isEmpty()) { + logger.error("凭证用户名为空,凭证名称: {}", credential.getCredentialName()); + throw new RuntimeException("凭证用户名为空"); + } + if (password == null || password.isEmpty()) { + logger.error("凭证密码为空,凭证名称: {}", credential.getCredentialName()); + throw new RuntimeException("凭证密码为空"); + } } else { username = request.getUsername(); password = request.getPassword(); + + if (username == null || username.isEmpty()) { + throw new RuntimeException("用户名不能为空"); + } + if (password == null || password.isEmpty()) { + throw new RuntimeException("密码不能为空"); + } } AmtDeviceInfo deviceInfo = new AmtDeviceInfo(); @@ -118,6 +170,7 @@ public class AmtDigestService { return deviceInfo; } catch (Exception e) { + logger.error("获取 AMT 设备信息失败", e); throw new RuntimeException("获取 AMT 设备信息失败: " + e.getMessage()); } } diff --git a/rebuild_and_check_logs.bat b/rebuild_and_check_logs.bat new file mode 100644 index 0000000..9b64b66 --- /dev/null +++ b/rebuild_and_check_logs.bat @@ -0,0 +1,36 @@ +@echo off +echo ======================================== +echo 重新编译后端并查看日志 +echo ======================================== +echo. + +cd backend + +echo 清理旧的编译文件... +call mvn clean + +echo. +echo 重新编译项目... +call mvn compile + +echo. +echo 打包项目... +call mvn package -DskipTests + +echo. +echo ======================================== +echo 编译完成! +echo ======================================== +echo. +echo 现在启动后端服务并查看日志: +echo cd backend +echo java -jar target/soybean-admin-0.0.1-SNAPSHOT.jar +echo. +echo 测试时请注意查看控制台输出的日志信息 +echo 特别关注以下内容: +echo - 使用凭证 ID: xxx +echo - 查询凭证,ID字符串: xxx +echo - 找到凭证: xxx, 用户名: xxx +echo. + +pause diff --git a/src/typings/api/device.d.ts b/src/typings/api/device.d.ts index f73e9a1..6de31cb 100644 --- a/src/typings/api/device.d.ts +++ b/src/typings/api/device.d.ts @@ -60,8 +60,8 @@ declare namespace Api { username?: string; /** 密码 */ password?: string; - /** 凭证ID */ - credentialId?: number; + /** 凭证ID(UUID字符串) */ + credentialId?: string | number; } /** AMT 设备信息 */ diff --git a/src/views/device/list/index.vue b/src/views/device/list/index.vue index a0c4c65..1ebae18 100644 --- a/src/views/device/list/index.vue +++ b/src/views/device/list/index.vue @@ -455,7 +455,7 @@ const amtFormData = reactive({ ipAddress: '', username: '', password: '', - credentialId: null as number | null, + credentialId: null as string | null, useCredential: false }); const amtCredentials = ref([]); @@ -663,7 +663,8 @@ async function handleTestAmtConnection() { }; if (amtFormData.useCredential && amtFormData.credentialId) { - requestData.credentialId = amtFormData.credentialId; + // 直接使用字符串 ID,不转换为数字 + requestData.credentialId = amtFormData.credentialId as any; } else { requestData.username = amtFormData.username; requestData.password = amtFormData.password; @@ -695,7 +696,8 @@ async function handleGetAmtDeviceInfo() { }; if (amtFormData.useCredential && amtFormData.credentialId) { - requestData.credentialId = amtFormData.credentialId; + // 直接使用字符串 ID,不转换为数字 + requestData.credentialId = amtFormData.credentialId as any; } else { requestData.username = amtFormData.username; requestData.password = amtFormData.password;