Pārlūkot izejas kodu

自定义异常

zhenghao 5 mēneši atpakaļ
vecāks
revīzija
6b492ee9ff

+ 1 - 2
src/main/java/cn/jlsxwkj/common/config/SaTokenConfigure.java

@@ -19,7 +19,6 @@ public class SaTokenConfigure implements WebMvcConfigurer {
     @Override
     public void addInterceptors(InterceptorRegistry registry) {
         registry.addInterceptor(new SaInterceptor(handler -> StpUtil.checkLogin()))
-                .addPathPatterns("/**")
-                .excludePathPatterns("/user/login", "/user/auth");
+                .addPathPatterns("/**");
     }
 }

+ 14 - 0
src/main/java/cn/jlsxwkj/common/exception/AccountAuthFailException.java

@@ -0,0 +1,14 @@
+package cn.jlsxwkj.common.exception;
+
+/**
+ * @author zh
+ */
+public class AccountAuthFailException extends CustomException {
+
+    /**
+     * 注册失败
+     */
+    public AccountAuthFailException(String message) {
+        super(message);
+    }
+}

+ 12 - 0
src/main/java/cn/jlsxwkj/common/exception/CustomException.java

@@ -0,0 +1,12 @@
+package cn.jlsxwkj.common.exception;
+
+/**
+ * @author zh
+ * 抽象自定义异常类, 用于统一捕捉
+ */
+public abstract class CustomException extends Exception {
+
+    public CustomException(String message) {
+        super(message);
+    }
+}

+ 14 - 0
src/main/java/cn/jlsxwkj/common/exception/FileDeleteFailException.java

@@ -0,0 +1,14 @@
+package cn.jlsxwkj.common.exception;
+
+/**
+ * @author zh
+ */
+public class FileDeleteFailException extends CustomException {
+
+    /**
+     * 文件删除失败
+     */
+    public FileDeleteFailException(String message) {
+        super(message);
+    }
+}

+ 14 - 0
src/main/java/cn/jlsxwkj/common/exception/FileTypeDoesNotSupportException.java

@@ -0,0 +1,14 @@
+package cn.jlsxwkj.common.exception;
+
+/**
+ * @author zh
+ */
+public class FileTypeDoesNotSupportException extends CustomException {
+
+    /**
+     * 文件类型不支持
+     */
+    public FileTypeDoesNotSupportException(String message) {
+        super(message);
+    }
+}

+ 14 - 0
src/main/java/cn/jlsxwkj/common/exception/LoginAccountAlreadyLoginException.java

@@ -0,0 +1,14 @@
+package cn.jlsxwkj.common.exception;
+
+/**
+ * @author zh
+ */
+public class LoginAccountAlreadyLoginException extends CustomException{
+
+    /**
+     * 重复登录
+     */
+    public LoginAccountAlreadyLoginException(String message) {
+        super(message);
+    }
+}

+ 14 - 0
src/main/java/cn/jlsxwkj/common/exception/LoginWrongPasswordException.java

@@ -0,0 +1,14 @@
+package cn.jlsxwkj.common.exception;
+
+/**
+ * @author zh
+ */
+public class LoginWrongPasswordException extends CustomException{
+
+    /**
+     * 登录密码错误
+     */
+    public LoginWrongPasswordException(String message) {
+        super(message);
+    }
+}

+ 14 - 0
src/main/java/cn/jlsxwkj/common/exception/UnknownException.java

@@ -0,0 +1,14 @@
+package cn.jlsxwkj.common.exception;
+
+/**
+ * @author zh
+ */
+public class UnknownException extends CustomException {
+
+    /**
+     *
+     */
+    public UnknownException(String message) {
+        super(message);
+    }
+}

+ 2 - 2
src/main/java/cn/jlsxwkj/common/handler/GlobalRestExceptionHandler.java

@@ -2,6 +2,7 @@ package cn.jlsxwkj.common.handler;
 
 import cn.dev33.satoken.exception.SaTokenException;
 import cn.jlsxwkj.common.R.ResponseError;
+import cn.jlsxwkj.common.exception.CustomException;
 import cn.jlsxwkj.common.utils.Log;
 import cn.jlsxwkj.moudles.logerror.LogError;
 import cn.jlsxwkj.moudles.logerror.LogErrorService;
@@ -10,7 +11,6 @@ import org.springframework.web.ErrorResponse;
 import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.RestControllerAdvice;
 
-import javax.security.auth.login.LoginException;
 import java.util.Arrays;
 
 /**
@@ -31,7 +31,7 @@ public class GlobalRestExceptionHandler {
     public ResponseError exception(Exception e) {
         ResponseError errorHandler = ResponseError.data(e.getClass().getName());
 
-        if (e instanceof LoginException) {
+        if (e instanceof CustomException) {
             errorHandler.setMessage(e.getMessage());
         }
         if (e instanceof ErrorResponse) {

+ 4 - 1
src/main/java/cn/jlsxwkj/moudles/chat/ChatController.java

@@ -1,5 +1,7 @@
 package cn.jlsxwkj.moudles.chat;
 
+import cn.dev33.satoken.annotation.SaIgnore;
+import cn.jlsxwkj.common.exception.CustomException;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.tags.Tag;
 import jakarta.annotation.Resource;
@@ -23,7 +25,8 @@ public class ChatController {
 
 	@Operation(summary = "上传文档")
 	@PostMapping("/upload")
-	public String uploadDoc(@RequestBody MultipartFile file) throws IOException {
+	@SaIgnore
+	public String uploadDoc(@RequestBody MultipartFile file) throws IOException, CustomException {
 		return chatService.uploadDocument(file);
 	}
 

+ 8 - 4
src/main/java/cn/jlsxwkj/moudles/chat/ChatService.java

@@ -2,6 +2,10 @@ package cn.jlsxwkj.moudles.chat;
 
 import cn.dev33.satoken.SaManager;
 import cn.hutool.crypto.digest.MD5;
+import cn.jlsxwkj.common.exception.CustomException;
+import cn.jlsxwkj.common.exception.FileDeleteFailException;
+import cn.jlsxwkj.common.exception.FileTypeDoesNotSupportException;
+import cn.jlsxwkj.common.exception.UnknownException;
 import cn.jlsxwkj.common.reader.ParagraphDocReader;
 import cn.jlsxwkj.common.reader.ParagraphTextReader;
 import cn.jlsxwkj.common.utils.Log;
@@ -75,7 +79,7 @@ public class ChatService {
 	 * @return 保存状态
 	 * @throws IOException 保存失败
 	 */
