|
|
@@ -0,0 +1,121 @@
|
|
|
+package com.fs.framework.filter;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+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.datasource.DynamicDataSourceContextHolder;
|
|
|
+import com.fs.framework.datasource.TenantDataSourceManager;
|
|
|
+import com.fs.framework.security.TenantPrincipal;
|
|
|
+import com.fs.system.domain.SysConfig;
|
|
|
+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.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.core.annotation.Order;
|
|
|
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
+import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.filter.OncePerRequestFilter;
|
|
|
+
|
|
|
+import javax.servlet.FilterChain;
|
|
|
+import javax.servlet.ServletException;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Collections;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+@Order(5) // 在业务处理前尽早完成数据源和租户配置的上下文初始化
|
|
|
+public class AppTenantSwitchFilter extends OncePerRequestFilter {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 租户编码请求头名称。
|
|
|
+ * 前端需在每次请求中携带该头,才能启用多租户能力。
|
|
|
+ */
|
|
|
+ public static final String HEADER_TENANT_CODE = "X-Qw-CorpId";
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private TenantInfoService tenantInfoService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private TenantDataSourceManager tenantDataSourceManager;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SysConfigMapper sysConfigMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void doFilterInternal(HttpServletRequest request,
|
|
|
+ HttpServletResponse response,
|
|
|
+ FilterChain filterChain) throws ServletException, IOException {
|
|
|
+ String corpId = request.getHeader(HEADER_TENANT_CODE);
|
|
|
+
|
|
|
+ try {
|
|
|
+ if (StringUtils.isBlank(corpId)) {
|
|
|
+ SysConfig cfg = sysConfigMapper.selectConfigByConfigKey("projectConfig");
|
|
|
+ if (cfg != null && StringUtils.isNotBlank(cfg.getConfigValue())) {
|
|
|
+ TenantConfigContext.set(JSONObject.parseObject(cfg.getConfigValue()));
|
|
|
+ } else {
|
|
|
+ TenantConfigContext.set(null);
|
|
|
+ }
|
|
|
+ ProjectConfig.loadTenantConfigsFromContext();
|
|
|
+
|
|
|
+ // 未携带企微编号:保持单库行为,直接放行
|
|
|
+ filterChain.doFilter(request, response);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Step 1:切到主库,根据 企微编号 查询租户信息
|
|
|
+ DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.MASTER.name());
|
|
|
+
|
|
|
+ TenantInfo tenantInfo = tenantInfoService.getTenByCorpId(corpId);
|
|
|
+ if (tenantInfo == null) {
|
|
|
+ log.warn("[SaaS qw-hook] 请求携带的 corpId={} 未找到租户记录,直接终止请求。", corpId);
|
|
|
+
|
|
|
+ // 方案1:返回401/403未授权(推荐,接口场景)
|
|
|
+ response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); // 401
|
|
|
+
|
|
|
+ // 可选:返回JSON提示(前后端分离接口必加)
|
|
|
+ response.setContentType("application/json;charset=UTF-8");
|
|
|
+ response.getWriter().write("{\"code\":401,\"msg\":\"未找到对应租户,请求拒绝\"}");
|
|
|
+
|
|
|
+ return; // 直接退出,不执行任何后续逻辑
|
|
|
+ }
|
|
|
+
|
|
|
+ // Step 2:切换到租户库(如无则动态创建数据源)
|
|
|
+ tenantDataSourceManager.switchTenant(tenantInfo);
|
|
|
+
|
|
|
+ // Step 3:从租户库读取项目配置(projectConfig),填充 TenantConfigContext 和 ProjectConfig
|
|
|
+ SysConfig cfg = sysConfigMapper.selectConfigByConfigKey("projectConfig");
|
|
|
+ if (cfg != null && StringUtils.isNotBlank(cfg.getConfigValue())) {
|
|
|
+ TenantConfigContext.set(JSONObject.parseObject(cfg.getConfigValue()));
|
|
|
+ } else {
|
|
|
+ TenantConfigContext.set(null);
|
|
|
+ }
|
|
|
+ ProjectConfig.loadTenantConfigsFromContext();
|
|
|
+
|
|
|
+ // 设置租户到 SecurityContext,供 TenantKeyRedisSerializer 自动为 Redis Key 加 tenantid 前缀
|
|
|
+ SecurityContextHolder.getContext().setAuthentication(
|
|
|
+ new UsernamePasswordAuthenticationToken(
|
|
|
+ new TenantPrincipal(tenantInfo.getId()),
|
|
|
+ null,
|
|
|
+ Collections.emptyList()));
|
|
|
+
|
|
|
+ // Step 4:继续后续过滤器链和业务处理(此时 Mapper 操作的就是当前租户库)
|
|
|
+ filterChain.doFilter(request, response);
|
|
|
+ } finally {
|
|
|
+ // 确保请求结束后清理租户上下文和数据源标记,避免线程复用时串库
|
|
|
+ try {
|
|
|
+ ProjectConfig.clearTenantConfigs();
|
|
|
+ } catch (Exception ignored) {
|
|
|
+ }
|
|
|
+ TenantConfigContext.clear();
|
|
|
+ tenantDataSourceManager.clear();
|
|
|
+ SecurityContextHolder.clearContext();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|