修复加密的bug

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