数据分离-未登录状态兼容

This commit is contained in:
attiya 2025-08-29 10:07:29 +08:00
parent 5c1bcfec16
commit 924662c500
2 changed files with 12 additions and 9 deletions

View File

@ -9,7 +9,10 @@ public class MyTenantFactory implements TenantFactory {
public Object[] getTenantIds() { public Object[] getTenantIds() {
RequestAttributes attributes = RequestContextHolder.getRequestAttributes(); RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
Long tenantId = (Long) attributes.getAttribute("tenantId", RequestAttributes.SCOPE_REQUEST); Long tenantId = (Long) attributes.getAttribute("tenantId", RequestAttributes.SCOPE_REQUEST);
if (tenantId != null) {
return new Object[]{tenantId}; return new Object[]{tenantId};
} else {
return new Object[]{};
}
} }
} }

View File

@ -6,7 +6,6 @@ import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.HandlerInterceptor;
import java.util.List;
public class TenantInterceptor implements HandlerInterceptor { public class TenantInterceptor implements HandlerInterceptor {
@ -15,11 +14,12 @@ public class TenantInterceptor implements HandlerInterceptor {
, HttpServletResponse response, Object handler) throws Exception { , HttpServletResponse response, Object handler) throws Exception {
//通过 request 去获取租户 ID //通过 request 去获取租户 ID
if (StpUtil.isLogin()) {
Long tenantId = getTenantIdByReuqest(request); Long tenantId = getTenantIdByReuqest(request);
//设置租户ID到 request attribute //设置租户ID到 request attribute
request.setAttribute("tenantId", tenantId); request.setAttribute("tenantId", tenantId);
return true;
}
return true; return true;
} }