Browse Source

修改密码的功能优化

dev-V1.2
vincent 15 hours ago
parent
commit
7c050891db
  1. 47
      zdxt-modules/zdxt-system/src/main/java/org/dromara/system/controller/system/SysProfileController.java
  2. 20
      zdxt-modules/zdxt-system/src/main/java/org/dromara/system/service/ICryptoSensitiveHsmService.java
  3. 28
      zdxt-modules/zdxt-system/src/main/java/org/dromara/system/service/impl/CryptoSensitiveHsmServiceImpl.java

47
zdxt-modules/zdxt-system/src/main/java/org/dromara/system/controller/system/SysProfileController.java

@ -61,6 +61,15 @@ public class SysProfileController extends BaseController {
String postGroup = userService.selectUserPostGroup(user.getUserId()); String postGroup = userService.selectUserPostGroup(user.getUserId());
// 单独做一个vo专门给个人中心用 避免数据被脱敏 // 单独做一个vo专门给个人中心用 避免数据被脱敏
ProfileUserVo profileUser = BeanUtil.toBean(user, ProfileUserVo.class); ProfileUserVo profileUser = BeanUtil.toBean(user, ProfileUserVo.class);
// phonenumber 已为 HSM 密文(本地不可解), 改经备用字段 crypto_remark_two(AES 密文)解密为明文返回;
// 未迁移存量用户降级尝试解密 phonenumber 本身(AES 密文形态), 均失败保持原值
String phonePlain = ICryptoSensitiveHsmService.decryptWithSrnwgAes(user.getCryptoRemarkTwo());
if (phonePlain == null) {
phonePlain = ICryptoSensitiveHsmService.decryptWithSrnwgAes(user.getPhonenumber());
}
if (StringUtils.isNotBlank(phonePlain)) {
profileUser.setPhonenumber(phonePlain);
}
ProfileVo profileVo = new ProfileVo(profileUser, roleGroup, postGroup); ProfileVo profileVo = new ProfileVo(profileUser, roleGroup, postGroup);
return R.ok(profileVo); return R.ok(profileVo);
} }
@ -127,47 +136,29 @@ public class SysProfileController extends BaseController {
String salt = user.getSalt(); String salt = user.getSalt();
String loginAccount = StringUtils.isNotBlank(user.getLoginName()) ? user.getLoginName() : user.getUserName(); String loginAccount = StringUtils.isNotBlank(user.getLoginName()) ? user.getLoginName() : user.getUserName();
boolean oldPasswordCorrect = false; // 取消密码服务平台(HSM)依赖: 旧密码以备用字段 crypto_remark_one(MD5原值)校验, 兼容未迁移存量用户直比 password 列
String bizId = String.valueOf(user.getUserId());
// HSM 密文内容为 MD5(loginAccount+旧密码+salt),须以 MD5 形态校验,明文口径永远不匹配
String oldEncrypted = DigestUtil.md5Hex(loginAccount + bo.getOldPassword() + salt); String oldEncrypted = DigestUtil.md5Hex(loginAccount + bo.getOldPassword() + salt);
try { boolean oldPasswordCorrect = oldEncrypted.equals(user.getCryptoRemarkOne())
oldPasswordCorrect = ICryptoSensitiveHsmService.verifyPassword("sys_user", bizId, oldEncrypted); || oldEncrypted.equals(user.getPassword());
} catch (Exception e) {
log.warn("HSM校验旧密码失败, userId={}, 降级MD5校验", user.getUserId());
}
if (!oldPasswordCorrect) {
oldPasswordCorrect = oldEncrypted.equals(user.getPassword());
}
if (!oldPasswordCorrect) { if (!oldPasswordCorrect) {
return R.fail("修改密码失败,旧密码错误"); return R.fail("修改密码失败,旧密码错误");
} }
String newEncrypted = DigestUtil.md5Hex(loginAccount + bo.getNewPassword() + salt); String newEncrypted = DigestUtil.md5Hex(loginAccount + bo.getNewPassword() + salt);
// 新旧相同校验: 迁移后 user.getPassword() 为 HSM 密文, MD5 直比失效, 补充 HSM 口径比对 // 新旧相同校验: 已迁移用户 password 列为 HSM 密文, MD5 直比不可靠, 以备用字段为准并兼容未迁移用户
boolean sameAsOld = newEncrypted.equals(user.getPassword()) boolean sameAsOld = newEncrypted.equals(user.getCryptoRemarkOne())
|| ICryptoSensitiveHsmService.verifyPassword("sys_user", bizId, newEncrypted); || newEncrypted.equals(user.getPassword());
if (sameAsOld) { if (sameAsOld) {
return R.fail("新密码不能与旧密码相同"); return R.fail("新密码不能与旧密码相同");
} }
int rows = DataPermissionHelper.ignore(() -> userService.resetUserPwd(user.getUserId(), newEncrypted)); int rows = DataPermissionHelper.ignore(() -> userService.resetUserPwd(user.getUserId(), newEncrypted));
if (rows > 0) { if (rows > 0) {
// 取消 HSM 双轨写入: password 列已写入新 MD5, 仅同步刷新备用字段 crypto_remark_one 保证口径一致
// (完整性摘要由每日 syncIntegrityHashModifiedUsers 定时任务按库中实际值兜底补算)
try { try {
// 修复: 双轨写入 password=HSM密文 + crypto_remark_one=新MD5原值 ICryptoSensitiveHsmService.saveUserPasswordRemark(user.getUserId(), newEncrypted);
// (原逻辑只覆写 password, crypto_remark_one 停留旧 MD5, 改密后双轨失真且降级校验拿不到最新值)
ICryptoSensitiveHsmService.saveUserPasswordDualTrack(user.getUserId(), newEncrypted);
} catch (Exception e) { } catch (Exception e) {
log.warn("HSM加密新密码失败, userId={}", user.getUserId(), e); log.warn("更新密码备用字段失败, userId={}", user.getUserId(), e);
}
try {
SysUserVo updatedUser = userService.selectUserById(user.getUserId());
ICryptoSensitiveHsmService.saveIntegrityHash("sys_user", bizId,
"password,phonenumber,certificateNumber",
updatedUser.getPassword() != null ? updatedUser.getPassword() : "",
updatedUser.getPhonenumber() != null ? updatedUser.getPhonenumber() : "",
updatedUser.getCertificateNumber() != null ? updatedUser.getCertificateNumber() : "");
} catch (Exception e) {
log.warn("HSM完整性摘要更新失败, userId={}", user.getUserId(), e);
} }
return R.ok(); return R.ok();
} }

20
zdxt-modules/zdxt-system/src/main/java/org/dromara/system/service/ICryptoSensitiveHsmService.java

@ -66,6 +66,17 @@ public interface ICryptoSensitiveHsmService {
*/ */
boolean verifyPasswordByRemark(Long userId, String md5Password); boolean verifyPasswordByRemark(Long userId, String md5Password);
/**
* 仅更新密码备用字段: sys_user.crypto_remark_one=MD5原值
* <p>不调用 HSM 密码服务平台, 供取消平台依赖的改密链路同步刷新备份字段,
* 保证 password 列与 crypto_remark_one 双轨口径一致</p>
*
* @param userId 用户ID
* @param md5Password MD5(loginName+password+salt) 原值
* @return 是否更新成功
*/
boolean saveUserPasswordRemark(Long userId, String md5Password);
/** /**
* 使用 srnwg.secretKey 对明文做 AES 加密(生成"原逻辑值"形态) * 使用 srnwg.secretKey 对明文做 AES 加密(生成"原逻辑值"形态)
* <p>供 zdxt-system 模块内无法引用 IdCardCryptoUtil 的场景(导入监听器等)使用</p> * <p>供 zdxt-system 模块内无法引用 IdCardCryptoUtil 的场景(导入监听器等)使用</p>
@ -75,6 +86,15 @@ public interface ICryptoSensitiveHsmService {
*/ */
String encryptWithSrnwgAes(String plainValue); String encryptWithSrnwgAes(String plainValue);
/**
* 使用 srnwg.secretKey 对密文做 AES 解密(与 encryptWithSrnwgAes 对称)
* <p>供 zdxt-system 模块内无法引用 IdCardCryptoUtil 的场景使用</p>
*
* @param cipherValue AES 密文(Base64); 纯数字视为明文原样返回
* @return 明文; 入参为空或解密失败返回 null
*/
String decryptWithSrnwgAes(String cipherValue);
/** /**
* 手机号归一化为确定性 AES 形态(与 crypto_remark_two 备份口径一致) * 手机号归一化为确定性 AES 形态(与 crypto_remark_two 备份口径一致)
* <p>纯数字明文 → AES 加密; 密文形态(AES Base64) → 原样保留</p> * <p>纯数字明文 → AES 加密; 密文形态(AES Base64) → 原样保留</p>

28
zdxt-modules/zdxt-system/src/main/java/org/dromara/system/service/impl/CryptoSensitiveHsmServiceImpl.java

@ -160,6 +160,17 @@ public class CryptoSensitiveHsmServiceImpl implements ICryptoSensitiveHsmService
return user != null && md5Password.equals(user.getCryptoRemarkOne()); return user != null && md5Password.equals(user.getCryptoRemarkOne());
} }
@Override
public boolean saveUserPasswordRemark(Long userId, String md5Password) {
if (userId == null || StrUtil.isBlank(md5Password)) {
return false;
}
LambdaUpdateWrapper<SysUser> uw = new LambdaUpdateWrapper<>();
uw.eq(SysUser::getUserId, userId)
.set(SysUser::getCryptoRemarkOne, md5Password);
return sysUserMapper.update(null, uw) > 0;
}
@Override @Override
public String encryptWithSrnwgAes(String plainValue) { public String encryptWithSrnwgAes(String plainValue) {
if (StrUtil.isBlank(plainValue)) { if (StrUtil.isBlank(plainValue)) {
@ -174,6 +185,23 @@ public class CryptoSensitiveHsmServiceImpl implements ICryptoSensitiveHsmService
} }
} }
@Override
public String decryptWithSrnwgAes(String cipherValue) {
if (StrUtil.isBlank(cipherValue)) {
return null;
}
// 纯数字视为明文原样返回(与 CommonUtil.decryptPhone 容错口径一致)
if (cipherValue.matches("^\\d+$")) {
return cipherValue;
}
try {
return SecureUtil.aes(SecureUtil.decode(secretKey)).decryptStr(cipherValue);
} catch (Exception e) {
log.error("srnwg AES 解密失败", e);
return null;
}
}
@Override @Override
public String normalizePhoneToAes(String phone) { public String normalizePhoneToAes(String phone) {
if (StrUtil.isBlank(phone)) { if (StrUtil.isBlank(phone)) {

Loading…
Cancel
Save