mirror of
https://github.com/jeecgboot/JeecgBoot.git
synced 2026-03-18 03:33:31 +00:00
--author:scott--date:20260310--for:issues/9388 新增多租户上下文过滤器,支持从请求头获取租户ID并设置到TenantContext
This commit is contained in:
parent
5a2822a4fb
commit
5956264d63
@ -0,0 +1,39 @@
|
|||||||
|
package org.jeecg.config.security;
|
||||||
|
|
||||||
|
import jakarta.servlet.FilterChain;
|
||||||
|
import jakarta.servlet.ServletException;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.jeecg.common.config.TenantContext;
|
||||||
|
import org.jeecg.common.constant.CommonConstant;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 多租户上下文过滤器
|
||||||
|
* 从请求头中获取租户ID并设置到TenantContext中
|
||||||
|
* 对应shiro版本中JwtFilter里的TenantContext.setTenant逻辑
|
||||||
|
*
|
||||||
|
* @author jeecg-boot
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class TenantContextFilter extends OncePerRequestFilter {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
|
||||||
|
throws ServletException, IOException {
|
||||||
|
// 从请求头获取租户ID并设置到TenantContext(多租户用到)
|
||||||
|
String tenantId = request.getHeader(CommonConstant.TENANT_ID);
|
||||||
|
TenantContext.setTenant(tenantId);
|
||||||
|
try {
|
||||||
|
filterChain.doFilter(request, response);
|
||||||
|
} finally {
|
||||||
|
// 请求结束后清除,防止线程复用导致租户信息污染
|
||||||
|
TenantContext.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user