fix: 修复 AMT 凭证 ID 类型问题

- 将 AmtTestRequest 的 credentialId 从 Long 改为 String(支持 UUID)
- 更新 AmtDigestService 处理字符串类型的凭证 ID
- 前端不再将 credentialId 转换为数字,直接发送字符串
- 添加详细日志用于调试凭证获取过程
- 修复使用保存凭证时的认证失败问题
This commit is contained in:
lvfengfree 2026-03-01 15:34:28 +08:00
parent afb327b036
commit c1111b8b09
5 changed files with 103 additions and 12 deletions

View File

@ -7,5 +7,5 @@ public class AmtTestRequest {
private String ipAddress; private String ipAddress;
private String username; private String username;
private String password; private String password;
private Long credentialId; // 可选使用已保存的凭证ID private String credentialId; // 可选使用已保存的凭证IDUUID字符串
} }

View File

@ -46,16 +46,44 @@ public class AmtDigestService {
String username; String username;
String password; String password;
if (request.getCredentialId() != null) { if (request.getCredentialId() != null && !request.getCredentialId().isEmpty()) {
AmtCredential credential = amtCredentialService.getCredentialById(request.getCredentialId().toString()); logger.info("使用凭证 ID: {}", request.getCredentialId());
AmtCredential credential = amtCredentialService.getCredentialById(request.getCredentialId());
if (credential == null) { 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(); username = credential.getUsername();
password = credential.getPassword(); 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 { } else {
username = request.getUsername(); username = request.getUsername();
password = request.getPassword(); 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); logger.info("Digest 认证 - 用户名: {}", username);
@ -85,16 +113,40 @@ public class AmtDigestService {
String username; String username;
String password; String password;
if (request.getCredentialId() != null) { if (request.getCredentialId() != null && !request.getCredentialId().isEmpty()) {
AmtCredential credential = amtCredentialService.getCredentialById(request.getCredentialId().toString()); logger.info("使用凭证 ID 获取设备信息: {}", request.getCredentialId());
AmtCredential credential = amtCredentialService.getCredentialById(request.getCredentialId());
if (credential == null) { 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(); username = credential.getUsername();
password = credential.getPassword(); 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 { } else {
username = request.getUsername(); username = request.getUsername();
password = request.getPassword(); password = request.getPassword();
if (username == null || username.isEmpty()) {
throw new RuntimeException("用户名不能为空");
}
if (password == null || password.isEmpty()) {
throw new RuntimeException("密码不能为空");
}
} }
AmtDeviceInfo deviceInfo = new AmtDeviceInfo(); AmtDeviceInfo deviceInfo = new AmtDeviceInfo();
@ -118,6 +170,7 @@ public class AmtDigestService {
return deviceInfo; return deviceInfo;
} catch (Exception e) { } catch (Exception e) {
logger.error("获取 AMT 设备信息失败", e);
throw new RuntimeException("获取 AMT 设备信息失败: " + e.getMessage()); throw new RuntimeException("获取 AMT 设备信息失败: " + e.getMessage());
} }
} }

View File

@ -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

View File

@ -60,8 +60,8 @@ declare namespace Api {
username?: string; username?: string;
/** 密码 */ /** 密码 */
password?: string; password?: string;
/** 凭证ID */ /** 凭证IDUUID字符串 */
credentialId?: number; credentialId?: string | number;
} }
/** AMT 设备信息 */ /** AMT 设备信息 */

View File

@ -455,7 +455,7 @@ const amtFormData = reactive({
ipAddress: '', ipAddress: '',
username: '', username: '',
password: '', password: '',
credentialId: null as number | null, credentialId: null as string | null,
useCredential: false useCredential: false
}); });
const amtCredentials = ref<AmtCredential[]>([]); const amtCredentials = ref<AmtCredential[]>([]);
@ -663,7 +663,8 @@ async function handleTestAmtConnection() {
}; };
if (amtFormData.useCredential && amtFormData.credentialId) { if (amtFormData.useCredential && amtFormData.credentialId) {
requestData.credentialId = amtFormData.credentialId; // 使 ID
requestData.credentialId = amtFormData.credentialId as any;
} else { } else {
requestData.username = amtFormData.username; requestData.username = amtFormData.username;
requestData.password = amtFormData.password; requestData.password = amtFormData.password;
@ -695,7 +696,8 @@ async function handleGetAmtDeviceInfo() {
}; };
if (amtFormData.useCredential && amtFormData.credentialId) { if (amtFormData.useCredential && amtFormData.credentialId) {
requestData.credentialId = amtFormData.credentialId; // 使 ID
requestData.credentialId = amtFormData.credentialId as any;
} else { } else {
requestData.username = amtFormData.username; requestData.username = amtFormData.username;
requestData.password = amtFormData.password; requestData.password = amtFormData.password;