Browse Source

fix:全局拦截异常,定义状态码602为解析数据源信息异常返前端重新登录

韩帛霖 1 year ago
parent
commit
332698cce6

+ 10 - 19
ruoyi-admin/src/main/java/com/ruoyi/web/controller/dragForm/CommonController.java

@@ -1,29 +1,18 @@
 package com.ruoyi.web.controller.dragForm;
 
-import com.ruoyi.common.annotation.Anonymous;
-import com.ruoyi.common.config.RuoYiConfig;
 import com.ruoyi.common.core.controller.BaseController;
 import com.ruoyi.common.core.domain.AjaxResult;
-import com.ruoyi.common.core.domain.entity.SysUser;
-import com.ruoyi.common.core.domain.model.LoginUser;
 import com.ruoyi.common.core.page.TableDataInfo;
-import com.ruoyi.common.utils.SecurityUtils;
-import com.ruoyi.common.utils.file.FileUploadUtils;
-import com.ruoyi.common.utils.file.FileUtils;
+import com.ruoyi.common.exception.tenantdatassource.TenantDataSource;
 import com.ruoyi.system.entity.CommonEntity;
 import com.ruoyi.system.entity.TableSql;
 import com.ruoyi.system.service.ICommonService;
 import com.ruoyi.system.service.IDragTableService;
 import com.ruoyi.system.service.ITableSqlService;
-import dm.jdbc.filter.stat.util.JSONUtils;
 import org.springframework.web.bind.annotation.*;
-import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletResponse;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
 
 @RestController
 @RequestMapping("/dragform/common")
