yys 3 месяцев назад
Родитель
Сommit
a4cd5f4281

+ 14 - 14
fs-framework/src/main/java/com/fs/framework/task/TenantTaskRunner.java

@@ -1,11 +1,11 @@
 package com.fs.framework.task;
 
 import com.alibaba.fastjson.JSONObject;
+import com.fs.common.config.RedisTenantContext;
 import com.fs.common.enums.DataSourceType;
 import com.fs.common.utils.StringUtils;
 import com.fs.config.saas.ProjectConfig;
 import com.fs.core.config.TenantConfigContext;
-import com.fs.framework.config.TenantPrincipal;
 import com.fs.framework.datasource.DynamicDataSourceContextHolder;
 import com.fs.framework.datasource.TenantDataSourceManager;
 import com.fs.system.domain.SysConfig;
@@ -13,12 +13,9 @@ import com.fs.system.mapper.SysConfigMapper;
 import com.fs.tenant.domain.TenantInfo;
 import com.fs.tenant.service.TenantInfoService;
 import lombok.extern.slf4j.Slf4j;
-import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
-import org.springframework.security.core.context.SecurityContextHolder;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
-import java.util.Collections;
 import java.util.Date;
 import java.util.List;
 import java.util.function.Consumer;
@@ -97,8 +94,14 @@ public class TenantTaskRunner {
     private void runForOneTenant(TenantInfo tenant, String taskName, Consumer<TenantInfo> action) {
         String dataSourceKey = "tenant:" + tenant.getId();
         try {
+            // 切换到租户数据源
             tenantDataSourceManager.switchTenant(tenant);
-            log.info("[SaaS Task] 定时任务切换数据源 dataSource={}, tenantId={}, tenantCode={}, task={}", dataSourceKey, tenant.getId(), tenant.getTenantCode(), taskName != null ? taskName : "");
+            // 切换Redis租户上下文
+            RedisTenantContext.setTenantId(tenant.getId());
+            log.info("[SaaS Task] 定时任务切换数据源和Redis dataSource={}, tenantId={}, tenantCode={}, task={}",
+                    dataSourceKey, tenant.getId(), tenant.getTenantCode(), taskName != null ? taskName : "");
+
+            // 加载租户项目配置
             SysConfig cfg = sysConfigMapper.selectConfigByConfigKey("projectConfig");
             if (cfg != null && StringUtils.isNotBlank(cfg.getConfigValue())) {
                 TenantConfigContext.set(JSONObject.parseObject(cfg.getConfigValue()));
@@ -106,21 +109,18 @@ public class TenantTaskRunner {
                 TenantConfigContext.set(null);
             }
             ProjectConfig.loadTenantConfigsFromContext();
+
+            // 执行租户任务
             action.accept(tenant);
 
-            // 设置租户到 SecurityContext,供 TenantKeyRedisSerializer 自动为 Redis Key 加 tenantid 前缀
-            SecurityContextHolder.getContext().setAuthentication(
-                    new UsernamePasswordAuthenticationToken(
-                            new TenantPrincipal(tenant.getId()),
-                            null,
-                            Collections.emptyList()
-                    )
-            );
         } catch (Exception e) {
-            log.error("[SaaS Task] 租户 tenantId={}, tenantCode={} 执行异常", tenant.getId(), tenant.getTenantCode(), e);
+            log.error("[SaaS Task] 租户 tenantId={}, tenantCode={} 执行异常, task={}",
+                    tenant.getId(), tenant.getTenantCode(), taskName, e);
         } finally {
             ProjectConfig.clearTenantConfigs();
             TenantConfigContext.clear();
+            // 清理Redis租户上下文
+            RedisTenantContext.clear();
             DynamicDataSourceContextHolder.clearDataSourceType();
         }
     }

+ 5 - 12
fs-live-app/src/main/java/com/fs/live/task/TenantTaskRunner.java

@@ -1,11 +1,11 @@
 package com.fs.live.task;
 
 import com.alibaba.fastjson.JSONObject;
+import com.fs.common.config.RedisTenantContext;
 import com.fs.common.enums.DataSourceType;
 import com.fs.common.utils.StringUtils;
 import com.fs.config.saas.ProjectConfig;
 import com.fs.core.config.TenantConfigContext;
-import com.fs.live.config.TenantPrincipal;
 import com.fs.live.datasource.TenantDataSourceManager;
 import com.fs.framework.datasource.DynamicDataSourceContextHolder;
 import com.fs.system.domain.SysConfig;
@@ -13,8 +13,6 @@ import com.fs.system.mapper.SysConfigMapper;
 import com.fs.tenant.domain.TenantInfo;
 import com.fs.tenant.service.TenantInfoService;
 import lombok.extern.slf4j.Slf4j;
-import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
-import org.springframework.security.core.context.SecurityContextHolder;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
@@ -157,14 +155,8 @@ public class TenantTaskRunner {
                 TenantConfigContext.set(null);
             }
 
-            // 设置租户 SecurityContext,让 TenantKeyRedisSerializer 自动拼租户前缀
-            SecurityContextHolder.getContext().setAuthentication(
-                    new UsernamePasswordAuthenticationToken(
-                            new TenantPrincipal(tenant.getId()),
-                            null,
-                            Collections.emptyList()
-                    )
-            );
+            // 设置Redis租户上下文,让 TenantKeyRedisSerializer 自动拼租户前缀
+            RedisTenantContext.setTenantId(tenant.getId());
 
             // 标记当前线程正在租户执行上下文中,防止递归分发
             IN_TENANT_EXECUTION.set(true);
@@ -177,8 +169,9 @@ public class TenantTaskRunner {
             IN_TENANT_EXECUTION.remove();
             ProjectConfig.clearTenantConfigs();
             TenantConfigContext.clear();
+            // 清理Redis租户上下文
+            RedisTenantContext.clear();
             DynamicDataSourceContextHolder.clearDataSourceType();
-            SecurityContextHolder.clearContext();
         }
     }
 

+ 7 - 1
fs-qw-api/src/main/java/com/fs/app/qwTask/TenantTaskRunner.java

@@ -1,6 +1,7 @@
 package com.fs.app.qwTask;
 
 import com.alibaba.fastjson.JSONObject;
+import com.fs.common.config.RedisTenantContext;
 import com.fs.common.enums.DataSourceType;
 import com.fs.common.utils.StringUtils;
 import com.fs.config.saas.ProjectConfig;
@@ -183,8 +184,11 @@ public class TenantTaskRunner {
             // 标记当前线程正在租户执行上下文中
             IN_TENANT_EXECUTION.set(true);
 
+            // 切换到租户数据源
             tenantDataSourceManager.switchTenant(tenant);
-            log.info("[SaaS Task] 定时任务切换数据源 dataSource={}, tenantId={}, tenantCode={}, task={}",
+            // 切换Redis租户上下文
+            RedisTenantContext.setTenantId(tenant.getId());
+            log.info("[SaaS Task] 定时任务切换数据源和Redis dataSource={}, tenantId={}, tenantCode={}, task={}",
                     dataSourceKey, tenant.getId(), tenant.getTenantCode(), taskName != null ? taskName : "");
 
             // 加载租户项目配置
@@ -206,6 +210,8 @@ public class TenantTaskRunner {
             IN_TENANT_EXECUTION.remove();
             ProjectConfig.clearTenantConfigs();
             TenantConfigContext.clear();
+            // 清理Redis租户上下文
+            RedisTenantContext.clear();
             DynamicDataSourceContextHolder.clearDataSourceType();
         }
     }

+ 14 - 15
fs-qw-task/src/main/java/com/fs/app/task/TenantTaskRunner.java

@@ -1,11 +1,11 @@
 package com.fs.app.task;
 
 import com.alibaba.fastjson.JSONObject;
+import com.fs.common.config.RedisTenantContext;
 import com.fs.common.enums.DataSourceType;
 import com.fs.common.utils.StringUtils;
 import com.fs.config.saas.ProjectConfig;
 import com.fs.core.config.TenantConfigContext;
-import com.fs.framework.config.TenantPrincipal;
 import com.fs.framework.datasource.DynamicDataSourceContextHolder;
 import com.fs.framework.datasource.TenantDataSourceManager;
 import com.fs.system.domain.SysConfig;
@@ -14,14 +14,11 @@ import com.fs.tenant.domain.TenantInfo;
 import com.fs.tenant.service.TenantInfoService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Value;
-import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
-import org.springframework.security.core.context.SecurityContextHolder;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
 import javax.annotation.Resource;
-import java.util.Collections;
 import java.util.Date;
 import java.util.List;
 import java.util.concurrent.*;
@@ -174,8 +171,14 @@ public class TenantTaskRunner {
     private void runForOneTenant(TenantInfo tenant, String taskName, Consumer<TenantInfo> action) {
         String dataSourceKey = "tenant:" + tenant.getId();
         try {
+            // 切换到租户数据源
             tenantDataSourceManager.switchTenant(tenant);
-            log.info("[SaaS Task] 定时任务切换数据源 dataSource={}, tenantId={}, tenantCode={}, task={}", dataSourceKey, tenant.getId(), tenant.getTenantCode(), taskName != null ? taskName : "");
+            // 切换Redis租户上下文
+            RedisTenantContext.setTenantId(tenant.getId());
+            log.info("[SaaS Task] 定时任务切换数据源和Redis dataSource={}, tenantId={}, tenantCode={}, task={}",
+                    dataSourceKey, tenant.getId(), tenant.getTenantCode(), taskName != null ? taskName : "");
+
+            // 加载租户项目配置
             SysConfig cfg = sysConfigMapper.selectConfigByConfigKey("projectConfig");
             if (cfg != null && StringUtils.isNotBlank(cfg.getConfigValue())) {
                 TenantConfigContext.set(JSONObject.parseObject(cfg.getConfigValue()));
@@ -183,22 +186,18 @@ public class TenantTaskRunner {
                 TenantConfigContext.set(null);
             }
             ProjectConfig.loadTenantConfigsFromContext();
-            action.accept(tenant);
 
-            // 设置租户到 SecurityContext,供 TenantKeyRedisSerializer 自动为 Redis Key 加 tenantid 前缀
-            SecurityContextHolder.getContext().setAuthentication(
-                    new UsernamePasswordAuthenticationToken(
-                            new TenantPrincipal(tenant.getId()),
-                            null,
-                            Collections.emptyList()
-                    )
-            );
+            // 执行租户任务
+            action.accept(tenant);
 
         } catch (Exception e) {
-            log.error("[SaaS Task] 租户 tenantId={}, tenantCode={} 执行异常", tenant.getId(), tenant.getTenantCode(), e);
+            log.error("[SaaS Task] 租户 tenantId={}, tenantCode={} 执行异常, task={}",
+                    tenant.getId(), tenant.getTenantCode(), taskName, e);
         } finally {
             ProjectConfig.clearTenantConfigs();
             TenantConfigContext.clear();
+            // 清理Redis租户上下文
+            RedisTenantContext.clear();
             DynamicDataSourceContextHolder.clearDataSourceType();
         }
     }