2
This commit is contained in:
parent
09ebd82dac
commit
ef10067345
5
pom.xml
5
pom.xml
@ -93,11 +93,6 @@
|
||||
<scope>runtime</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.h2database</groupId>-->
|
||||
<!-- <artifactId>h2</artifactId>-->
|
||||
<!-- <scope>runtime</scope>-->
|
||||
<!-- </dependency>-->
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
|
||||
@ -0,0 +1,67 @@
|
||||
package org.cmh.backend.CourseManagement.controller;
|
||||
|
||||
import org.cmh.backend.CourseManagement.model.Course;
|
||||
import org.cmh.backend.CourseManagement.service.CourseService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/course")
|
||||
public class CourseController {
|
||||
|
||||
@Autowired
|
||||
private CourseService courseService;
|
||||
|
||||
@PostMapping("/create")
|
||||
public ResponseEntity<String> createCourse(
|
||||
@RequestParam("name") String name,
|
||||
@RequestParam("description") String description,
|
||||
@RequestParam("author") String author,
|
||||
@RequestParam("sortOrder") String sortOrder,
|
||||
@RequestParam("coverImage") MultipartFile coverImage,
|
||||
@RequestParam("video") MultipartFile video) {
|
||||
|
||||
Course createdCourse = courseService.createCourse(name, description, author, sortOrder, coverImage, video);
|
||||
if (createdCourse == null) {
|
||||
return new ResponseEntity<>("课程已存在", HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
return new ResponseEntity<>("课程创建成功", HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PutMapping("/edit/{id}")
|
||||
public ResponseEntity<String> updateCourse(
|
||||
@PathVariable Long id,
|
||||
@RequestParam("name") String name,
|
||||
@RequestParam("description") String description,
|
||||
@RequestParam("author") String author,
|
||||
@RequestParam("sortOrder") String sortOrder,
|
||||
@RequestParam(value = "coverImage", required = false) MultipartFile coverImage,
|
||||
@RequestParam(value = "video", required = false) MultipartFile video) {
|
||||
|
||||
Course updatedCourse = courseService.updateCourse(id, name, description, author, sortOrder, coverImage, video);
|
||||
if (updatedCourse == null) {
|
||||
return new ResponseEntity<>("课程未找到", HttpStatus.NOT_FOUND);
|
||||
}
|
||||
return new ResponseEntity<>("课程修改成功", HttpStatus.OK);
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete/{id}")
|
||||
public ResponseEntity<String> deleteCourse(@PathVariable Long id) {
|
||||
boolean isDeleted = courseService.deleteCourse(id);
|
||||
if (!isDeleted) {
|
||||
return new ResponseEntity<>("课程未找到", HttpStatus.NOT_FOUND);
|
||||
}
|
||||
return new ResponseEntity<>("课程删除成功", HttpStatus.OK);
|
||||
}
|
||||
|
||||
@GetMapping("/getAll")
|
||||
public ResponseEntity<List<Course>> getAllCourses() {
|
||||
List<Course> courses = courseService.getAllCourses();
|
||||
return new ResponseEntity<>(courses, HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
package org.cmh.backend.CourseManagement.controller;
|
||||
|
||||
import org.springframework.core.io.UrlResource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
public class FileController {
|
||||
private static final String UPLOAD_DIR = "uploads/";
|
||||
|
||||
@GetMapping("/files/{filename}")
|
||||
public ResponseEntity<Resource> getFile(@PathVariable String filename) {
|
||||
try {
|
||||
Path filePath = Paths.get(UPLOAD_DIR).resolve(filename).normalize();
|
||||
Resource resource = new UrlResource(filePath.toUri());
|
||||
|
||||
if (resource.exists() && resource.isReadable()) {
|
||||
return ResponseEntity.ok()
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
|
||||
.body(resource);
|
||||
} else {
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
package org.cmh.backend.CourseManagement.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@Entity
|
||||
@Table(name = "courses")
|
||||
public class Course {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String name;
|
||||
private String description;
|
||||
private String author;
|
||||
private String sortOrder;
|
||||
private String coverImagePath;
|
||||
private String videoPath;
|
||||
|
||||
// getters and setters
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
package org.cmh.backend.CourseManagement.repository;
|
||||
|
||||
import org.cmh.backend.CourseManagement.model.Course;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface CourseRepository extends JpaRepository<Course, Long> {
|
||||
Optional<Course> findByName(String name);
|
||||
}
|
||||
@ -0,0 +1,96 @@
|
||||
package org.cmh.backend.CourseManagement.service;
|
||||
|
||||
import org.cmh.backend.CourseManagement.model.Course;
|
||||
import org.cmh.backend.CourseManagement.repository.CourseRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class CourseService {
|
||||
|
||||
@Autowired
|
||||
private CourseRepository courseRepository;
|
||||
|
||||
public Course createCourse(String name, String description, String author, String sortOrder, MultipartFile coverImage, MultipartFile video) {
|
||||
Optional<Course> existingCourse = courseRepository.findByName(name);
|
||||
if (existingCourse.isPresent()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Course course = new Course();
|
||||
course.setName(name);
|
||||
course.setDescription(description);
|
||||
course.setAuthor(author);
|
||||
course.setSortOrder(sortOrder);
|
||||
|
||||
try {
|
||||
if (coverImage != null && !coverImage.isEmpty()) {
|
||||
String coverImagePath = saveFile(coverImage);
|
||||
course.setCoverImagePath(coverImagePath);
|
||||
}
|
||||
if (video != null && !video.isEmpty()) {
|
||||
String videoPath = saveFile(video);
|
||||
course.setVideoPath(videoPath);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
|
||||
return courseRepository.save(course);
|
||||
}
|
||||
|
||||
public Course updateCourse(Long id, String name, String description, String author, String sortOrder, MultipartFile coverImage, MultipartFile video) {
|
||||
return courseRepository.findById(id)
|
||||
.map(course -> {
|
||||
course.setName(name);
|
||||
course.setDescription(description);
|
||||
course.setAuthor(author);
|
||||
course.setSortOrder(sortOrder);
|
||||
|
||||
try {
|
||||
if (coverImage != null && !coverImage.isEmpty()) {
|
||||
String coverImagePath = saveFile(coverImage);
|
||||
course.setCoverImagePath(coverImagePath);
|
||||
}
|
||||
if (video != null && !video.isEmpty()) {
|
||||
String videoPath = saveFile(video);
|
||||
course.setVideoPath(videoPath);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
|
||||
return courseRepository.save(course);
|
||||
}).orElse(null);
|
||||
}
|
||||
|
||||
private String saveFile(MultipartFile file) throws IOException {
|
||||
String filename = System.currentTimeMillis() + "_" + file.getOriginalFilename();
|
||||
Path filePath = Paths.get("uploads", filename);
|
||||
Files.createDirectories(filePath.getParent());
|
||||
Files.write(filePath, file.getBytes());
|
||||
return filePath.toString();
|
||||
}
|
||||
|
||||
public boolean deleteCourse(Long id) {
|
||||
return courseRepository.findById(id)
|
||||
.map(course -> {
|
||||
courseRepository.delete(course);
|
||||
return true;
|
||||
}).orElse(false);
|
||||
}
|
||||
|
||||
public List<Course> getAllCourses() {
|
||||
return courseRepository.findAll();
|
||||
}
|
||||
}
|
||||
@ -1,45 +0,0 @@
|
||||
package org.cmh.backend.authentication.controller;
|
||||
|
||||
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.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/auth")
|
||||
public class AuthenticationController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@PostMapping("/register")
|
||||
public User register(@RequestBody User user) {
|
||||
return userService.register(user);
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
public ResponseEntity<Map<String, Object>> login(@RequestBody User loginRequest) {
|
||||
User user = userService.login(loginRequest.getUsername(), loginRequest.getPassword());
|
||||
if (user == null) {
|
||||
throw new RuntimeException("Invalid username or password");
|
||||
}
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("userId", user.getId());
|
||||
response.put("user", user);
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
|
||||
@GetMapping("/hello")
|
||||
public String hello(){
|
||||
return "Hello SpringBoot!";
|
||||
}
|
||||
|
||||
@PostMapping("/getVerificationCode")
|
||||
public String getVerificationCode(@RequestBody String contact) {
|
||||
return "Verification code sent to " + contact;
|
||||
}
|
||||
}
|
||||
@ -1,55 +1,64 @@
|
||||
package org.cmh.backend.authentication.controller;
|
||||
|
||||
import org.cmh.backend.Utils.JwtUtil;
|
||||
import org.cmh.backend.authentication.dto.*;
|
||||
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.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/users")
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@CrossOrigin(origins = "http://localhost:3000")
|
||||
@PostMapping("/register")
|
||||
public ResponseEntity<User> register(@RequestBody User user) {
|
||||
public ResponseEntity<RegisterResponse> register(@RequestBody User user) {
|
||||
User registeredUser = userService.register(user);
|
||||
return ResponseEntity.ok(registeredUser);
|
||||
if (registeredUser == null) {
|
||||
return new ResponseEntity<>(new RegisterResponse("用户已存在"),HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
return new ResponseEntity<>(new RegisterResponse("注册成功"),HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
public ResponseEntity<User> login(@RequestParam String username, @RequestParam String password) {
|
||||
User user = userService.login(username, password);
|
||||
public ResponseEntity<LoginResponse> login(@RequestBody LoginRequest request) {
|
||||
User user = userService.login(request.getUsername(), request.getPassword());
|
||||
if (user != null) {
|
||||
return ResponseEntity.ok(user);
|
||||
return new ResponseEntity<>(new LoginResponse("登录成功", JwtUtil.generateToken(user.getUsername())), HttpStatus.OK);
|
||||
}
|
||||
return ResponseEntity.status(401).build();
|
||||
return new ResponseEntity<>(new LoginResponse("登录失败", null), HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
|
||||
@PutMapping("/update")
|
||||
public ResponseEntity<User> updateUserInfo(@RequestBody User user) {
|
||||
User updatedUser = userService.updateUserInfo(user);
|
||||
return ResponseEntity.ok(updatedUser);
|
||||
@PostMapping("/update")
|
||||
public ResponseEntity<?> updateUserInfo(@RequestBody UpdateRequest user) {
|
||||
userService.updateUserInfo(user);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
@PutMapping("/changePassword")
|
||||
public ResponseEntity<?> changePassword(@RequestParam Long userId, @RequestParam String oldPassword, @RequestParam String newPassword) {
|
||||
boolean isChanged = userService.changePassword(userId, oldPassword, newPassword);
|
||||
@PostMapping("/changePassword")
|
||||
public ResponseEntity<?> changePassword(@RequestParam String username, @RequestParam String oldPassword, @RequestParam String newPassword) {
|
||||
boolean isChanged = userService.changePassword(username, oldPassword, newPassword);
|
||||
if (isChanged) {
|
||||
return ResponseEntity.ok("Password changed successfully");
|
||||
}
|
||||
return ResponseEntity.status(400).body("Old password is incorrect");
|
||||
}
|
||||
|
||||
@GetMapping("/{userId}")
|
||||
public ResponseEntity<User> getUserInfo(@PathVariable Long userId) {
|
||||
User user = userService.findById(userId).orElse(null);
|
||||
if (user != null) {
|
||||
return ResponseEntity.ok(user);
|
||||
@PostMapping("/getVerificationCode")
|
||||
public String getVerificationCode(@RequestBody String contact) {
|
||||
return "Verification code sent to " + contact;
|
||||
}
|
||||
|
||||
@GetMapping("/profile")
|
||||
public ResponseEntity<ProfileResponse> getUserInfo(@RequestParam String token) {
|
||||
if (!JwtUtil.isTokenValid(token)) {
|
||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
||||
}
|
||||
return ResponseEntity.status(404).build();
|
||||
String username = JwtUtil.extractUsername(token);
|
||||
return new ResponseEntity<>(userService.getUserInfo(username),HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
package org.cmh.backend.authentication.dto;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class LoginRequest {
|
||||
private String username;
|
||||
private String password;
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
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;
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
package org.cmh.backend.authentication.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
public class ProfileResponse {
|
||||
private String username;
|
||||
private String email;
|
||||
private String role;
|
||||
private String phoneNumber;
|
||||
private String company;
|
||||
private LocalDateTime createdDate;
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
package org.cmh.backend.authentication.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
public class RegisterResponse {
|
||||
private String message;
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
package org.cmh.backend.authentication.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
public class UpdateRequest {
|
||||
private String username;
|
||||
private String email;
|
||||
private String phoneNumber;
|
||||
private String company;
|
||||
}
|
||||
@ -9,6 +9,4 @@ import java.util.Optional;
|
||||
@Repository
|
||||
public interface UserRepository extends JpaRepository<User, Long> {
|
||||
User findByUsername(String username);
|
||||
Optional<User> findById(Long id);
|
||||
User save(User user);
|
||||
}
|
||||
@ -1,14 +1,72 @@
|
||||
package org.cmh.backend.authentication.service;
|
||||
|
||||
import org.cmh.backend.authentication.dto.ProfileResponse;
|
||||
import org.cmh.backend.authentication.dto.UpdateRequest;
|
||||
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.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
// UserService.java
|
||||
public interface UserService {
|
||||
User register(User user);
|
||||
User login(String username, String password);
|
||||
User updateUserInfo(User user);
|
||||
boolean changePassword(Long userId, String oldPassword, String newPassword);
|
||||
Optional<User> findById(Long id);
|
||||
@Service
|
||||
public class UserService {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
private final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
||||
|
||||
public User register(User user) {
|
||||
User existingUser = userRepository.findByUsername(user.getUsername());
|
||||
if (existingUser != null) {
|
||||
return null;
|
||||
}
|
||||
user.setPassword(passwordEncoder.encode(user.getPassword()));
|
||||
user.setCreatedDate(LocalDateTime.now());
|
||||
return userRepository.save(user);
|
||||
}
|
||||
|
||||
public User login(String username, String password) {
|
||||
User user = userRepository.findByUsername(username);
|
||||
if (user != null && passwordEncoder.matches(password, user.getPassword())) {
|
||||
return user;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void updateUserInfo(UpdateRequest newUser) {
|
||||
User user = userRepository.findByUsername(newUser.getUsername());
|
||||
if (user != null) {
|
||||
user.setEmail(newUser.getEmail());
|
||||
user.setPhoneNumber(newUser.getPhoneNumber());
|
||||
user.setCompany(newUser.getCompany());
|
||||
userRepository.save(user);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean changePassword(String username, String oldPassword, String newPassword) {
|
||||
User user = userRepository.findByUsername(username);
|
||||
if (user != null && passwordEncoder.matches(oldPassword, user.getPassword())) {
|
||||
user.setPassword(passwordEncoder.encode(newPassword));
|
||||
userRepository.save(user);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public ProfileResponse getUserInfo(String username) {
|
||||
User user = userRepository.findByUsername(username);
|
||||
return new ProfileResponse(
|
||||
user.getUsername(),
|
||||
user.getEmail(),
|
||||
user.getRole(),
|
||||
user.getPhoneNumber(),
|
||||
user.getCompany(),
|
||||
user.getCreatedDate()
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -1,58 +0,0 @@
|
||||
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;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Optional;
|
||||
|
||||
// UserServiceImpl.java
|
||||
@Service
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
private PasswordEncoder passwordEncoder;
|
||||
|
||||
@Override
|
||||
public User register(User user) {
|
||||
user.setPassword(passwordEncoder.encode(user.getPassword()));
|
||||
user.setCreatedDate(LocalDateTime.now());
|
||||
return userRepository.save(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public User login(String username, String password) {
|
||||
User user = userRepository.findByUsername(username);
|
||||
if (user != null && passwordEncoder.matches(password, user.getPassword())) {
|
||||
return user;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public User updateUserInfo(User user) {
|
||||
return userRepository.save(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean changePassword(Long userId, String oldPassword, String newPassword) {
|
||||
User user = userRepository.findById(userId).orElse(null);
|
||||
if (user != null && passwordEncoder.matches(oldPassword, user.getPassword())) {
|
||||
user.setPassword(passwordEncoder.encode(newPassword));
|
||||
userRepository.save(user);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<User> findById(Long id) {
|
||||
return userRepository.findById(id);
|
||||
}
|
||||
}
|
||||
@ -1,7 +0,0 @@
|
||||
spring.datasource.url=jdbc:h2:mem:testdb
|
||||
spring.datasource.driverClassName=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=password
|
||||
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
spring.h2.console.enabled=true
|
||||
@ -1,12 +0,0 @@
|
||||
package org.cmh.backend;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
public class BackendApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
package org.cmh.backend;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class TestBackendApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TestBackendApplication.class, args);
|
||||
}
|
||||
}
|
||||
@ -1,25 +0,0 @@
|
||||
package org.cmh.backend;
|
||||
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.testcontainers.containers.MariaDBContainer;
|
||||
import org.testcontainers.containers.MySQLContainer;
|
||||
import org.testcontainers.utility.DockerImageName;
|
||||
|
||||
@TestConfiguration(proxyBeanMethods = false)
|
||||
class TestcontainersConfiguration {
|
||||
|
||||
@Bean
|
||||
@ServiceConnection
|
||||
MariaDBContainer<?> mariaDbContainer() {
|
||||
return new MariaDBContainer<>(DockerImageName.parse("mariadb:latest"));
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ServiceConnection
|
||||
MySQLContainer<?> mysqlContainer() {
|
||||
return new MySQLContainer<>(DockerImageName.parse("mysql:latest"));
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,88 +0,0 @@
|
||||
package org.cmh.backend.Utils;
|
||||
|
||||
import org.cmh.backend.authentication.service.UserService;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
public class JwtVerifyAspectTest {
|
||||
|
||||
@Configuration
|
||||
@EnableAspectJAutoProxy
|
||||
@Import({JwtVerifyAspect.class})
|
||||
static class Config {
|
||||
@Bean
|
||||
public JwtUtil jwtUtil() {
|
||||
return Mockito.mock(JwtUtil.class);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public UserService userService() {
|
||||
return Mockito.mock(UserService.class);
|
||||
}
|
||||
}
|
||||
|
||||
private JwtUtil jwtUtil = new JwtUtil();
|
||||
|
||||
@InjectMocks
|
||||
private JwtVerifyAspect jwtVerifyAspect;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpClass() {
|
||||
// Static setup if needed
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
Mockito.when(jwtUtil.isTokenValid("validToken")).thenReturn(true);
|
||||
Mockito.when(jwtUtil.isTokenValid("invalidToken")).thenReturn(false);
|
||||
}
|
||||
|
||||
// TODO:这个测试跑不动,有问题,先取消掉
|
||||
// @Test
|
||||
// public void testVerify() {
|
||||
// SomeController validTokenController = new SomeController("validToken");
|
||||
// SomeController invalidTokenController = new SomeController("invalidToken");
|
||||
//
|
||||
// Assert.assertTrue("Valid token should pass verification", validTokenController.run());
|
||||
// Assert.assertFalse("Invalid token should fail verification", invalidTokenController.run());
|
||||
// }
|
||||
}
|
||||
|
||||
class SomeController {
|
||||
private SomeJwtRequest request;
|
||||
|
||||
SomeController(String token) {
|
||||
this.request = new SomeJwtRequest(token, "test");
|
||||
}
|
||||
|
||||
public boolean run() {
|
||||
try {
|
||||
return verify(request);
|
||||
} catch (JwtValidationException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@JwtVerify
|
||||
public boolean verify(SomeJwtRequest request) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
class SomeJwtRequest extends JwtRequest {
|
||||
String msg;
|
||||
|
||||
public SomeJwtRequest(String token, String msg) {
|
||||
super.setToken(token);
|
||||
this.msg = msg;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user