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); return "修改密码成功"; } else { throw new RuntimeException("密码格式不正确"); } }这个修改用户密码的方法还有什么不足的地方吗?
时间: 2024-04-03 21:34:55 浏览: 20
在这个修改用户密码的方法中,还有以下不足之处:
1. 该方法中抛出的异常都是 `RuntimeException`,这会导致调用该方法的上层代码难以处理异常,因为无法预知该方法会抛出哪些异常,应该根据具体情况抛出合适的受检查异常,并在方法声明中使用 `throws` 关键字声明抛出的异常。
2. `selectUserByPhone()` 方法中传入的参数是一个 `User` 对象,其中只设置了手机号码,但其他信息可能也会对查询结果产生影响,建议将查询条件改为一个包含所有查询条件的对象。
3. 在修改密码成功时,应该返回一个成功标识,而不是返回一个字符串。
4. 对于密码格式的校验,应该根据具体需求进行设置,例如密码长度、是否包含特殊字符等,而不是简单地调用 `validatePassword()` 方法进行校验。
5. 在修改密码时,应该使用加密算法对密码进行加密存储,而不是直接存储明文密码。
相关问题
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 = ""; 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; }简化代码
这段代码实现了用户修改密码的功能,下面是简化后的代码:
```
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 "修改密码成功";
}
```
简化后的代码去掉了一些无用的变量和重复的判断语句,让代码更加清晰易懂。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)