fix:数据加密

This commit is contained in:
zhangwenzan 2025-07-29 16:14:18 +08:00
parent 9dbe015e0c
commit 86966e75d9
7 changed files with 106 additions and 22 deletions

View File

@ -78,7 +78,7 @@ public class EncryptionService {
// 解码Base64密文
byte[] decoded = Base64.getDecoder().decode(ciphertext);
// 提取IV
// 提取IV固定取前12字节
byte[] iv = new byte[GCM_IV_LENGTH];
System.arraycopy(decoded, 0, iv, 0, iv.length);

View File

@ -48,7 +48,8 @@ public class SensitiveDataConverter extends AbstractJsonTypeHandler<String> impl
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, String s, JdbcType jdbcType) throws SQLException {
if (s != null && !s.isEmpty() && !s.startsWith(Const.ENCRYPTED_PREFIX)) {
s = getEncryptionService().encryptAes(s);
// 加密后添加前缀标识
s = Const.ENCRYPTED_PREFIX + getEncryptionService().encryptAes(s);
}
preparedStatement.setString(i, s);
}
@ -57,7 +58,8 @@ public class SensitiveDataConverter extends AbstractJsonTypeHandler<String> impl
public String getNullableResult(ResultSet resultSet, String s) throws SQLException {
String value = resultSet.getString(s);
if (value != null && value.startsWith(Const.ENCRYPTED_PREFIX)) {
value = getEncryptionService().decryptAes(value);
// 修复移除前缀后再解密
value = getEncryptionService().decryptAes(value.substring(Const.ENCRYPTED_PREFIX.length()));
}
return value;
}
@ -66,7 +68,8 @@ public class SensitiveDataConverter extends AbstractJsonTypeHandler<String> impl
public String getNullableResult(ResultSet resultSet, int i) throws SQLException {
String value = resultSet.getString(i);
if (value != null && value.startsWith(Const.ENCRYPTED_PREFIX)) {
value = getEncryptionService().decryptAes(value);
// 修复移除前缀后再解密
value = getEncryptionService().decryptAes(value.substring(Const.ENCRYPTED_PREFIX.length()));
}
return value;
}
@ -75,7 +78,8 @@ public class SensitiveDataConverter extends AbstractJsonTypeHandler<String> impl
public String getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
String value = callableStatement.getString(i);
if (value != null && value.startsWith(Const.ENCRYPTED_PREFIX)) {
value = getEncryptionService().decryptAes(value);
// 修复移除前缀后再解密
value = getEncryptionService().decryptAes(value.substring(Const.ENCRYPTED_PREFIX.length()));
}
return value;
}

View File

