diff --git a/src/main/java/org/cmh/backend/authentication/controller/AuthenticationController.java b/src/main/java/org/cmh/backend/authentication/controller/AuthenticationController.java index 35568cd..217a20e 100644 --- a/src/main/java/org/cmh/backend/authentication/controller/AuthenticationController.java +++ b/src/main/java/org/cmh/backend/authentication/controller/AuthenticationController.java @@ -27,12 +27,17 @@ class AuthenticationController { @PostMapping("/register") public ResponseEntity 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); } + } diff --git a/src/main/java/org/cmh/backend/authentication/service/UserService.java b/src/main/java/org/cmh/backend/authentication/service/UserService.java index 0562188..33d3d29 100644 --- a/src/main/java/org/cmh/backend/authentication/service/UserService.java +++ b/src/main/java/org/cmh/backend/authentication/service/UserService.java @@ -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(); } // 加密密码