Просмотр исходного кода

Update 优化 拆分异常处理器

YueYunyun 1 год назад
Родитель
Сommit
a1ab510f4e

+ 10 - 0
SERVER/VberAdminPlusV3/vber-common/vber-common-satoken/src/main/java/com/vber/common/satoken/config/SaTokenConfig.java

@@ -7,6 +7,7 @@ import cn.dev33.satoken.stp.StpLogic;
 import com.vber.common.core.factory.YmlPropertySourceFactory;
 import com.vber.common.satoken.core.dao.PlusSaTokenDao;
 import com.vber.common.satoken.core.service.SaPermissionImpl;
+import com.vber.common.satoken.handler.SaTokenExceptionHandler;
 import org.springframework.boot.autoconfigure.AutoConfiguration;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.PropertySource;
@@ -42,4 +43,13 @@ public class SaTokenConfig {
         return new PlusSaTokenDao();
     }
 
+
+    /**
+     * 异常处理器
+     */
+    @Bean
+    public SaTokenExceptionHandler saTokenExceptionHandler() {
+        return new SaTokenExceptionHandler();
+    }
+
 }

+ 52 - 0
SERVER/VberAdminPlusV3/vber-common/vber-common-satoken/src/main/java/com/vber/common/satoken/handler/SaTokenExceptionHandler.java

@@ -0,0 +1,52 @@
+package com.vber.common.satoken.handler;
+
+import cn.dev33.satoken.exception.NotLoginException;
+import cn.dev33.satoken.exception.NotPermissionException;
+import cn.dev33.satoken.exception.NotRoleException;
+import cn.hutool.http.HttpStatus;
+import com.vber.common.core.domain.R;
+import jakarta.servlet.http.HttpServletRequest;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+
+/**
+ * SaToken异常处理器
+ *
+ * @author iwb
+ */
+@Slf4j
+@RestControllerAdvice
+public class SaTokenExceptionHandler {
+
+    /**
+     * 权限码异常
+     */
+    @ExceptionHandler(NotPermissionException.class)
+    public R<Void> handleNotPermissionException(NotPermissionException e, HttpServletRequest request) {
+        String requestURI = request.getRequestURI();
+        log.error("请求地址'{}',权限码校验失败'{}'", requestURI, e.getMessage());
+        return R.fail(HttpStatus.HTTP_FORBIDDEN, "没有访问权限,请联系管理员授权");
+    }
+
+    /**
+     * 角色权限异常
+     */
+    @ExceptionHandler(NotRoleException.class)
+    public R<Void> handleNotRoleException(NotRoleException e, HttpServletRequest request) {
+        String requestURI = request.getRequestURI();
+        log.error("请求地址'{}',角色权限校验失败'{}'", requestURI, e.getMessage());
+        return R.fail(HttpStatus.HTTP_FORBIDDEN, "没有访问权限,请联系管理员授权");
+    }
+
+    /**
+     * 认证失败
+     */
+    @ExceptionHandler(NotLoginException.class)
+    public R<Void> handleNotLoginException(NotLoginException e, HttpServletRequest request) {
+        String requestURI = request.getRequestURI();
+        log.error("请求地址'{}',认证失败'{}',无法访问系统资源", requestURI, e.getMessage());
+        return R.fail(HttpStatus.HTTP_UNAUTHORIZED, "认证失败,无法访问系统资源");
+    }
+
+}

+ 0 - 1
SERVER/VberAdminPlusV3/vber-common/vber-common-security/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

@@ -1,3 +1,2 @@
-com.vber.common.security.handler.GlobalExceptionHandler
 com.vber.common.security.handler.AllUrlHandler
 com.vber.common.security.config.SecurityConfig

+ 10 - 0
SERVER/VberAdminPlusV3/vber-common/vber-common-web/src/main/java/com/vber/common/web/config/ResourcesConfig.java

@@ -1,5 +1,6 @@
 package com.vber.common.web.config;
 
+import com.vber.common.web.handler.GlobalExceptionHandler;
 import com.vber.common.web.interceptor.PlusWebInvokeTimeInterceptor;
 import org.springframework.boot.autoconfigure.AutoConfiguration;
 import org.springframework.context.annotation.Bean;
@@ -49,4 +50,13 @@ public class ResourcesConfig implements WebMvcConfigurer {
         // 返回新的CorsFilter
         return new CorsFilter(source);
     }
+
+
+    /**
+     * 全局异常处理器
+     */
+    @Bean
+    public GlobalExceptionHandler globalExceptionHandler() {
+        return new GlobalExceptionHandler();
+    }
 }

+ 27 - 47
SERVER/VberAdminPlusV3/vber-common/vber-common-security/src/main/java/com/vber/common/security/handler/GlobalExceptionHandler.java → SERVER/VberAdminPlusV3/vber-common/vber-common-web/src/main/java/com/vber/common/web/handler/GlobalExceptionHandler.java

@@ -1,8 +1,5 @@
-package com.vber.common.security.handler;
+package com.vber.common.web.handler;
 
-import cn.dev33.satoken.exception.NotLoginException;
-import cn.dev33.satoken.exception.NotPermissionException;
-import cn.dev33.satoken.exception.NotRoleException;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.http.HttpStatus;
 import com.vber.common.core.domain.R;
@@ -21,55 +18,28 @@ import org.springframework.web.bind.MissingPathVariableException;
 import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.RestControllerAdvice;
 import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
