Compare commits
1 Commits
main
...
personal/l
| Author | SHA1 | Date | |
|---|---|---|---|
| 7a6d2b5c4b |
6
pom.xml
@ -128,6 +128,12 @@
|
||||
<artifactId>spring-boot-testcontainers</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter-test</artifactId>
|
||||
|
||||
@ -1,109 +0,0 @@
|
||||
package org.cmh.backend.Authentication.controller;
|
||||
|
||||
|
||||
import org.cmh.backend.Utils.JwtUtil;
|
||||
import org.cmh.backend.Utils.JwtVerify;
|
||||
import org.cmh.backend.Authentication.dto.*;
|
||||
import org.cmh.backend.Authentication.model.UserHS;
|
||||
import org.cmh.backend.Authentication.service.UserService;
|
||||
import org.cmh.backend.Authentication.service.VerificationCodeService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
class AuthenticationController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private VerificationCodeService verificationCodeService;
|
||||
|
||||
@GetMapping("/hello")
|
||||
public String hello() {
|
||||
return "Hello SpringBoot!";
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/register")
|
||||
public ResponseEntity<RegisterResponse> register(@RequestBody RegisterRequest request) {
|
||||
try {
|
||||
boolean isRegistered = userService.registerUser(request);
|
||||
if (isRegistered) {
|
||||
return new ResponseEntity<>(new RegisterResponse("注册成功"), HttpStatus.OK);
|
||||
} else {
|
||||
return new ResponseEntity<>(new RegisterResponse("注册失败:用户已存在"), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
return new ResponseEntity<>(new RegisterResponse("注册失败:输入格式有误"), HttpStatus.BAD_REQUEST);
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>(new RegisterResponse("注册失败:服务器错误"), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
public ResponseEntity<LoginResponse> login(@RequestBody LoginRequest loginRequest) {
|
||||
boolean isValidUser = userService.loginUser(loginRequest.getUsername(), loginRequest.getPassword());
|
||||
|
||||
if (isValidUser) {
|
||||
return new ResponseEntity<>(new LoginResponse("登录成功", JwtUtil.generateToken(loginRequest.getUsername())), HttpStatus.OK);
|
||||
} else {
|
||||
return new ResponseEntity<>(new LoginResponse("用户名或密码错误", null), HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/userProfile")
|
||||
public ResponseEntity<UserProfileResponse> getUserProfile(@RequestParam String token) {
|
||||
if (JwtUtil.isTokenValid(token)) {
|
||||
UserHS user = userService.getUserByUsername(JwtUtil.extractUsername(token));
|
||||
if (user != null) {
|
||||
UserProfileResponse response = new UserProfileResponse(
|
||||
user.getUsername(),
|
||||
user.getNickname(),
|
||||
user.getGender(),
|
||||
user.getPhoneNumber(),
|
||||
user.getEmail(),
|
||||
user.getDepartment(),
|
||||
user.getRole(),
|
||||
user.getCreatedAt()
|
||||
);
|
||||
if (user.getSuperAdmin()) {
|
||||
response.setDepartment("超级管理员");
|
||||
response.setRole("超级管理员");
|
||||
}
|
||||
return new ResponseEntity<>(response, HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
@PostMapping("/manageUserProfile")
|
||||
@JwtVerify
|
||||
public ResponseEntity<Object> manageUserProfile(@RequestBody ManageUserProfileRequest userProfileRequest) {
|
||||
String username = JwtUtil.extractUsername(userProfileRequest.getToken());
|
||||
boolean succeeded = userService.updateUserProfile(username, userProfileRequest);
|
||||
if (succeeded) {
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
@PostMapping("/changePassword")
|
||||
@JwtVerify
|
||||
public ResponseEntity<Object> changePassword(@RequestBody ChangePasswordRequest changePasswordRequest) {
|
||||
if (userService.changePassword(JwtUtil.extractUsername(changePasswordRequest.getToken()), changePasswordRequest)) {
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
@GetMapping("/getVerificationCode")
|
||||
public ResponseEntity<VerificationCodeResponse> getVerificationCode() {
|
||||
return new ResponseEntity<>(verificationCodeService.provideVerificationCode(), HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
package org.cmh.backend.Authentication.dto;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.cmh.backend.Utils.JwtRequest;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class ChangePasswordRequest extends JwtRequest {
|
||||
private String currentPassword;
|
||||
private String newPassword;
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
package org.cmh.backend.Authentication.dto;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class LoginRequest {
|
||||
private String username;
|
||||
private String password;
|
||||
private String verificationCode;
|
||||
}
|
||||
@ -1,13 +0,0 @@
|
||||
package org.cmh.backend.Authentication.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
public class LoginResponse {
|
||||
private String message;
|
||||
private String token;
|
||||
}
|
||||
@ -1,16 +0,0 @@
|
||||
package org.cmh.backend.Authentication.dto;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.cmh.backend.Utils.JwtRequest;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class ManageUserProfileRequest extends JwtRequest {
|
||||
private String nickname;
|
||||
private String gender;
|
||||
private String phoneNumber;
|
||||
private String email;
|
||||
private String department;
|
||||
private String role;
|
||||
}
|
||||
@ -1,13 +0,0 @@
|
||||
package org.cmh.backend.Authentication.dto;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class RegisterRequest {
|
||||
private String username;
|
||||
private String password;
|
||||
private String phoneNumber;
|
||||
private String verificationCode;
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
package org.cmh.backend.Authentication.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
public class RegisterResponse {
|
||||
private String message;
|
||||
}
|
||||
@ -1,21 +0,0 @@
|
||||
package org.cmh.backend.Authentication.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
public class UserProfileResponse {
|
||||
private String username;
|
||||
private String nickname;
|
||||
private String gender;
|
||||
private String phoneNumber;
|
||||
private String email;
|
||||
private String department;
|
||||
private String role;
|
||||
private LocalDateTime createdAt;
|
||||
}
|
||||
@ -1,13 +0,0 @@
|
||||
package org.cmh.backend.Authentication.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
public class VerificationCodeResponse {
|
||||
private String code;
|
||||
private String path;
|
||||
}
|
||||
@ -1,29 +0,0 @@
|
||||
package org.cmh.backend.Authentication.model;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@Entity
|
||||
public class UserHS {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String username;
|
||||
private String password;
|
||||
private String nickname;
|
||||
private String gender;
|
||||
private String phoneNumber;
|
||||
private String email;
|
||||
private String department;
|
||||
private String role;
|
||||
private LocalDateTime createdAt;
|
||||
private Boolean superAdmin = false;
|
||||
}
|
||||
@ -1,8 +0,0 @@
|
||||
package org.cmh.backend.Authentication.repository;
|
||||
|
||||
import org.cmh.backend.Authentication.model.UserHS;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface UserRepository extends JpaRepository<UserHS, Long> {
|
||||
UserHS findByUsername(String username);
|
||||
}
|
||||
@ -1,117 +0,0 @@
|
||||
package org.cmh.backend.Authentication.service;
|
||||
|
||||
import org.cmh.backend.Authentication.dto.ChangePasswordRequest;
|
||||
import org.cmh.backend.Authentication.dto.ManageUserProfileRequest;
|
||||
import org.cmh.backend.Authentication.dto.RegisterRequest;
|
||||
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 final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
||||
|
||||
public UserHS getUserByUsername(String username) {
|
||||
return userRepository.findByUsername(username);
|
||||
}
|
||||
|
||||
public boolean registerUser(RegisterRequest request) {
|
||||
String username = request.getUsername();
|
||||
String password = request.getPassword();
|
||||
String phoneNumber = request.getPhoneNumber();
|
||||
// 验证用户名是否已存在
|
||||
if (userRepository.findByUsername(username) != null) {
|
||||
return false; // 用户已存在
|
||||
}
|
||||
|
||||
// 验证输入格式
|
||||
if (!isValidUsername(username) || !isValidPassword(password) || !isValidContactInfo(phoneNumber)) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
// 加密密码
|
||||
String encodedPassword = passwordEncoder.encode(password);
|
||||
|
||||
// 创建新用户
|
||||
UserHS newUser = new UserHS();
|
||||
newUser.setUsername(username);
|
||||
newUser.setPassword(encodedPassword);
|
||||
newUser.setPhoneNumber(phoneNumber);
|
||||
newUser.setCreatedAt(LocalDateTime.now());
|
||||
|
||||
userRepository.save(newUser);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean loginUser(String username, String password) {
|
||||
UserHS user = userRepository.findByUsername(username);
|
||||
return user != null && passwordEncoder.matches(password, user.getPassword());
|
||||
}
|
||||
|
||||
public boolean updateUserProfile(String username, ManageUserProfileRequest request) {
|
||||
UserHS user = userRepository.findByUsername(username);
|
||||
if (user != null) {
|
||||
user.setNickname(request.getNickname());
|
||||
user.setGender(request.getGender());
|
||||
user.setPhoneNumber(request.getPhoneNumber());
|
||||
user.setEmail(request.getEmail());
|
||||
user.setDepartment(request.getDepartment());
|
||||
user.setRole(request.getRole());
|
||||
try {
|
||||
userRepository.save(user);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean changePassword(String username, ChangePasswordRequest request) {
|
||||
UserHS user = userRepository.findByUsername(username);
|
||||
if (user != null) {
|
||||
if (passwordEncoder.matches(request.getCurrentPassword(), user.getPassword())) {
|
||||
if (isValidPassword(request.getNewPassword())) {
|
||||
String encodedPassword = passwordEncoder.encode(request.getNewPassword());
|
||||
user.setPassword(encodedPassword);
|
||||
try {
|
||||
userRepository.save(user);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// 验证用户名格式
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,51 +0,0 @@
|
||||
package org.cmh.backend.Authentication.service;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import lombok.Getter;
|
||||
import org.cmh.backend.Authentication.dto.VerificationCodeResponse;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Service
|
||||
public class VerificationCodeService {
|
||||
|
||||
@Value("${verification.code.images.path}")
|
||||
private String verificationCodeImagesPath;
|
||||
|
||||
@Getter
|
||||
private List<String> verificationCodeList = new ArrayList<>();
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
loadVerificationCodeImages();
|
||||
}
|
||||
|
||||
private void loadVerificationCodeImages() {
|
||||
try (Stream<Path> paths = Files.list(Paths.get(verificationCodeImagesPath))) {
|
||||
paths.filter(path -> path.toString().endsWith(".png"))
|
||||
.forEach(path -> verificationCodeList.add(path.getFileName().toString()));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public VerificationCodeResponse provideVerificationCode() {
|
||||
if (verificationCodeList.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
Random random = new Random();
|
||||
int code = random.nextInt(random.nextInt(verificationCodeList.size()));
|
||||
String selectedCode = verificationCodeList.get(code);
|
||||
return new VerificationCodeResponse(selectedCode.split("\\.")[0], "/verificationCodeImages/" + selectedCode);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,47 @@
|
||||
package org.cmh.backend.UserManagement.controller;
|
||||
|
||||
import jakarta.transaction.Transactional;
|
||||
import org.cmh.backend.UserManagement.model.Tenant;
|
||||
import org.cmh.backend.UserManagement.service.TenantManagementService;
|
||||
import org.cmh.backend.authentication.model.User;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
public class TenantManagementController {
|
||||
@Autowired
|
||||
private TenantManagementService tenantManagementService;
|
||||
|
||||
@CrossOrigin(origins = "http://localhost:5173")
|
||||
@PostMapping("/addTenant")
|
||||
public Tenant addTenant(@RequestBody Tenant tenant) {
|
||||
Tenant tenant1=tenantManagementService.registerTenant(tenant);
|
||||
System.out.println(tenant.toString());
|
||||
return tenant1;
|
||||
}
|
||||
|
||||
@CrossOrigin(origins = "http://localhost:5173")
|
||||
@GetMapping("/getAllTenant")
|
||||
public List<Tenant> getAll() {
|
||||
List<Tenant> tenantList=tenantManagementService.findAll();
|
||||
return tenantList;
|
||||
}
|
||||
|
||||
@CrossOrigin(origins = "http://localhost:5173")
|
||||
@PostMapping("/updateTenant")
|
||||
public Tenant update(@RequestBody Tenant tenant) {
|
||||
System.out.println(tenant);
|
||||
tenantManagementService.update(tenant);
|
||||
return tenant;
|
||||
}
|
||||
|
||||
@CrossOrigin(origins = "http://localhost:5173")
|
||||
@PostMapping("/deleteTenant")
|
||||
@Transactional
|
||||
public void delete(@RequestBody User user) {
|
||||
tenantManagementService.delete(user.getId());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,51 @@
|
||||
package org.cmh.backend.UserManagement.controller;
|
||||
|
||||
import jakarta.transaction.Transactional;
|
||||
import org.cmh.backend.UserManagement.service.UserManagementService;
|
||||
import org.cmh.backend.UserManagement.model.User;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
public class UserManagementController {
|
||||
@Autowired
|
||||
private UserManagementService userManagementService;
|
||||
|
||||
@CrossOrigin(origins = "http://localhost:5173")
|
||||
@PostMapping("/addUser")
|
||||
public User addUser(@RequestBody User user) {
|
||||
User user1=userManagementService.registerUser(user);
|
||||
if(user1 != null){
|
||||
System.out.println(user1);
|
||||
return user1;
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@CrossOrigin(origins = "http://localhost:5173")
|
||||
@GetMapping("/getAll")
|
||||
public List<User> getAll() {
|
||||
List<User> userList=userManagementService.findAll();
|
||||
return userList;
|
||||
}
|
||||
|
||||
@CrossOrigin(origins = "http://localhost:5173")
|
||||
@PostMapping("/update")
|
||||
public User update(@RequestBody User user) {
|
||||
System.out.println(user);
|
||||
userManagementService.update(user);
|
||||
return user;
|
||||
}
|
||||
|
||||
@CrossOrigin(origins = "http://localhost:5173")
|
||||
@PostMapping("/delete")
|
||||
@Transactional
|
||||
public void delete(@RequestBody User user) {
|
||||
System.out.println(user);
|
||||
userManagementService.delete(user.getId());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,33 @@
|
||||
package org.cmh.backend.UserManagement.model;
|
||||
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Table(name="tenant")
|
||||
@Entity
|
||||
public class Tenant {
|
||||
@Id
|
||||
@Getter
|
||||
@Setter
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private String contact;
|
||||
@Getter
|
||||
@Setter
|
||||
private String phone;
|
||||
@Getter
|
||||
@Setter
|
||||
private String name;
|
||||
@Getter
|
||||
@Setter
|
||||
private String manager;
|
||||
@Getter
|
||||
@Setter
|
||||
private String symbol;
|
||||
|
||||
}
|
||||
68
src/main/java/org/cmh/backend/UserManagement/model/User.java
Normal file
@ -0,0 +1,68 @@
|
||||
package org.cmh.backend.UserManagement.model;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Entity
|
||||
public class User {
|
||||
@Setter
|
||||
@Getter
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
@Setter
|
||||
@Getter
|
||||
private String account;
|
||||
@Setter
|
||||
@Getter
|
||||
private String password;
|
||||
@Setter
|
||||
@Getter
|
||||
private String name;
|
||||
@Getter
|
||||
@Setter
|
||||
private String organization;
|
||||
@Setter
|
||||
@Getter
|
||||
private String gender;
|
||||
@Setter
|
||||
@Getter
|
||||
private String email;
|
||||
@Getter
|
||||
@Setter
|
||||
private String phone;
|
||||
|
||||
public User() {
|
||||
|
||||
}
|
||||
|
||||
public User(String account, String password, String name, String organization, String gender, String email, String phone) {
|
||||
this.account = account;
|
||||
this.password = password;
|
||||
this.name = name;
|
||||
this.organization = organization;
|
||||
this.gender = gender;
|
||||
this.email = email;
|
||||
this.phone = phone;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" +
|
||||
"id=" + id +
|
||||
", account='" + account + '\'' +
|
||||
", password='" + password + '\'' +
|
||||
", name='" + name + '\'' +
|
||||
", organization='" + organization + '\'' +
|
||||
", gender='" + gender + '\'' +
|
||||
", email='" + email + '\'' +
|
||||
", phone='" + phone + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
package org.cmh.backend.UserManagement.repository;
|
||||
import org.cmh.backend.UserManagement.model.Tenant;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.List;
|
||||
public interface TenantManagementRepository extends JpaRepository<Tenant, Long>{
|
||||
Tenant findByName(String name);
|
||||
Tenant save(Tenant tenant);
|
||||
|
||||
List<Tenant> findAll();
|
||||
|
||||
Tenant findTenantById(Long id);
|
||||
|
||||
Tenant deleteByName(String Name);
|
||||
|
||||
void deleteById(Long id);
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
package org.cmh.backend.UserManagement.repository;
|
||||
|
||||
|
||||
import org.cmh.backend.UserManagement.model.User;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface UserManagementRepository extends JpaRepository<User, Long> {
|
||||
User findByName(String name);
|
||||
User findByAccountAndPassword(String name,String password);
|
||||
User save(User user);
|
||||
|
||||
List<User> findAll();
|
||||
|
||||
User findUserById(Long id);
|
||||
|
||||
User deleteByAccount(String account);
|
||||
|
||||
void deleteById(Long id);
|
||||
|
||||
}
|
||||
@ -0,0 +1,29 @@
|
||||
package org.cmh.backend.UserManagement.service;
|
||||
|
||||
import org.cmh.backend.UserManagement.model.Tenant;
|
||||
import org.cmh.backend.UserManagement.repository.TenantManagementRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class TenantManagementService {
|
||||
|
||||
@Autowired
|
||||
private TenantManagementRepository tenantManagementRepository;
|
||||
|
||||
public Tenant registerTenant(Tenant tenant) {
|
||||
Tenant savedTenant = tenantManagementRepository.save(tenant);
|
||||
return savedTenant;
|
||||
}
|
||||
|
||||
public List<Tenant> findAll(){
|
||||
return tenantManagementRepository.findAll();
|
||||
}
|
||||
|
||||
public Tenant update(Tenant tenant){return tenantManagementRepository.save(tenant);}
|
||||
|
||||
public Tenant delete(String name){return tenantManagementRepository.deleteByName(name);}
|
||||
|
||||
public void delete(Long id){tenantManagementRepository.deleteById(id);}
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
package org.cmh.backend.UserManagement.service;
|
||||
|
||||
import org.cmh.backend.UserManagement.repository.UserManagementRepository;
|
||||
import org.cmh.backend.UserManagement.model.User;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class UserManagementService {
|
||||
|
||||
@Autowired
|
||||
private UserManagementRepository userManagementRepository;
|
||||
|
||||
|
||||
public User registerUser(User user) {
|
||||
User savedUser = userManagementRepository.save(user);
|
||||
return savedUser;
|
||||
}
|
||||
|
||||
public List<User> findAll(){
|
||||
return userManagementRepository.findAll();
|
||||
}
|
||||
|
||||
public User findUserById(Long id){return userManagementRepository.findUserById(id);}
|
||||
|
||||
public User update(User user){return userManagementRepository.save(user);}
|
||||
|
||||
public void delete(Long id){userManagementRepository.deleteById(id);}
|
||||
|
||||
}
|
||||
@ -0,0 +1,39 @@
|
||||
package org.cmh.backend.authentication.controller;
|
||||
|
||||
import org.cmh.backend.UserManagement.model.User;
|
||||
import org.cmh.backend.authentication.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.springframework.http.ResponseEntity.ok;
|
||||
|
||||
@RestController
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
@CrossOrigin(origins = "http://localhost:5173")
|
||||
@PostMapping("/checkLogin")
|
||||
public ResponseEntity<User> login(@RequestBody Map<String,String> credentials) {
|
||||
String account = credentials.get("account");
|
||||
String password = credentials.get("password");
|
||||
|
||||
User user = userService.getUserByAccountAndPassword(account, password);
|
||||
return ok(user);
|
||||
|
||||
}
|
||||
@CrossOrigin(origins = "http://localhost:5173")
|
||||
@PostMapping("/checkRegister")
|
||||
public String register(@RequestBody User user) {
|
||||
|
||||
if(userService.registerUser(user) != null){
|
||||
return "注册成功";
|
||||
}else
|
||||
return "注册错误";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
package org.cmh.backend.authentication.repository;
|
||||
|
||||
import org.cmh.backend.UserManagement.model.User;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface UserRepository extends JpaRepository<User, Long> {
|
||||
User findByName(String name);
|
||||
User findByAccountAndPassword(String name,String password);
|
||||
User save(User user);
|
||||
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
package org.cmh.backend.authentication.service;
|
||||
|
||||
import org.cmh.backend.UserManagement.model.User;
|
||||
import org.cmh.backend.authentication.repository.UserRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
public User getUserByUsername(String username) {
|
||||
return userRepository.findByName(username);
|
||||
}
|
||||
public User getUserByAccountAndPassword(String name,String password){return userRepository.findByAccountAndPassword(name,password);}
|
||||
|
||||
public User registerUser(User user) {
|
||||
User savedUser = userRepository.save(user);
|
||||
return savedUser;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 3.1 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 3.0 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 3.3 KiB |
|
Before Width: | Height: | Size: 3.1 KiB |
|
Before Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 2.3 KiB |
|
Before Width: | Height: | Size: 2.9 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.2 KiB |
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.2 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 3.0 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 3.2 KiB |
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 3.0 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 3.0 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 3.0 KiB |
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.0 KiB |
|
Before Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 3.0 KiB |