修改部分接口,方便前后端链接
Change-Id: Icce71f5085084a4185cb11fe3313d618dfb01177
diff --git a/Dockerfile b/Dockerfile
index abbe549..b646295 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -7,8 +7,6 @@
# 复制 JAR 文件到容器
COPY target/*.jar team12.jar
-COPY nginx.conf /etc/nginx/conf.d/g12b.conf
-
# 暴露端口(与 application.yml/server.port 一致)
EXPOSE 8080
diff --git a/nginx.conf b/nginx.conf
deleted file mode 100644
index d1ccc05..0000000
--- a/nginx.conf
+++ /dev/null
@@ -1,15 +0,0 @@
-server{
- listen 80;
- server_name team12.10813352.xyz;
-
- # 解决 React Router 单页应用路由问题
- location / {
- return 200 "Hello, World!"
- }
-
- # API 请求代理到后端
- location /api/ {
-
- }
-
-}
\ No newline at end of file
diff --git a/src/main/java/com/pt/controller/AdminController.java b/src/main/java/com/pt/controller/AdminController.java
index 22e2b86..8af5b56 100644
--- a/src/main/java/com/pt/controller/AdminController.java
+++ b/src/main/java/com/pt/controller/AdminController.java
@@ -6,11 +6,7 @@
import com.pt.service.AdminService;
import com.pt.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.CrossOrigin;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
-import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
import java.util.HashMap;
@@ -25,18 +21,18 @@
private AdminService adminService;
@PostMapping("/login")
- public ResponseEntity<?> loginAdmin(@RequestParam("username") String username,
- @RequestParam("password") String password) {
+ public ResponseEntity<?> loginAdmin(@RequestBody Map<String, String> request) {
+ String username = request.get("username");
+ String password = request.get("password");
+
Map<String, Object> ans = new HashMap<>();
Admin admin = adminService.findByUsernameAndPassword(username, password);
if (admin != null) {
- ans.put("result", "Login successful");
- ans.put("data", Map.of(
- "token", JWTUtils.generateToken(username, Constants.UserRole.ADMIN, (int)1.2e8)
- ));
+ ans.put("message", "Login successful");
+ ans.put("data", JWTUtils.generateToken(username, Constants.UserRole.ADMIN, Constants.DEFAULT_EXPIRE_TIME));
return ResponseEntity.ok().body(ans);
} else {
- ans.put("result", "Invalid username or password");
+ ans.put("message", "Invalid username or password");
return ResponseEntity.badRequest().body(ans);
}
}
diff --git a/src/main/java/com/pt/controller/CommentController.java b/src/main/java/com/pt/controller/CommentController.java
index 17dabb3..00e091d 100644
--- a/src/main/java/com/pt/controller/CommentController.java
+++ b/src/main/java/com/pt/controller/CommentController.java
@@ -23,55 +23,59 @@
@PostMapping("/add")
public ResponseEntity<?> addComment(
@RequestHeader("token") String token,
- @RequestParam("content") String content,
- @RequestParam("username") String username,
- @RequestParam("postId") int postId
+ @RequestBody Map<String, String> request
) {
+ String content = request.get("content");
+ String username = request.get("username");
+ int postId = Integer.parseInt(request.get("postId"));
+
Map<String, Object> ans = new HashMap<>();
if (!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) {
- ans.put("result", "Invalid token");
+ ans.put("message", "Invalid token");
return ResponseEntity.badRequest().body(ans);
}
commentService.addComment(content, username, postId);
- ans.put("result", "Comment added successfully");
+ ans.put("message", "Comment added successfully");
return ResponseEntity.ok(ans);
}
@DeleteMapping("/delete")
public ResponseEntity<?> deleteComment(
@RequestHeader("token") String token,
- @RequestParam("commentId") int commentId,
- @RequestParam("username") String username
+ @RequestBody Map<String, String> request
) {
+ String username = request.get("username");
+ int commentId = Integer.parseInt(request.get("commentId"));
+
Map<String, Object> ans = new HashMap<>();
if (!JWTUtils.checkToken(token, username, Constants.UserRole.ADMIN)) {
- ans.put("result", "Invalid token");
+ ans.put("message", "Invalid token");
return ResponseEntity.badRequest().body(ans);
}
commentService.deleteComment(commentId);
- ans.put("result", "Comment deleted successfully");
+ ans.put("message", "Comment deleted successfully");
return ResponseEntity.ok(ans);
}
@GetMapping("/get")
public ResponseEntity<?> getComments(
@RequestHeader("token") String token,
- @RequestParam("postId") int postId,
- @RequestParam("username") String username
+ @RequestParam("username") String username,
+ @RequestParam("postId") int postId
) {
Map<String, Object> ans = new HashMap<>();
if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) {
- ans.put("result", "Invalid token");
+ ans.put("message", "Invalid token");
return ResponseEntity.badRequest().body(ans);
}
List<Comment> comments = commentService.getCommentsByPostId(postId);
- ans.put("result", "Comments retrieved successfully");
+ ans.put("message", "Comments retrieved successfully");
ans.put("data", Map.of(
"comments", comments
));
diff --git a/src/main/java/com/pt/controller/PostController.java b/src/main/java/com/pt/controller/PostController.java
index 0ab86ed..3723dbd 100644
--- a/src/main/java/com/pt/controller/PostController.java
+++ b/src/main/java/com/pt/controller/PostController.java
@@ -31,10 +31,11 @@
@PostMapping("/create")
public ResponseEntity<?> createPost(
@RequestHeader("token") String token,
- @RequestParam("title") String title,
- @RequestParam("content") String content,
- @RequestParam("author") String author
+ @RequestBody Map<String, String> request
) {
+ String title = request.get("title");
+ String content = request.get("content");
+ String author = request.get("author");
Map<String, Object> ans = new HashMap<>();
@@ -50,7 +51,7 @@
}
postService.createPost(title, content, author);
- ans.put("result", "Post created successfully");
+ ans.put("message", "Post created successfully");
return ResponseEntity.ok(ans);
}
@@ -70,11 +71,10 @@
@RequestParam(value = "author", required = false) String author,
@RequestParam(value = "date", required = false) String date
) {
-
Map<String, Object> ans = new HashMap<>();
if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){
- ans.put("result", "Invalid token");
+ ans.put("message", "Invalid token");
return ResponseEntity.badRequest().body(ans);
}
@@ -91,7 +91,7 @@
if(date != null){
posts.removeIf(post -> !post.getPublishDate().toString().equals(date));
}
- ans.put("result", "Post retrieved successfully");
+ ans.put("message", "Post retrieved successfully");
ans.put("data", Map.of(
"post", posts
));
@@ -108,25 +108,26 @@
@DeleteMapping("/delete")
public ResponseEntity<?> deletePost(
@RequestHeader("token") String token,
- @RequestParam("username") String username,
- @RequestParam("pid") int pid
+ @RequestBody Map<String, String> request
) {
+ String username = request.get("username");
+ int pid = Integer.parseInt(request.get("pid"));
Map<String, Object> ans = new HashMap<>();
if(!JWTUtils.checkToken(token, username, Constants.UserRole.ADMIN)){
- ans.put("result", "Invalid token");
+ ans.put("message", "Invalid token");
return ResponseEntity.badRequest().body(ans);
}
Post post = postService.findPostById(pid);
if (post == null) {
- ans.put("result", "Post not found");
+ ans.put("message", "Post not found");
return ResponseEntity.badRequest().body(ans);
}
postService.deletePost(post);
- ans.put("result", "Post deleted successfully");
+ ans.put("message", "Post deleted successfully");
return ResponseEntity.ok(ans);
}
}
diff --git a/src/main/java/com/pt/controller/ResourceController.java b/src/main/java/com/pt/controller/ResourceController.java
index 411e7bc..3e3c875 100644
--- a/src/main/java/com/pt/controller/ResourceController.java
+++ b/src/main/java/com/pt/controller/ResourceController.java
@@ -7,6 +7,7 @@
import com.pt.service.UserService;
import com.pt.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@@ -29,10 +30,11 @@
@GetMapping("/list/all")
public ResponseEntity<?> getAllResources(@RequestHeader("token") String token,
@RequestParam("username") String username) {
+
Map<String, Object> ans = new HashMap<>();
if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){
- ans.put("result", "Invalid token");
+ ans.put("message", "Invalid token");
return ResponseEntity.badRequest().body(ans);
}
@@ -46,10 +48,11 @@
@GetMapping("/list/user")
public ResponseEntity<?> getUserResources(@RequestHeader("token") String token,
@RequestParam("username") String username) {
+
Map<String, Object> ans = new HashMap<>();
if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){
- ans.put("result", "Invalid token");
+ ans.put("message", "Invalid token");
return ResponseEntity.badRequest().body(ans);
}
@@ -72,13 +75,13 @@
Map<String, Object> ans = new HashMap<>();
if (!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) {
- ans.put("result", "Invalid token");
+ ans.put("message", "Invalid token");
return ResponseEntity.badRequest().body(ans);
}
User user = userService.findByUsername(username);
if (user == null || user.getLevel() < 2) {
- ans.put("result", "Insufficient permissions to publish resources");
+ ans.put("message", "Insufficient permissions to publish resources");
return ResponseEntity.status(403).body(ans);
}
@@ -86,11 +89,11 @@
// 传入种子文件字节,同时传入资源其他信息
resourceService.publishResource(name, description, username, size, torrentFile.getBytes());
} catch (Exception e) {
- ans.put("result", "Failed to publish resource: " + e.getMessage());
+ ans.put("message", "Failed to publish resource: " + e.getMessage());
return ResponseEntity.status(500).body(ans);
}
- ans.put("result", "Resource published successfully");
+ ans.put("message", "Resource published successfully");
return ResponseEntity.ok(ans);
}
@@ -104,7 +107,7 @@
Map<String, Object> ans = new HashMap<>();
if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) {
- ans.put("result", "Invalid token");
+ ans.put("message", "Invalid token");
return ResponseEntity.badRequest().body(ans);
}
@@ -122,7 +125,7 @@
Map<String, Object> ans = new HashMap<>();
if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) {
- ans.put("result", "Invalid token");
+ ans.put("message", "Invalid token");
return ResponseEntity.badRequest().body(ans);
}
@@ -145,7 +148,7 @@
Map<String, Object> ans = new HashMap<>();
if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){
- ans.put("result", "Invalid token");
+ ans.put("message", "Invalid token");
return ResponseEntity.badRequest().body(ans);
}
@@ -164,18 +167,18 @@
Resource resource = resourceService.getResourceById(resourceId);
if(!JWTUtils.checkToken(token, username, Constants.UserRole.ADMIN) || resource == null || !resource.getAuthor().equals(username)) {
- ans.put("result", "Invalid token or insufficient permissions");
+ ans.put("message", "Invalid token or insufficient permissions");
return ResponseEntity.badRequest().body(ans);
}
try {
resourceService.deleteResource(resourceId);
} catch (Exception e) {
- ans.put("result", "Failed to delete resource: " + e.getMessage());
+ ans.put("message", "Failed to delete resource: " + e.getMessage());
return ResponseEntity.status(500).body(ans);
}
- ans.put("result", "Resource deleted successfully");
+ ans.put("message", "Resource deleted successfully");
return ResponseEntity.ok(ans);
}
diff --git a/src/main/java/com/pt/controller/UserController.java b/src/main/java/com/pt/controller/UserController.java
index 577b2a3..7222f36 100644
--- a/src/main/java/com/pt/controller/UserController.java
+++ b/src/main/java/com/pt/controller/UserController.java
@@ -56,7 +56,7 @@
userService.save(newUser);
Map<String, Object> ans = new HashMap<>();
- ans.put("result", "User registered successfully");
+ ans.put("message", "User registered successfully");
ans.put("data", newUser);
return ResponseEntity.ok().body(ans);
@@ -64,9 +64,9 @@
}
@PostMapping("/login")
- public ResponseEntity<?> loginUser(@RequestParam("username") String username,
- @RequestParam("password") String password) {
-
+ public ResponseEntity<?> loginUser(@RequestBody Map<String, String> request) {
+ String username = request.get("username");
+ String password = request.get("password");
if (username == null || password == null) {
return ResponseEntity.badRequest().body("Missing username or password");
@@ -76,22 +76,24 @@
Map<String, Object> ans = new HashMap<>();
if (user != null) {
String token = JWTUtils.generateToken(username, Constants.UserRole.USER, Constants.DEFAULT_EXPIRE_TIME);
- ans.put("result", "Login successful");
+ ans.put("message", "Login successful");
ans.put("data", token);
return ResponseEntity.ok().body(ans);
} else {
- ans.put("result", "Invalid username or password");
+ ans.put("message", "Invalid username or password");
return ResponseEntity.badRequest().body(ans);
}
}
@PostMapping("/update/username")
public ResponseEntity<?> updateUsername(@RequestHeader("token") String token,
- @RequestParam("username") String oldUsername,
- @RequestParam("newUsername") String newUsername) {
+ @RequestBody Map<String, String> request) {
+ String oldUsername = request.get("username");
+ String newUsername = request.get("newUsername");
+
Map<String, Object> ans = new HashMap<>();
if(!JWTUtils.checkToken(token, oldUsername, Constants.UserRole.USER)) {
- ans.put("result", "Invalid token");
+ ans.put("message", "Invalid token");
return ResponseEntity.badRequest().body(ans);
}
@@ -99,21 +101,24 @@
if (user != null) {
user.setUsername(newUsername);
userService.save(user);
- ans.put("result", "Username updated successfully");
+ ans.put("message", "Username updated successfully");
return ResponseEntity.ok(ans);
} else {
- ans.put("result", "User not found");
+ ans.put("message", "User not found");
return ResponseEntity.badRequest().body(ans);
}
}
@PostMapping("/update/password")
public ResponseEntity<?> updatePassword(@RequestHeader("token") String token,
- @RequestParam("username") String username,
- @RequestParam("newPassword") String newPassword) {
+ @RequestBody Map<String, String> request
+ ) {
+ String username = request.get("username");
+ String newPassword = request.get("newPassword");
+
Map<String, Object> ans = new HashMap<>();
if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) {
- ans.put("result", "Invalid token");
+ ans.put("message", "Invalid token");
return ResponseEntity.badRequest().body(ans);
}
@@ -121,22 +126,23 @@
if (user != null) {
user.setPassword(newPassword);
userService.save(user);
- ans.put("result", "Password updated successfully");
+ ans.put("message", "Password updated successfully");
return ResponseEntity.ok(ans);
} else {
- ans.put("result", "Invalid username or password");
+ ans.put("message", "Invalid username or password");
return ResponseEntity.badRequest().body(ans);
}
}
@PostMapping("/update/email")
public ResponseEntity<?> updateEmail(@RequestHeader("token") String token,
- @RequestParam("username") String username,
- @RequestParam("newEmail") String newEmail) {
+ @RequestBody Map<String, String> request) {
+ String username = request.get("username");
+ String newEmail = request.get("newEmail");
Map<String, Object> ans = new HashMap<>();
if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) {
- ans.put("result", "Invalid token");
+ ans.put("message", "Invalid token");
return ResponseEntity.badRequest().body(ans);
}
@@ -144,45 +150,47 @@
if (user != null) {
user.setEmail(newEmail);
userService.save(user);
- ans.put("result", "Email updated successfully");
+ ans.put("message", "Email updated successfully");
return ResponseEntity.ok(ans);
} else {
- ans.put("result", "User not found");
+ ans.put("message", "User not found");
return ResponseEntity.badRequest().body(ans);
}
}
@DeleteMapping("/delete")
public ResponseEntity<?> deleteUser(@RequestHeader("token") String token,
- @RequestParam("username") String username,
- @RequestParam("targetUsername") String targetUsername
- ) {
+ @RequestBody Map<String, String> request) {
+ String username = request.get("username");
+ String targetUsername = request.get("targetUsername");
+
Map<String, Object> ans = new HashMap<>();
if(!JWTUtils.checkToken(token, username, Constants.UserRole.ADMIN)) {
- ans.put("result", "Invalid token");
+ ans.put("message", "Invalid token");
return ResponseEntity.badRequest().body(ans);
}
User user = userService.findByUsername(targetUsername);
if (user != null) {
userService.deleteById(user.getUid());
- ans.put("result", "User deleted successfully");
+ ans.put("message", "User deleted successfully");
return ResponseEntity.ok(ans);
} else {
- ans.put("result", "User not found");
+ ans.put("message", "User not found");
return ResponseEntity.badRequest().body(ans);
}
}
@GetMapping("/list")
public ResponseEntity<?> listUsers(@RequestHeader("token") String token,
- @RequestParam("username") String username) {
+ @RequestBody Map<String, String> request) {
+ String username = request.get("username");
if(!JWTUtils.checkToken(token, username, Constants.UserRole.ADMIN)) {
return ResponseEntity.badRequest().body("Invalid token");
}
Map<String, Object> ans = new HashMap<>();
- ans.put("result", "User list retrieved successfully");
+ ans.put("message", "User list retrieved successfully");
ans.put("data", Map.of(
"amount", userService.listAll().size(),
"users", userService.listAll()
@@ -193,19 +201,20 @@
@GetMapping("/get/info")
public ResponseEntity<?> getUserInfo(@RequestHeader("token") String token,
@RequestParam("username") String username) {
+
Map<String, Object> ans = new HashMap<>();
if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) {
- ans.put("result", "Invalid token");
+ ans.put("message", "Invalid token");
return ResponseEntity.badRequest().body(ans);
}
User user = userService.findByUsername(username);
if (user != null) {
- ans.put("result", "User info retrieved successfully");
+ ans.put("message", "User info retrieved successfully");
ans.put("data", user);
return ResponseEntity.ok(ans);
} else {
- ans.put("result", "User not found");
+ ans.put("message", "User not found");
return ResponseEntity.badRequest().body(ans);
}
}