forked from RyanGoodwill/backend
注册功能完成,Insomnia测试通过
This commit is contained in:
parent
94474d6c42
commit
feec889732
@ -1,13 +1,54 @@
|
|||||||
package org.cmh.backend.authentication.controller;
|
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.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
class AuthenticationController {
|
class AuthenticationController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
@GetMapping("/hello")
|
@GetMapping("/hello")
|
||||||
public String hello(){
|
public String hello() {
|
||||||
return "Hello SpringBoot!";
|
return "Hello SpringBoot!";
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
@PostMapping("/register")
|
||||||
|
public ResponseEntity<Map<String, Object>> register(@RequestBody RegisterRequest request) {
|
||||||
|
Map<String, Object> 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@ -3,14 +3,70 @@ package org.cmh.backend.authentication.service;
|
|||||||
import org.cmh.backend.authentication.model.UserHS;
|
import org.cmh.backend.authentication.model.UserHS;
|
||||||
import org.cmh.backend.authentication.repository.UserRepository;
|
import org.cmh.backend.authentication.repository.UserRepository;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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 org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class UserService {
|
public class UserService {
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserRepository userRepository;
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
private PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
||||||
|
|
||||||
public UserHS getUserByUsername(String username) {
|
public UserHS getUserByUsername(String username) {
|
||||||
return userRepository.findByUsername(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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user