优化失败message展示逻辑

This commit is contained in:
高子兴 2024-07-01 17:27:44 +08:00
parent 80ae2fb8d3
commit 4eb09c3bca
2 changed files with 12 additions and 7 deletions

View File

@ -27,12 +27,17 @@ class AuthenticationController {
@PostMapping("/register")
public ResponseEntity<RegisterResponse> register(@RequestBody RegisterRequest request) {
boolean isRegistered = userService.registerUser(request.getUsername(), request.getPassword(), request.getContactInfo());
if (isRegistered) {
return new ResponseEntity<>(new RegisterResponse("Registration successful!"), HttpStatus.OK);
} else {
return new ResponseEntity<>(new RegisterResponse("Registration failed! User already exist"), HttpStatus.BAD_REQUEST);
try {
boolean isRegistered = userService.registerUser(request.getUsername(), request.getPassword(), request.getContactInfo());
if (isRegistered) {
return new ResponseEntity<>(new RegisterResponse("注册成功"), HttpStatus.OK);
} else {
return new ResponseEntity<>(new RegisterResponse("注册失败:用户已存在"), HttpStatus.BAD_REQUEST);
}
} catch (Exception e) {
return new ResponseEntity<>(new RegisterResponse("注册失败:输入格式有误"), HttpStatus.BAD_REQUEST);
}
}

View File

@ -15,7 +15,7 @@ public class UserService {
@Autowired
private UserRepository userRepository;
private PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
private final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
public UserHS getUserByUsername(String username) {
return userRepository.findByUsername(username);
@ -29,7 +29,7 @@ public class UserService {
// 验证输入格式
if (!isValidUsername(username) || !isValidPassword(password) || !isValidContactInfo(contactInfo)) {
return false; // 输入格式不正确
throw new IllegalArgumentException();
}
// 加密密码