mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-27 01:23:55 +08:00
优化:方法分割提取
--AgentMcpAccessPointServiceImpl.java 方法分割提取共用 1.获取密钥方法 2.获取智能体mcp接入点url部分 3.获取对智能体id加密的token
This commit is contained in:
+62
-21
@@ -3,6 +3,7 @@ package xiaozhi.modules.agent.service.impl;
|
|||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import xiaozhi.common.constant.Constant;
|
import xiaozhi.common.constant.Constant;
|
||||||
import xiaozhi.common.utils.AESUtils;
|
import xiaozhi.common.utils.AESUtils;
|
||||||
@@ -28,35 +29,75 @@ public class AgentMcpAccessPointServiceImpl implements AgentMcpAccessPointServic
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
// 使用md5对智能体id进行加密
|
|
||||||
String md5 = HashEncryptionUtil.Md5hexDigest(id);
|
|
||||||
// aes需要加密文本
|
|
||||||
String json = "{\"agentId\": %s}".formatted(md5);
|
|
||||||
URI uri = new URI(url);
|
URI uri = new URI(url);
|
||||||
String query = uri.getQuery();
|
// 获取智能体mcp的url前缀
|
||||||
// 获取aes加密密钥
|
String agentMcpUrl = getAgentMcpUrl(uri);
|
||||||
String str = "key=";
|
// 获取密钥
|
||||||
String key = query.substring(query.indexOf(str) + str.length());
|
String key = getSecretKey(uri);
|
||||||
// 加密后成token值
|
// 获取加密的token
|
||||||
String encryptToken = AESUtils.encrypt(key, json);
|
String encryptToken = encryptToken(id, key);
|
||||||
// 获取路径组成部分
|
|
||||||
// 获取协议
|
|
||||||
String wsScheme = (uri.getScheme().equals("https")) ? "wss" : "ws" ;
|
|
||||||
// 获取主机,端口,路径
|
|
||||||
String path = uri.getSchemeSpecificPart();
|
|
||||||
// 获取参数
|
|
||||||
path = path.substring(0,path.lastIndexOf("/"));
|
|
||||||
// 返回智能体Mcp路径的格式
|
// 返回智能体Mcp路径的格式
|
||||||
String AgentMcpUrl = "%s:%s/mcp?token=%s";
|
String AgentMcpUrl = "%s/mcp?token=%s";
|
||||||
return AgentMcpUrl.formatted(wsScheme,path,encryptToken);
|
return AgentMcpUrl.formatted(agentMcpUrl,encryptToken);
|
||||||
} catch (URISyntaxException e) {
|
} catch (URISyntaxException e) {
|
||||||
log.error("路径格式不正确路径:{},\n错误信息:{}",url,e.getMessage());
|
log.error("路径格式不正确路径:{},\n错误信息:{}",url,e.getMessage());
|
||||||
throw new RuntimeException("mcp的地址存在错误,请进入参数管理修改mcp接入点地址");
|
throw new RuntimeException("mcp的地址存在错误,请进入参数管理修改mcp接入点地址");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> getAgentMcpToolsList(String id) {
|
public List<String> getAgentMcpToolsList(String id) {
|
||||||
return List.of();
|
List<String> list = List.of();
|
||||||
|
String url = sysParamsService.getValue(Constant.SERVER_MCP_ENDPOINT, true);
|
||||||
|
if (StringUtils.isBlank(url)) {
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取密钥
|
||||||
|
* @param uri mcp地址
|
||||||
|
* @return 密钥
|
||||||
|
*/
|
||||||
|
private static String getSecretKey(URI uri) {
|
||||||
|
// 获取参数
|
||||||
|
String query = uri.getQuery();
|
||||||
|
// 获取aes加密密钥
|
||||||
|
String str = "key=";
|
||||||
|
return query.substring(query.indexOf(str) + str.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取智能体mcp接入点url
|
||||||
|
* @param uri mcp地址
|
||||||
|
* @return 智能体mcp接入点url
|
||||||
|
*/
|
||||||
|
private String getAgentMcpUrl(URI uri) {
|
||||||
|
// 获取协议
|
||||||
|
String wsScheme = (uri.getScheme().equals("https")) ? "wss" : "ws" ;
|
||||||
|
// 获取主机,端口,路径
|
||||||
|
String path = uri.getSchemeSpecificPart();
|
||||||
|
// 获取到最后一个/前的path
|
||||||
|
path = path.substring(0,path.lastIndexOf("/"));
|
||||||
|
return wsScheme+":"+path;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取对智能体id加密的token
|
||||||
|
* @param agentId 智能体id
|
||||||
|
* @param key 加密密钥
|
||||||
|
* @return 加密后token
|
||||||
|
*/
|
||||||
|
private static String encryptToken(String agentId, String key) {
|
||||||
|
// 使用md5对智能体id进行加密
|
||||||
|
String md5 = HashEncryptionUtil.Md5hexDigest(agentId);
|
||||||
|
// aes需要加密文本
|
||||||
|
String json = "{\"agentId\": %s}".formatted(md5);
|
||||||
|
// 加密后成token值
|
||||||
|
return AESUtils.encrypt(key, json);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user