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 8ec56df..1f2c243 100644 --- a/src/main/java/org/cmh/backend/authentication/controller/AuthenticationController.java +++ b/src/main/java/org/cmh/backend/authentication/controller/AuthenticationController.java @@ -1,13 +1,54 @@ package org.cmh.backend.authentication.controller; +import lombok.Getter; +import lombok.Setter; +import org.cmh.backend.authentication.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; +import java.util.HashMap; +import java.util.Map; + @RestController class AuthenticationController { + + @Autowired + private UserService userService; + @GetMapping("/hello") - public String hello(){ + public String hello() { return "Hello SpringBoot!"; } -} \ No newline at end of file + + @PostMapping("/register") + public ResponseEntity> register(@RequestBody RegisterRequest request) { + Map response = new HashMap<>(); + + boolean isRegistered = userService.registerUser(request.getUsername(), request.getPassword(), request.getContactInfo()); + if (isRegistered) { + response.put("message", "Registration successful!"); + return new ResponseEntity<>(response, HttpStatus.OK); + } else { + response.put("message", "Registration failed! User already exist"); + return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); + } + } + + +} + +// DTO classes +@Getter +@Setter +class RegisterRequest { + private String username; + private String password; + private String contactInfo; +} + 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 1351b1f..0562188 100644 --- a/src/main/java/org/cmh/backend/authentication/service/UserService.java +++ b/src/main/java/org/cmh/backend/authentication/service/UserService.java @@ -3,14 +3,70 @@ package org.cmh.backend.authentication.service; import org.cmh.backend.authentication.model.UserHS; import org.cmh.backend.authentication.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; +import java.time.LocalDateTime; +import java.util.regex.Pattern; + @Service public class UserService { @Autowired private UserRepository userRepository; + private PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); + public UserHS getUserByUsername(String username) { return userRepository.findByUsername(username); } -} + + public boolean registerUser(String username, String password, String contactInfo) { + // 验证用户名是否已存在 + if (userRepository.findByUsername(username) != null) { + return false; // 用户已存在 + } + + // 验证输入格式 + if (!isValidUsername(username) || !isValidPassword(password) || !isValidContactInfo(contactInfo)) { + return false; // 输入格式不正确 + } + + // 加密密码 + String encodedPassword = passwordEncoder.encode(password); + + // 创建新用户 + UserHS newUser = new UserHS(); + newUser.setUsername(username); + newUser.setPassword(encodedPassword); + newUser.setPhoneNumber(contactInfo); // 假设contactInfo是电话号码 + newUser.setCreatedAt(LocalDateTime.now()); + + try { + userRepository.save(newUser); + } catch (Exception e) { + // 处理保存用户时可能出现的异常 + return false; + } + + return true; + } + + + // 验证用户名格式 + private boolean isValidUsername(String username) { + return username != null && username.length() >= 3 && username.length() <= 20; + } + + // 验证密码格式 + private boolean isValidPassword(String password) { + return password != null && password.length() >= 4; + } + + // 验证联系方式格式(假设为电话号码) + private boolean isValidContactInfo(String contactInfo) { + String regex = "^\\+?[0-9. ()-]{7,25}$"; // 电话号码验证 + return contactInfo != null && Pattern.matches(regex, contactInfo); + } + +} \ No newline at end of file