public String ChangePassword(UserInfo userInfo){ String msg = ""; if(userInfo.getPassword().equals(userInfo.getNewPassword())){ msg = "新密码不能与旧密码相同"; throw new RuntimeException("新密码不能与旧密码相同"); } User user = selectUserByPhone(User.builder().phoneNumber(userInfo.getUsername()).build()); if (user == null) { msg = "用户不存在"; throw new RuntimeException("用户不存在"); } if (!user.getPassword().equals(userInfo.getPassword())) { msg = "旧密码不正确"; throw new RuntimeException("旧密码不正确"); } if(validatePassword(userInfo.getPassword())){ user.setPassword(userInfo.getNewPassword()); userInfoMapper.updateByPrimaryKeySelective(user); msg = "修改密码成功"; } else { msg = "旧密码不正确"; throw new RuntimeException("旧密码不正确"); } return msg; }简化代码
时间: 2024-03-21 17:41:18 浏览: 57
这段代码实现了用户修改密码的功能,下面是简化后的代码:
```
public String changePassword(UserInfo userInfo) {
if (userInfo.getPassword().equals(userInfo.getNewPassword())) {
throw new RuntimeException("新密码不能与旧密码相同");
}
User user = selectUserByPhone(User.builder().phoneNumber(userInfo.getUsername()).build());
if (user == null || !user.getPassword().equals(userInfo.getPassword())) {
throw new RuntimeException("旧密码不正确");
}
user.setPassword(userInfo.getNewPassword());
userInfoMapper.updateByPrimaryKeySelective(user);
return "修改密码成功";
}
```
简化后的代码去掉了一些无用的变量和重复的判断语句,让代码更加清晰易懂。
相关问题
public String ChangePassword(UserInfo userInfo){ if(userInfo.getPassword().equals(userInfo.getNewPassword())){ throw new RuntimeException("新密码不能与旧密码相同"); } User user = selectUserByPhone(User.builder().phoneNumber(userInfo.getUsername()).build()); if (user == null || !user.getPassword().equals(userInfo.getPassword())) { throw new RuntimeException("账号或密码不正确"); } if(validatePassword(userInfo.getPassword())){ user.setPassword(userInfo.getNewPassword()); userInfoMapper.updateByPrimaryKeySelective(user); } else { throw new RuntimeException("密码格式不正确"); } return "修改密码成功"; }简化代码
以下是简化后的代码:
```
public String ChangePassword(UserInfo userInfo) {
User user = selectUserByPhone(User.builder().phoneNumber(userInfo.getUsername()).build());
if (user == null || !user.getPassword().equals(userInfo.getPassword())) {
throw new RuntimeException("账号或密码不正确");
}
if (userInfo.getPassword().equals(userInfo.getNewPassword())) {
throw new RuntimeException("新密码不能与旧密码相同");
}
if (validatePassword(userInfo.getPassword())) {
user.setPassword(userInfo.getNewPassword());
userInfoMapper.updateByPrimaryKeySelective(user);
return "修改密码成功";
} else {
throw new RuntimeException("密码格式不正确");
}
}
```
public String ChangePassword(UserInfo userInfo){ String msg = ""; User user = selectUserByPhone(userInfo.getUsername() ); if (user == null) { msg = "用户不存在"; throw new RuntimeException("用户不存在"); } if (!user.getPassword().equals(userInfo.getPassword())) { msg = "旧密码不正确"; throw new RuntimeException("旧密码不正确"); } else { user.setPassword(userInfo.getNewPassword()); userInfoMapper.updateByPrimaryKeySelective(user); msg = "修改密码成功"; } return msg; }这段代码是否存在问题?
这段代码存在一个潜在的安全问题,因为在比对旧密码是否正确时,使用了字符串比较的方式,而不是使用加密后的密码进行比对。这意味着,如果有人能够获取到数据库中的用户信息,就可以直接获取到明文密码,从而轻易地登录该用户的账户。
为了解决这个问题,建议在存储用户密码时,使用加密算法(如SHA-256)对密码进行加密,并将加密后的密码存储在数据库中。在比对密码时,也需要对用户输入的密码进行加密后再进行比对。这样即使数据库信息被泄露,攻击者也无法直接获取到用户密码,提高了系统的安全性。
另外,代码中的异常处理也存在一定问题。在抛出RuntimeException时,没有对异常进行具体的描述,可能导致开发人员在排查问题时难以定位具体的异常原因。建议在抛出RuntimeException时,增加异常描述,方便开发人员进行问题排查。
阅读全文
相关推荐