@@ -42,7 +31,7 @@ public class CommonController extends BaseController {
      * 共通查询
      */
     @GetMapping("/selectList")
-    public TableDataInfo selectList(CommonEntity commonEntity) throws Exception {
+    public TableDataInfo selectList(CommonEntity commonEntity) {
         startPage();
         return getDataTable(commonService.selectList(commonEntity));
     }
@@ -51,7 +40,7 @@ public class CommonController extends BaseController {
      * 共通批量新增
      */
     @PostMapping("/batchInsert")
-    public AjaxResult batchInsert(@RequestBody CommonEntity commonEntity) throws Exception {
+    public AjaxResult batchInsert(@RequestBody CommonEntity commonEntity) {
         return toAjax(commonService.batchInsert(commonEntity));
     }
 
@@ -59,7 +48,7 @@ public class CommonController extends BaseController {
      * 共通修改sql
      */
     @PutMapping("/batchEdit")
-    public AjaxResult edit(@RequestBody CommonEntity commonEntity) throws Exception {
+    public AjaxResult edit(@RequestBody CommonEntity commonEntity) {
         return toAjax(commonService.edit(commonEntity));
     }
 
@@ -67,7 +56,7 @@ public class CommonController extends BaseController {
      * 批量删除
      */
     @DeleteMapping("/batchDelete")
-    public AjaxResult batchDelete(@RequestBody CommonEntity commonEntity) throws Exception {
+    public AjaxResult batchDelete(@RequestBody CommonEntity commonEntity) {
         return toAjax(commonService.batchDelete(commonEntity));
     }
 
@@ -78,10 +67,12 @@ public class CommonController extends BaseController {
      * @return
      */
     @GetMapping("/getTableList")
-    public TableDataInfo queryTableList(CommonEntity commonEntity)  {
+    public TableDataInfo queryTableList(CommonEntity commonEntity) throws TenantDataSource {
+
         TableSql tableSql = iTableSqlService.selectTableSqlByTSqlKey(commonEntity.getQueryMap().get("sqlkey").toString());
         startPage();  // 校验是否sqlserver 否执行 是执行另一种方式的分页
         return getDataTable(commonService.queryTableList(commonEntity, tableSql));
+
     }
 
     /**
@@ -108,7 +99,7 @@ public class CommonController extends BaseController {
      * 通用动态表单详情
      */
     @GetMapping("/dragTableInfo")
-    public AjaxResult dragTableInfo(CommonEntity commonEntity) throws Exception {
+    public AjaxResult dragTableInfo(CommonEntity commonEntity) {
         return AjaxResult.success(dragTableService.dragTableInfo(commonEntity.getQueryMap().get("sqlkey").toString()));
     }
 
@@ -116,7 +107,7 @@ public class CommonController extends BaseController {
      * 动态表单预览接口
      */
     @GetMapping("/dragTablePreview")
-    public TableDataInfo DragTablePreview(CommonEntity commonEntity) throws Exception {
+    public TableDataInfo DragTablePreview(CommonEntity commonEntity) {
         startPage();
         return getDataTable(commonService.dragTablePreview(commonEntity));
     }

+ 1 - 3
ruoyi-common/src/main/java/com/ruoyi/common/utils/SecurityUtils.java

@@ -54,9 +54,7 @@ public class SecurityUtils {
         try {
             return getLoginUser().getUser().getTenant().getDataSource().getDatabaseType();
         } catch (Exception e) {
-            return "error";
-            //            System.out.println("获取获取数据源类型异常:" + HttpStatus.ERRORWARN);
-//            throw new TenantDataSource("获取获取数据源类型异常", HttpStatus.ERRORWARN);
+            throw new TenantDataSource("为解析到当前请求中数据源信息", HttpStatus.ERRORWARN);
         }
     }
 

+ 14 - 10
ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/SqlInterceptor.java

@@ -34,7 +34,7 @@ public class SqlInterceptor implements Interceptor {
         String modifiedSql = BeforeSQL(sql);
         if (modifiedSql.equals("error")) {
             // 终止程序
-            throw new TenantDataSource("租户异常");
+            return AjaxResult.error("当前用户没有数据源信息!");
         } else {
             // 将修改后的SQL语句设置回StatementHandler
             ReflectUtils.setFieldValue(statementHandler.getBoundSql(), "sql", modifiedSql);
@@ -47,13 +47,6 @@ public class SqlInterceptor implements Interceptor {
     // 根据类型设置不同的选择数据源格式
     private String modifySql(String sql) {
         try {
-            /**
-             * 如何当前请求中并不携带该用户的数据源信息
-             * 分两种情况:
-             * 1.当前用户并没有数据源信息(例如:admin并没有从库数据源信息)
-             * 2.当前用户的token失效(理论上在客户端发送请求时会携带token,逻辑代码执行前会首先验证token的有效性)
-             * 几乎不会出现第二种情况
-             */
             SecurityUtils.getDatabaseType();
         } catch (Exception e) {
             return "error";
@@ -69,14 +62,25 @@ public class SqlInterceptor implements Interceptor {
 
     // 根据类型设置不同的选择数据源格式
     private String BeforeSQL(String sql) {
+        try {
+            SecurityUtils.getDatabaseType();
+        } catch (Exception e) {
+            /**
+             * 如何当前请求中并不携带该用户的数据源信息
+             * 分两种情况:
+             * 1.当前用户并没有数据源信息(例如:admin并没有从库数据源信息)
+             * 2.当前用户的token失效(理论上在客户端发送请求时会携带token,逻辑代码执行前会首先验证token的有效性)
+             * 几乎不会出现第二种情况
+             */
+            return "error";
+        }
         if (SecurityUtils.getDatabaseType().equals("sqlserver"))
             return "USE `" + SecurityUtils.getDatabaseName() + "`; ";
         if (SecurityUtils.getDatabaseType().equals("dm"))
             return "set schema " + SecurityUtils.getDatabaseName() + "; ";
         if (SecurityUtils.getDatabaseType().equals("mysql"))
             return "USE `" + SecurityUtils.getDatabaseName() + "`; ";
-        // 如果当前请求携带信息异常,返回前端提示重新登录。
-        return SecurityUtils.getDatabaseType();
+        return "error";
     }
 
 

+ 30 - 0
ruoyi-framework/src/main/java/com/ruoyi/framework/web/exception/GlobalExceptionAspect.java

@@ -0,0 +1,30 @@
+package com.ruoyi.framework.web.exception;
+
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Component;
+
+/**
+ * 拦截实现类中的异常信息
+ *
+ * @author hzh
+ * @date 2023-09-21
+ */
+@Aspect
+@Component
+public class GlobalExceptionAspect {
+    // com.ruoyi.system.service.impl
+    @Around("execution(* com.ruoyi.system..*(..))") // 切入点表达式
+    public Object handleExceptionALL(ProceedingJoinPoint joinPoint) throws Throwable {
+        try {
+            return joinPoint.proceed(); // 继续执行被拦截的方法  
+        } catch (Exception e) {
+            // 处理异常
+            System.err.println(e);
+            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
+        }
+    }
+}

+ 10 - 3
ruoyi-framework/src/main/java/com/ruoyi/framework/web/exception/GlobalExceptionHandler.java

@@ -2,6 +2,7 @@ package com.ruoyi.framework.web.exception;
 
 import javax.servlet.http.HttpServletRequest;
 
+import com.ruoyi.common.exception.tenantdatassource.TenantDataSource;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.security.access.AccessDeniedException;
@@ -16,7 +17,6 @@ import com.ruoyi.common.exception.DemoModeException;
 import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.utils.StringUtils;
 
-
 /**
  * 全局异常处理器
  *
@@ -110,7 +110,14 @@ public class GlobalExceptionHandler {
      */
     @ExceptionHandler(ClassCastException.class)
     public AjaxResult handleTenantDateSource(ClassCastException t) {
-        System.err.println("雀巢");
-        return AjaxResult.error("602");
+        return AjaxResult.error(602, "解析租户数据源异常");
+    }
+
+    /**
+     * 租户用户请求接口携带信息异常
+     */
+    @ExceptionHandler(TenantDataSource.class)
+    public AjaxResult handleTenantDateSource(TenantDataSource t) {
+        return AjaxResult.error(602, "解析租户数据源异常");
     }
 }