-	public String uploadDocument(MultipartFile file) throws IOException {
+	public String uploadDocument(MultipartFile file) throws CustomException, IOException {
 		//分割文件名为 文件名, 后缀
 		String[] split = Objects.requireNonNull(file.getOriginalFilename()).split("\\.");
 		//获取文件类型
@@ -97,7 +101,7 @@ public class ChatService {
 		switch (fileType) {
 			case "txt" -> reader = new ParagraphTextReader(fileUrl, 5);
 			case "doc", "docx" -> reader = new ParagraphDocReader(fileUrl, 5);
-			default -> throw new IOException("暂不支持的文件类型: " + fileType);
+			default -> throw new FileTypeDoesNotSupportException("暂不支持的文件类型: " + fileType);
 		}
 		// 判断文件是否存在
 		if (!saveFile.exists()) {
@@ -108,11 +112,11 @@ public class ChatService {
 			} catch (Exception e){
 				boolean delete = saveFile.delete();
 				if (!delete) {
-					throw new IOException("删除文件失败: " + fileUrl);
+					throw new FileDeleteFailException("删除文件失败: " + fileUrl);
 				}
 			}
 		}
-		throw new IOException("未知错误");
+		throw new UnknownException("未知错误");
 	}
 
 	/**

+ 4 - 3
src/main/java/cn/jlsxwkj/moudles/userlist/UserListController.java

@@ -1,12 +1,12 @@
 package cn.jlsxwkj.moudles.userlist;
 
+import cn.dev33.satoken.annotation.SaIgnore;
 import cn.dev33.satoken.stp.StpUtil;
+import cn.jlsxwkj.common.exception.CustomException;
 import io.swagger.v3.oas.annotations.Operation;
 import jakarta.annotation.Resource;
 import org.springframework.web.bind.annotation.*;
 
-import javax.security.auth.login.LoginException;
-
 /**
  * @author zh
  */
@@ -19,8 +19,9 @@ public class UserListController {
 
     @Operation(summary = "登录")
     @PostMapping("/login")
+    @SaIgnore
     public UserVO login(@RequestParam String userName,
-                          @RequestParam String userPassword) throws LoginException {
+                        @RequestParam String userPassword) throws CustomException {
         return userListService.checkOrAddUser(userName, userPassword);
     }
 

+ 8 - 8
src/main/java/cn/jlsxwkj/moudles/userlist/UserListService.java

@@ -3,13 +3,13 @@ package cn.jlsxwkj.moudles.userlist;
 import cn.dev33.satoken.secure.SaSecureUtil;
 import cn.dev33.satoken.stp.StpUtil;
 import cn.hutool.core.lang.Snowflake;
+import cn.jlsxwkj.common.exception.AccountAuthFailException;
+import cn.jlsxwkj.common.exception.CustomException;
+import cn.jlsxwkj.common.exception.LoginAccountAlreadyLoginException;
+import cn.jlsxwkj.common.exception.LoginWrongPasswordException;
 import jakarta.annotation.Resource;
 import org.springframework.stereotype.Service;
 
-import javax.security.auth.login.AccountException;
-import javax.security.auth.login.FailedLoginException;
-import javax.security.auth.login.LoginException;
-
 /**
  * @author zh
  */
@@ -20,13 +20,13 @@ public class UserListService {
     private UserListMapper userListMapper;
     private final Snowflake snowflake = new Snowflake();
 
-    public UserVO checkOrAddUser(String userName, String userPassword) throws LoginException {
+    public UserVO checkOrAddUser(String userName, String userPassword) throws CustomException {
         String sha512pwd = SaSecureUtil.sha512(userPassword);
         UserList userList = userListMapper.checkUser(userName);
         UserVO userVO = new UserVO();
         if (userList != null) {
             if (!userList.getUserPassword().contains(sha512pwd)) {
-                throw new FailedLoginException("密码错误");
+                throw new LoginWrongPasswordException("密码错误");
             }
             if (!StpUtil.isLogin()) {
                 String userId = userList.getUserId();
@@ -36,7 +36,7 @@ public class UserListService {
                 userVO.setMessage("登录成功");
                 return userVO;
             }
-            throw new LoginException("请勿重复登录");
+            throw new LoginAccountAlreadyLoginException("请勿重复登录");
         }
         userList = new UserList();
         userList.setUserId(snowflake.nextIdStr());
@@ -52,6 +52,6 @@ public class UserListService {
             return userVO;
 
         }
-        throw new AccountException("注册失败");
+        throw new AccountAuthFailException("注册失败");
     }
 }