回滚备份

This commit is contained in:
Sparkfreeman 2024-07-04 00:13:38 +08:00
parent 1e202aa863
commit 2563cdbd51
3 changed files with 45 additions and 5 deletions

View File

@ -17,7 +17,7 @@ public class SecurityConfig {
http.csrf(AbstractHttpConfigurer::disable) http.csrf(AbstractHttpConfigurer::disable)
// Permit all requests to specific endpoints // Permit all requests to specific endpoints
.authorizeHttpRequests(authorize -> authorize .authorizeHttpRequests(authorize -> authorize
.requestMatchers("/users/register", "/users/login").permitAll() // Allow these endpoints without authentication .requestMatchers("/users/register", "/users/login","/meetings/getMeetingById","/meetings/updateMeeting","/meetings/deleteMeeting","/meetings/addMeeting","/meetings/listAll").permitAll() // Allow these endpoints without authentication
.anyRequest().authenticated() // All other endpoints require authentication .anyRequest().authenticated() // All other endpoints require authentication
) )
// Configure form login // Configure form login

View File

@ -4,10 +4,8 @@ import org.cmh.backend.authentication.model.User;
import org.cmh.backend.authentication.service.UserService; import org.cmh.backend.authentication.service.UserService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
@RequestMapping("/users") @RequestMapping("/users")
@ -15,9 +13,34 @@ public class UserController {
@Autowired @Autowired
private UserService userService; private UserService userService;
@Autowired
private PasswordEncoder passwordEncoder;
@GetMapping("/{username}") @GetMapping("/{username}")
public ResponseEntity<User> getUser(@PathVariable String username) { public ResponseEntity<User> getUser(@PathVariable String username) {
User user = userService.getUserByUsername(username); User user = userService.getUserByUsername(username);
return ResponseEntity.ok(user); return ResponseEntity.ok(user);
} }
@PostMapping("/register")
public ResponseEntity<String> registerUser(@RequestBody User user) {
// 检查用户名是否已经存在
if (userService.isUsernameTaken(user.getUsername())) {
return ResponseEntity.status(400).body("用户名已存在");
}
// 保存用户信息
userService.saveUser(user);
return ResponseEntity.ok("User registered successfully");
}
@PostMapping("/login")
public ResponseEntity<String> login(@RequestBody User user) {
// 处理登录逻辑
User existingUser = userService.getUserByUsername(user.getUsername());
if (existingUser != null && passwordEncoder.matches(user.getPassword(), existingUser.getPassword())) {
return ResponseEntity.ok("登录成功");
} else {
return ResponseEntity.status(401).body("用户名或密码错误");
}
}
} }

View File

@ -3,6 +3,7 @@ package org.cmh.backend.authentication.service;
import org.cmh.backend.authentication.model.User; import org.cmh.backend.authentication.model.User;
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.password.PasswordEncoder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
@ -10,7 +11,23 @@ public class UserService {
@Autowired @Autowired
private UserRepository userRepository; private UserRepository userRepository;
@Autowired
private PasswordEncoder passwordEncoder;
// 获取用户信息通过用户名
public User getUserByUsername(String username) { public User getUserByUsername(String username) {
return userRepository.findByUsername(username); return userRepository.findByUsername(username);
} }
// 保存用户信息
public void saveUser(User user) {
// 对密码进行加密
user.setPassword(passwordEncoder.encode(user.getPassword()));
userRepository.save(user);
}
// 检查用户名是否已经存在
public boolean isUsernameTaken(String username) {
return userRepository.findByUsername(username) != null;
}
} }