再次优化注册逻辑,修复登录bug

lzt
哆哆咯哆哆咯 3 months ago
parent 095ae0ef4c
commit 147865213c

@ -7,4 +7,5 @@ public class User {
private Integer id;
private String username;
private String password;
private String confirmPassword;
}

@ -0,0 +1,8 @@
package com.example.User.Exceptions;
//处理登陆失败异常
public class LoginFailedException extends Exception{
public LoginFailedException(String message){
super(message);
}
}

@ -27,6 +27,10 @@ public class UserService {
if (user.getPassword() == null || user.getPassword().trim().isEmpty()) {
throw new InvalidInputException("密码不能为空");
}
//比较两次输入的密码
if (!user.getPassword().equals(user.getConfirmPassword())) {
throw new InvalidInputException("两次输入的密码不一致");
}
//检测用户名是否存在
User existUser = userMapper.selectByUsername(user.getUsername());
@ -43,10 +47,10 @@ public class UserService {
}
//用户登录
public User login(String username, String password) throws Exception {
public User login(String username, String password) throws LoginFailedException {
User user = userMapper.selectByUsername(username);
if (user == null || !user.getPassword().equals(password)) {
throw new Exception("登陆失败,账号或密码错误");
if (user == null || !passwordEncoder.matches(password, user.getPassword())) {
throw new LoginFailedException("登陆失败,账号或密码错误");
}
return user;
}

Loading…
Cancel
Save