@ -99,6 +99,7 @@ public enum CrmCodeEnum implements ResultCode {
THE_FIELD_DETAIL_TABLE_FORMAT_ERROR(2089,"清设置表格内的具体字段!"),
CRM_RECEIVABLES_PLAN_ADD_ERROR(2090,"只有审核通过或审核中的合同才可以添加回款计划!"),
CRM_CUSTOMER_POOL_NOT_IS_ADMIN(2091, "没有该公海权限,不能进行操作"),
CUSTOMER_XX_Y(2092, "客户信息已存在,无法重复添加"),
;

View File

@ -1,11 +1,13 @@
package com.kakarote.crm.entity.PO;
import com.baomidou.mybatisplus.annotation.*;
import com.kakarote.core.security.converter.SensitiveDataConverter;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.apache.ibatis.type.JdbcType;
import java.io.Serializable;
import java.util.Date;
@ -37,12 +39,17 @@ public class CrmContacts implements Serializable {
private Date nextTime;
@ApiModelProperty(value = "手机")
@TableField(typeHandler = SensitiveDataConverter.class, jdbcType = JdbcType.VARCHAR)
private String mobile;
@ApiModelProperty(value = "电话")
@TableField(typeHandler = SensitiveDataConverter.class, jdbcType = JdbcType.VARCHAR)
private String telephone;
@ApiModelProperty(value = "电子邮箱")
@TableField(typeHandler = SensitiveDataConverter.class, jdbcType = JdbcType.VARCHAR)
private String email;
@ApiModelProperty(value = "职务")

View File

@ -8,7 +8,7 @@ public interface ICrmOpenApiService {
Integer openAddCustomerBo(CrmAddCustomerBo crmAddCustomerBo);
//添加用户
void crmAddCustomer(CrmBusinessSaveBO crmModel,String source);
Integer crmAddCustomer(CrmBusinessSaveBO crmModel, String source);
CrmBusinessSaveBO assemblyRequestData(CrmQdInfoBo crmModel);
}

View File

@ -334,8 +334,7 @@ public class CrmCustomerServiceImpl extends BaseServiceImpl<CrmCustomerMapper, C
CrmModel crmModel;
if (id != null) {
crmModel = getBaseMapper().queryById(id, UserUtil.getUserId());
// 添加解密逻辑
decryptSensitiveData(crmModel);
// 添加解密逻辑decryptSensitiveData(crmModel);
crmModel.setLabel(CrmEnum.CUSTOMER.getType());
crmModel.setOwnerUserName(UserCacheUtil.getUserName(crmModel.getOwnerUserId()));
crmCustomerDataService.setDataByBatchId(crmModel);
@ -378,9 +377,16 @@ public class CrmCustomerServiceImpl extends BaseServiceImpl<CrmCustomerMapper, C
String[] sensitiveFields = {"mobile", "email", "idCard", "bankCard"};
for (String field : sensitiveFields) {
Object value = model.get(field);
if (value instanceof String) {
model.put(field, encryptionService.decryptAes((String) value));
System.out.println(encryptionService.decryptAes((String) value));
if (value instanceof String && ((String) value).startsWith(Const.ENCRYPTED_PREFIX)) {
try {
String decryptedValue = encryptionService.decryptAes((String) value);
model.put(field, decryptedValue);
// 移除调试输出
// System.out.println(decryptedValue);
} catch (Exception e) {
log.error("解密 {} 字段失败: {}, 原始值: {}", field, model.get("customerId"), value, e);
model.put(field, null);
}
}
}
}
@ -1125,7 +1131,39 @@ public class CrmCustomerServiceImpl extends BaseServiceImpl<CrmCustomerMapper, C
public BasePage<CrmContacts> queryContacts(CrmContactsPageBO pageEntity) {
BasePage<CrmContacts> contactsBasePage = pageEntity.parse();
String conditions = AuthUtil.getCrmAuthSql(CrmEnum.CONTACTS, 1,CrmAuthEnum.READ);
return getBaseMapper().queryContacts(contactsBasePage, pageEntity.getCustomerId(), pageEntity.getSearch(), conditions);
BasePage<CrmContacts> result = getBaseMapper().queryContacts(contactsBasePage, pageEntity.getCustomerId(), pageEntity.getSearch(), conditions);
// 手动解密敏感数据
for (CrmContacts contacts : result.getList()) {
// 恢复前缀检查并增加异常处理
if (contacts.getMobile() != null && contacts.getMobile().startsWith(Const.ENCRYPTED_PREFIX)) {
try {
contacts.setMobile(encryptionService.decryptAes(contacts.getMobile()));
} catch (Exception e) {
log.error("解密mobile失败: {}", contacts.getContactsId(), e);
contacts.setMobile(null); // 或保留原始值
}
}
// 对telephone和email字段执行相同修复
if (contacts.getTelephone() != null && contacts.getTelephone().startsWith(Const.ENCRYPTED_PREFIX)) {
try {
contacts.setTelephone(encryptionService.decryptAes(contacts.getTelephone()));
} catch (Exception e) {
log.error("解密telephone失败: {}", contacts.getContactsId(), e);
contacts.setTelephone(null);
}
}
if (contacts.getEmail() != null && contacts.getEmail().startsWith(Const.ENCRYPTED_PREFIX)) {
try {
contacts.setEmail(encryptionService.decryptAes(contacts.getEmail()));
} catch (Exception e) {
log.error("解密email失败: {}", contacts.getContactsId(), e);
contacts.setEmail(null);
}
}
}
return result;
}
@Autowired

View File

@ -1,14 +1,9 @@
package com.kakarote.crm.service.impl;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.kakarote.core.exception.CrmException;
import com.kakarote.crm.entity.BO.CrmAddCustomerBo;
import com.kakarote.crm.entity.BO.CrmBusinessSaveBO;
import com.kakarote.crm.entity.BO.CrmCustomerPoolBO;
import com.kakarote.crm.entity.BO.CrmQdInfoBo;
import com.kakarote.crm.entity.PO.CrmCustomer;
import com.kakarote.crm.entity.BO.*;
import com.kakarote.crm.entity.VO.CrmModelFiledVO;
import com.kakarote.crm.mapper.CrmFieldMapper;
import com.kakarote.crm.service.ICrmContactsService;
import com.kakarote.crm.service.ICrmCustomerService;
import com.kakarote.crm.service.ICrmOpenApiService;
import lombok.extern.slf4j.Slf4j;
@ -18,6 +13,7 @@ import org.springframework.stereotype.Service;
import java.util.*;
import static com.kakarote.core.common.SystemCodeEnum.SYSTEM_NO_AUTH;
import static com.kakarote.crm.constant.CrmCodeEnum.CUSTOMER_XX_Y;
import static com.kakarote.crm.constant.CrmPoolEnum.GSMGWZKH;
import static com.kakarote.crm.constant.CrmPoolEnum.PXQDMDPOOL;
@ -27,6 +23,9 @@ public class CrmOpenApiServiceImpl implements ICrmOpenApiService {
@Autowired
private ICrmCustomerService customerService;
@Autowired
private ICrmContactsService contactsService;
//V1
@Override
public Integer openAddCustomerBo(CrmAddCustomerBo crmAddCustomerBo) {
@ -174,16 +173,50 @@ public class CrmOpenApiServiceImpl implements ICrmOpenApiService {
list.add(fliedBcethz);
crmBusinessSaveBO.setField(list);
//添加数据
crmAddCustomer(crmBusinessSaveBO,crmModel.getSource());
Integer customerId = crmAddCustomer(crmBusinessSaveBO, crmModel.getSource());
//添加联系人
CrmContactsSaveBO contactsSaveBO = new CrmContactsSaveBO();
Map<String,Object> contacts = new HashMap<>();
contacts.put("customerId",customerId);
contacts.put("name",crmModel.getUserName());
contacts.put("mobile",crmModel.getMobile());
contacts.put("telephone",crmModel.getMobile());
contacts.put("address",crmModel.getAddress());
contacts.put("email","");
contacts.put("post","");
contacts.put("nextTime","");
contacts.put("remark","");
contactsSaveBO.setEntity(contacts);
List<CrmModelFiledVO> modelFileds = new ArrayList<>();
CrmModelFiledVO policymakers =new CrmModelFiledVO();
policymakers.setFieldId(1101853);
policymakers.setFieldName("policymakers");
policymakers.setName("是否关键决策人");
policymakers.setFieldType(2);
policymakers.setType(3);
modelFileds.add(policymakers);
CrmModelFiledVO sex =new CrmModelFiledVO();
sex.setFieldId(1101857);
sex.setFieldName("sex");
sex.setName("性别");
sex.setFieldType(2);
sex.setType(3);
modelFileds.add(sex);
contactsSaveBO.setField(modelFileds);
contactsService.addOrUpdate(contactsSaveBO,false);
return crmBusinessSaveBO;
}
public void crmAddCustomer(CrmBusinessSaveBO crmModel,String source){
public Integer crmAddCustomer(CrmBusinessSaveBO crmModel, String source){
//判断企业是否存在
Integer customerByQyjbxx = customerService.getCustomerByQyjbxx(crmModel);
//存在用户
if (customerByQyjbxx >0){
throw new CrmException(CUSTOMER_XX_Y);
}else {
//添加客户
Map<String, Object> stringObjectMap = customerService.addOrUpdate(crmModel, false, null);
@ -199,6 +232,7 @@ public class CrmOpenApiServiceImpl implements ICrmOpenApiService {
poolBO.setPoolId(GSMGWZKH.getId());
}
customerService.updateCustomerByIds(poolBO);
return customerId;
}
}