+import org.springframework.web.servlet.NoHandlerFoundException;
+
+import java.util.Objects;
 
 /**
  * 全局异常处理器
  *
- * @author Iwb
+ * @author iwb
  */
 @Slf4j
 @RestControllerAdvice
 public class GlobalExceptionHandler {
 
-    /**
-     * 权限码异常
-     */
-    @ExceptionHandler(NotPermissionException.class)
-    public R<Void> handleNotPermissionException(NotPermissionException e, HttpServletRequest request) {
-        String requestUri = request.getRequestURI();
-        log.error("请求地址'{}',权限码校验失败'{}'", requestUri, e.getMessage());
-        return R.fail(HttpStatus.HTTP_FORBIDDEN, "没有访问权限,请联系管理员授权");
-    }
-
-    /**
-     * 角色权限异常
-     */
-    @ExceptionHandler(NotRoleException.class)
-    public R<Void> handleNotRoleException(NotRoleException e, HttpServletRequest request) {
-        String requestUri = request.getRequestURI();
-        log.error("请求地址'{}',角色权限校验失败'{}'", requestUri, e.getMessage());
-        return R.fail(HttpStatus.HTTP_FORBIDDEN, "没有访问权限,请联系管理员授权");
-    }
-
-    /**
-     * 认证失败
-     */
-    @ExceptionHandler(NotLoginException.class)
-    public R<Void> handleNotLoginException(NotLoginException e, HttpServletRequest request) {
-        String requestUri = request.getRequestURI();
-        log.error("请求地址'{}',认证失败'{}',无法访问系统资源", requestUri, e.getMessage());
-        return R.fail(HttpStatus.HTTP_UNAUTHORIZED, "认证失败,无法访问系统资源");
-    }
-
     /**
      * 请求方式不支持
      */
     @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
     public R<Void> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e,
                                                        HttpServletRequest request) {
-        String requestUri = request.getRequestURI();
-        log.error("请求地址'{}',不支持'{}'请求", requestUri, e.getMethod());
-        return R.fail(e.getMessage());
+        String requestURI = request.getRequestURI();
+        log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod());
+        return R.fail(HttpStatus.HTTP_BAD_METHOD, e.getMessage());
     }
 
     /**
@@ -96,8 +66,8 @@ public class GlobalExceptionHandler {
      */
     @ExceptionHandler(MissingPathVariableException.class)
     public R<Void> handleMissingPathVariableException(MissingPathVariableException e, HttpServletRequest request) {
-        String requestUri = request.getRequestURI();
-        log.error("请求路径中缺少必需的路径变量'{}',发生系统异常.", requestUri);
+        String requestURI = request.getRequestURI();
+        log.error("请求路径中缺少必需的路径变量'{}',发生系统异常.", requestURI);
         return R.fail(String.format("请求路径中缺少必需的路径变量[%s]", e.getVariableName()));
     }
 
@@ -106,18 +76,28 @@ public class GlobalExceptionHandler {
      */
     @ExceptionHandler(MethodArgumentTypeMismatchException.class)
     public R<Void> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e, HttpServletRequest request) {
-        String requestUri = request.getRequestURI();
-        log.error("请求参数类型不匹配'{}',发生系统异常.", requestUri);
+        String requestURI = request.getRequestURI();
+        log.error("请求参数类型不匹配'{}',发生系统异常.", requestURI);
         return R.fail(String.format("请求参数类型不匹配,参数[%s]要求类型为:'%s',但输入值为:'%s'", e.getName(), e.getRequiredType().getName(), e.getValue()));
     }
 
+    /**
+     * 找不到路由
+     */
+    @ExceptionHandler(NoHandlerFoundException.class)
+    public R<Void> handleNoHandlerFoundException(NoHandlerFoundException e, HttpServletRequest request) {
+        String requestURI = request.getRequestURI();
+        log.error("请求地址'{}'不存在.", requestURI);
+        return R.fail(HttpStatus.HTTP_NOT_FOUND, e.getMessage());
+    }
+
     /**
      * 拦截未知的运行时异常
      */
     @ExceptionHandler(RuntimeException.class)
     public R<Void> handleRuntimeException(RuntimeException e, HttpServletRequest request) {
-        String requestUri = request.getRequestURI();
-        log.error("请求地址'{}',发生未知异常.", requestUri, e);
+        String requestURI = request.getRequestURI();
+        log.error("请求地址'{}',发生未知异常.", requestURI, e);
         return R.fail(e.getMessage());
     }
 
@@ -126,8 +106,8 @@ public class GlobalExceptionHandler {
      */
     @ExceptionHandler(Exception.class)
     public R<Void> handleException(Exception e, HttpServletRequest request) {
-        String requestUri = request.getRequestURI();
-        log.error("请求地址'{}',发生系统异常.", requestUri, e);
+        String requestURI = request.getRequestURI();
+        log.error("请求地址'{}',发生系统异常.", requestURI, e);
         return R.fail(e.getMessage());
     }
 
@@ -157,7 +137,7 @@ public class GlobalExceptionHandler {
     @ExceptionHandler(MethodArgumentNotValidException.class)
     public R<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
         log.error(e.getMessage());
-        String message = e.getBindingResult().getFieldError().getDefaultMessage();
+        String message = Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage();
         return R.fail(message);
     }