|
|
@@ -1,5 +1,6 @@
|
|
|
package com.fs.admin.interceptor;
|
|
|
|
|
|
+import com.fs.admin.annotation.TenantDS;
|
|
|
import com.fs.common.config.RedisTenantContext;
|
|
|
import com.fs.common.enums.DataSourceType;
|
|
|
import com.fs.framework.datasource.DynamicDataSourceContextHolder;
|
|
|
@@ -11,11 +12,13 @@ import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.method.HandlerMethod;
|
|
|
import org.springframework.web.servlet.HandlerMapping;
|
|
|
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.lang.reflect.Method;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
@@ -45,17 +48,28 @@ public class AdminTenantDataSourceInterceptor extends HandlerInterceptorAdapter
|
|
|
|
|
|
@Override
|
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
|
|
|
+ if (!(handler instanceof HandlerMethod)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ HandlerMethod handlerMethod = (HandlerMethod) handler;
|
|
|
+ Method method = handlerMethod.getMethod();
|
|
|
+ if (!method.getDeclaringClass().isAnnotationPresent(TenantDS.class)
|
|
|
+ && !method.isAnnotationPresent(TenantDS.class)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
|
|
|
String tenantIdStr = request.getParameter("tenantId");
|
|
|
if (!StringUtils.hasText(tenantIdStr)) {
|
|
|
tenantIdStr = request.getHeader(HEADER_TENANT_ID);
|
|
|
}
|
|
|
if (!StringUtils.hasText(tenantIdStr)) {
|
|
|
- // 获取路由上的 tenantId
|
|
|
Map<String, String> pathVars = (Map<String, String>) request.getAttribute(
|
|
|
HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
|
|
|
if (pathVars != null) {
|
|
|
tenantIdStr = pathVars.get("tenantId");
|
|
|
+ if (!StringUtils.hasText(tenantIdStr)) {
|
|
|
+ tenantIdStr = pathVars.get("companyId");
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
if (!StringUtils.hasText(tenantIdStr)) {
|