Commit 5de7af9d authored by 李健华's avatar 李健华

修改校验密码都优先判断新加密方式

parent a5fbd6fc
...@@ -332,7 +332,8 @@ public class UserController implements IBaseController { ...@@ -332,7 +332,8 @@ public class UserController implements IBaseController {
verifyPhoneAndCode(phoneNo, verificationCode); verifyPhoneAndCode(phoneNo, verificationCode);
User user = userService.findByPhoneInDb(phoneNo); User user = userService.findByPhoneInDb(phoneNo);
if (user != null) { if (user != null) {
user.setPassword(PasswordUtil.MD5WithSalt(password)); // 用户注册使用新加密方式
// user.setPassword(PasswordUtil.MD5WithSalt(password));
user.setEncryptedPassword(BctyptPasswordUtil.BCryptWithSalt(password)); user.setEncryptedPassword(BctyptPasswordUtil.BCryptWithSalt(password));
userService.saveUser(user); userService.saveUser(user);
log.info("用户注册失败,该手机号已经被注册:register -> registerFrom:{}, phoneNo:{}", registerFrom, phoneNo); log.info("用户注册失败,该手机号已经被注册:register -> registerFrom:{}, phoneNo:{}", registerFrom, phoneNo);
...@@ -454,9 +455,17 @@ public class UserController implements IBaseController { ...@@ -454,9 +455,17 @@ public class UserController implements IBaseController {
if (StringUtils.isBlank(user.getPassword()) ^ StringUtils.isBlank(password)) { if (StringUtils.isBlank(user.getPassword()) ^ StringUtils.isBlank(password)) {
return JsonResult.buildErrorStateResult("修改密码失败", null); return JsonResult.buildErrorStateResult("修改密码失败", null);
} }
if (StringUtils.isNotBlank(user.getPassword()) && !PasswordUtil.validatePassword(password, user.getPassword())) { // 优先校验新的密码
return JsonResult.buildErrorStateResult("修改密码失败", null); if (StringUtils.isNotBlank(user.getEncryptedPassword()) ) {
if (!BctyptPasswordUtil.BCryptCheckPw(password, user.getEncryptedPassword())) {
return JsonResult.buildErrorStateResult("修改密码失败", null);
}
} else {
if (StringUtils.isNotBlank(user.getPassword()) && !PasswordUtil.validatePassword(password, user.getPassword())) {
return JsonResult.buildErrorStateResult("修改密码失败", null);
}
} }
if (!userService.resetPassword(phoneNo, passwordNew)) { if (!userService.resetPassword(phoneNo, passwordNew)) {
return JsonResult.buildErrorStateResult("修改密码失败", null); return JsonResult.buildErrorStateResult("修改密码失败", null);
} }
...@@ -740,21 +749,25 @@ public class UserController implements IBaseController { ...@@ -740,21 +749,25 @@ public class UserController implements IBaseController {
lockIpv4Service.countErrorByPhoneNo(phoneNo); lockIpv4Service.countErrorByPhoneNo(phoneNo);
return null; return null;
} }
System.out.println(user.getPassword());
if (!BctyptPasswordUtil.BCryptCheckPw(pass, user.getEncryptedPassword())) { // 优先校验新密码加密方式 如果有并且密码校验不通过
// 向该ipv4添加错误计数器 if (StringUtils.isNotBlank(user.getEncryptedPassword())) {
lockIpv4Service.countErrorByIpv4(clientIp); if (!BctyptPasswordUtil.BCryptCheckPw(pass, user.getEncryptedPassword())) {
// 向该phoneNo添加错误计数器 // 向该ipv4添加错误计数器
lockIpv4Service.countErrorByPhoneNo(phoneNo); lockIpv4Service.countErrorByIpv4(clientIp);
return null; // 向该phoneNo添加错误计数器
} lockIpv4Service.countErrorByPhoneNo(phoneNo);
//验证密码 return null;
if (!PasswordUtil.validatePassword(pass, user.getPassword())) { }
// 向该ipv4添加错误计数器 } else {
lockIpv4Service.countErrorByIpv4(clientIp); //验证密码
// 向该phoneNo添加错误计数器 if (!PasswordUtil.validatePassword(pass, user.getPassword())) {
lockIpv4Service.countErrorByPhoneNo(phoneNo); // 向该ipv4添加错误计数器
return null; lockIpv4Service.countErrorByIpv4(clientIp);
// 向该phoneNo添加错误计数器
lockIpv4Service.countErrorByPhoneNo(phoneNo);
return null;
}
} }
// 向该ipv4添加成功计数器 // 向该ipv4添加成功计数器
......
...@@ -2,6 +2,7 @@ package cn.quantgroup.xyqb.controller.middleoffice.common; ...@@ -2,6 +2,7 @@ package cn.quantgroup.xyqb.controller.middleoffice.common;
import cn.quantgroup.xyqb.entity.User; import cn.quantgroup.xyqb.entity.User;
import cn.quantgroup.xyqb.exception.DataException; import cn.quantgroup.xyqb.exception.DataException;
import cn.quantgroup.xyqb.util.BctyptPasswordUtil;
import cn.quantgroup.xyqb.util.PasswordUtil; import cn.quantgroup.xyqb.util.PasswordUtil;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -19,6 +20,15 @@ public class PwdVerifyStrategy implements IVerifyStrategy { ...@@ -19,6 +20,15 @@ public class PwdVerifyStrategy implements IVerifyStrategy {
@Override @Override
public void verify(User user, String verify) { public void verify(User user, String verify) {
// 如果新加密的密码不为空校验新密码
String encryptedPassword = user.getEncryptedPassword();
if (!"".equals(encryptedPassword)) {
if (Objects.equals(encryptedPassword, BctyptPasswordUtil.BCryptWithSalt(verify))) {
return;
}
throw new DataException("用户名或密码错误");
}
// 否则校验旧的密码
String password = user.getPassword(); String password = user.getPassword();
if (Objects.equals(password, PasswordUtil.MD5WithSalt(verify))) { if (Objects.equals(password, PasswordUtil.MD5WithSalt(verify))) {
return; return;
......
...@@ -159,7 +159,8 @@ public class UserRegisterServiceImpl implements IUserRegisterService { ...@@ -159,7 +159,8 @@ public class UserRegisterServiceImpl implements IUserRegisterService {
password = PasswordUtil.generateRandomPwd(Constants.RANDOM_PWD_LEN); password = PasswordUtil.generateRandomPwd(Constants.RANDOM_PWD_LEN);
} }
if (StringUtils.isNotBlank(password)) { if (StringUtils.isNotBlank(password)) {
user.setPassword(PasswordUtil.MD5WithSalt(password)); // user.setPassword(PasswordUtil.MD5WithSalt(password));
// 新建用户使用新加密方式
user.setEncryptedPassword(BctyptPasswordUtil.BCryptWithSalt(password)); user.setEncryptedPassword(BctyptPasswordUtil.BCryptWithSalt(password));
} }
user = userService.saveUser(user); user = userService.saveUser(user);
......
...@@ -228,11 +228,12 @@ public class UserServiceImpl implements IUserService, IBaseController { ...@@ -228,11 +228,12 @@ public class UserServiceImpl implements IUserService, IBaseController {
if (user == null) { if (user == null) {
throw new RuntimeException("用户[" + phoneNo + "]不存在"); throw new RuntimeException("用户[" + phoneNo + "]不存在");
} }
user.setPassword(PasswordUtil.MD5WithSalt(password)); //修改密码使用新加密方式
// user.setPassword(PasswordUtil.MD5WithSalt(password));
user.setEncryptedPassword(BctyptPasswordUtil.BCryptWithSalt(password)); user.setEncryptedPassword(BctyptPasswordUtil.BCryptWithSalt(password));
user = userRepository.save(user); user = userRepository.save(user);
stringRedisTemplate.expire("usercache:xyqbuser" + phoneNo, 1L, TimeUnit.MILLISECONDS); stringRedisTemplate.expire("usercache:xyqbuser" + phoneNo, 1L, TimeUnit.MILLISECONDS);
return PasswordUtil.validatePassword(password, user.getPassword()); return BctyptPasswordUtil.BCryptCheckPw(password, user.getEncryptedPassword());
} }
@Override @Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment