forked from RyanGoodwill/backend
回滚备份
This commit is contained in:
parent
1e202aa863
commit
2563cdbd51
@ -17,7 +17,7 @@ public class SecurityConfig {
|
||||
http.csrf(AbstractHttpConfigurer::disable)
|
||||
// Permit all requests to specific endpoints
|
||||
.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
|
||||
)
|
||||
// Configure form login
|
||||
|
||||
@ -4,10 +4,8 @@ import org.cmh.backend.authentication.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.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/users")
|
||||
@ -15,9 +13,34 @@ public class UserController {
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private PasswordEncoder passwordEncoder;
|
||||
|
||||
@GetMapping("/{username}")
|
||||
public ResponseEntity<User> getUser(@PathVariable String username) {
|
||||
User user = userService.getUserByUsername(username);
|
||||
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("用户名或密码错误");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -3,6 +3,7 @@ package org.cmh.backend.authentication.service;
|
||||
import org.cmh.backend.authentication.model.User;
|
||||
import org.cmh.backend.authentication.repository.UserRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@ -10,7 +11,23 @@ public class UserService {
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
private PasswordEncoder passwordEncoder;
|
||||
|
||||
// 获取用户信息通过用户名
|
||||
public User getUserByUsername(String 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;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user