mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-30 15:53:56 +08:00
修复加密的bug
This commit is contained in:
@@ -36,8 +36,10 @@ public class SensitiveDataUtils {
|
||||
|
||||
int length = value.length();
|
||||
if (length <= 8) {
|
||||
// 短字符串保留前2后2
|
||||
return value.substring(0, 2) + "****" + value.substring(length - 2);
|
||||
} else {
|
||||
// 长字符串保留前4后4
|
||||
int maskLength = length - 8;
|
||||
StringBuilder maskBuilder = new StringBuilder();
|
||||
for (int i = 0; i < maskLength; i++) {
|
||||
|
||||
+6
-3
@@ -376,7 +376,8 @@ public class ConfigServiceImpl implements ConfigService {
|
||||
if (modelIds[i] == null) {
|
||||
continue;
|
||||
}
|
||||
ModelConfigEntity model = modelConfigService.getModelById(modelIds[i], isCache);
|
||||
// 关键:第三个参数传false,确保获取原始密钥
|
||||
ModelConfigEntity model = modelConfigService.getModelById(modelIds[i], isCache, false);
|
||||
if (model == null) {
|
||||
continue;
|
||||
}
|
||||
@@ -424,14 +425,16 @@ public class ConfigServiceImpl implements ConfigService {
|
||||
if ("LLM".equals(modelTypes[i])) {
|
||||
if (StringUtils.isNotBlank(intentLLMModelId)) {
|
||||
if (!typeConfig.containsKey(intentLLMModelId)) {
|
||||
ModelConfigEntity intentLLM = modelConfigService.getModelById(intentLLMModelId, isCache);
|
||||
// 修改这里:添加isMaskSensitive=false参数
|
||||
ModelConfigEntity intentLLM = modelConfigService.getModelById(intentLLMModelId, isCache, false);
|
||||
typeConfig.put(intentLLM.getId(), intentLLM.getConfigJson());
|
||||
}
|
||||
}
|
||||
if (StringUtils.isNotBlank(memLocalShortLLMModelId)) {
|
||||
if (!typeConfig.containsKey(memLocalShortLLMModelId)) {
|
||||
// 修改这里:添加isMaskSensitive=false参数
|
||||
ModelConfigEntity memLocalShortLLM = modelConfigService
|
||||
.getModelById(memLocalShortLLMModelId, isCache);
|
||||
.getModelById(memLocalShortLLMModelId, isCache, false);
|
||||
typeConfig.put(memLocalShortLLM.getId(), memLocalShortLLM.getConfigJson());
|
||||
}
|
||||
}
|
||||
|
||||
+14
-4
@@ -35,8 +35,18 @@ public interface ModelConfigService extends BaseService<ModelConfigEntity> {
|
||||
/**
|
||||
* 根据ID获取模型配置
|
||||
*
|
||||
* @param id 模型ID
|
||||
* @param isCache 是否缓存
|
||||
* @param id 模型ID
|
||||
* @param isCache 是否缓存
|
||||
* @param isMaskSensitive 是否掩码敏感信息
|
||||
* @return 模型配置实体
|
||||
*/
|
||||
ModelConfigEntity getModelById(String id, boolean isCache, boolean isMaskSensitive);
|
||||
|
||||
/**
|
||||
* 根据ID获取模型配置(默认掩码敏感信息)
|
||||
*
|
||||
* @param id 模型ID
|
||||
* @param isCache 是否缓存
|
||||
* @return 模型配置实体
|
||||
*/
|
||||
ModelConfigEntity getModelById(String id, boolean isCache);
|
||||
@@ -44,8 +54,8 @@ public interface ModelConfigService extends BaseService<ModelConfigEntity> {
|
||||
/**
|
||||
* 设置默认模型
|
||||
*
|
||||
* @param modelType 模型类型
|
||||
* @param isDefault 是否默认
|
||||
* @param modelType 模型类型
|
||||
* @param isDefault 是否默认(1:是,0:否)
|
||||
*/
|
||||
void setDefaultModel(String modelType, int isDefault);
|
||||
}
|
||||
|
||||
+20
-6
@@ -212,16 +212,30 @@ public class ModelConfigServiceImpl extends BaseServiceImpl<ModelConfigDao, Mode
|
||||
|
||||
@Override
|
||||
public ModelConfigEntity getModelById(String id, boolean isCache) {
|
||||
return getModelById(id, isCache, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ModelConfigEntity getModelById(String id, boolean isCache, boolean isMaskSensitive) {
|
||||
if (StringUtils.isBlank(id)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ModelConfigEntity entity = null;
|
||||
String cacheKey = RedisKeys.getModelConfigById(id);
|
||||
|
||||
if (isCache) {
|
||||
String cacheKey = RedisKeys.getModelConfigById(id);
|
||||
// 从缓存获取
|
||||
entity = (ModelConfigEntity) redisUtils.get(cacheKey);
|
||||
if (entity != null) {
|
||||
// 修改:根据isMaskSensitive参数重新处理敏感信息
|
||||
if (!isMaskSensitive && entity.getConfigJson() != null) {
|
||||
// 如果需要获取原始配置,但缓存的是掩码后的配置,则从数据库重新获取
|
||||
ModelConfigEntity originalEntity = modelConfigDao.selectById(id);
|
||||
if (originalEntity != null) {
|
||||
entity.setConfigJson(originalEntity.getConfigJson());
|
||||
}
|
||||
}
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
@@ -229,12 +243,13 @@ public class ModelConfigServiceImpl extends BaseServiceImpl<ModelConfigDao, Mode
|
||||
// 从数据库获取数据
|
||||
entity = modelConfigDao.selectById(id);
|
||||
if (entity != null) {
|
||||
if (entity.getConfigJson() != null) {
|
||||
// 根据isMaskSensitive参数决定是否掩码敏感信息
|
||||
if (isMaskSensitive && entity.getConfigJson() != null) {
|
||||
entity.setConfigJson(maskSensitiveFields(entity.getConfigJson()));
|
||||
}
|
||||
|
||||
if (isCache) {
|
||||
redisUtils.set(RedisKeys.getModelConfigById(id), entity);
|
||||
redisUtils.set(cacheKey, entity);
|
||||
}
|
||||
}
|
||||
return entity;
|
||||
@@ -353,7 +368,6 @@ public class ModelConfigServiceImpl extends BaseServiceImpl<ModelConfigDao, Mode
|
||||
modelConfigEntity.setModelName(modelConfigBodyDTO.getModelName());
|
||||
modelConfigEntity.setSort(modelConfigBodyDTO.getSort());
|
||||
modelConfigEntity.setIsEnabled(modelConfigBodyDTO.getIsEnabled());
|
||||
|
||||
// 3. 处理配置JSON,仅更新非敏感字段和明确修改的敏感字段
|
||||
if (modelConfigBodyDTO.getConfigJson() != null && originalEntity.getConfigJson() != null) {
|
||||
JSONObject originalJson = originalEntity.getConfigJson();
|
||||
@@ -365,8 +379,8 @@ public class ModelConfigServiceImpl extends BaseServiceImpl<ModelConfigDao, Mode
|
||||
|
||||
// 如果是敏感字段,需要确认是否真的被修改(前端传入的可能是掩码后的值)
|
||||
if (SensitiveDataUtils.isSensitiveField(key)) {
|
||||
// 只有当传入的值不是掩码格式时,才认为是真实修改
|
||||
if (value instanceof String && !isMaskedValue((String) value)) {
|
||||
|
||||
if (value instanceof String && !SensitiveDataUtils.isMaskedValue((String) value)) {
|
||||
updatedJson.put(key, value);
|
||||
}
|
||||
} else if (value instanceof JSONObject) {
|
||||
|
||||
Reference in New Issue
Block a user