租户模式-数据隔离

This commit is contained in:
attiya 2025-08-22 16:57:41 +08:00
parent 67ef5d181a
commit 9754a57679
5 changed files with 81 additions and 0 deletions

View File

@ -0,0 +1,15 @@
package com.cdzy.user.component;
import com.mybatisflex.core.tenant.TenantFactory;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
public class MyTenantFactory implements TenantFactory {
public Object[] getTenantIds(){
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
Long tenantId = (Long) attributes.getAttribute("tenantId", RequestAttributes.SCOPE_REQUEST);
return new Object[]{tenantId};
}
}

View File

@ -0,0 +1,39 @@
package com.cdzy.user.component;
import cn.dev33.satoken.stp.StpUtil;
import com.cdzy.common.model.StaffDto;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import java.util.List;
public class TenantInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request
, HttpServletResponse response, Object handler) throws Exception {
//通过 request 去获取租户 ID
Long tenantId = getTenantIdByReuqest(request);
//设置租户ID到 request attribute
request.setAttribute("tenantId", tenantId);
return true;
}
Long getTenantIdByReuqest(HttpServletRequest request) {
String token = request.getHeader("Authorization");
String id = (String) StpUtil.getLoginIdByToken(token);
StaffDto staffDto = (StaffDto) StpUtil.getSessionByLoginId(id).get(id);
return staffDto.getOrgId();
}
// List<Long> getAllTenantIdByReuqest(HttpServletRequest request) {
// String token = request.getHeader("Authorization");
// String id = (String) StpUtil.getLoginIdByToken(token);
// StaffDto staffDto = (StaffDto) StpUtil.getSessionByLoginId(id).get(id);
// return staffDto.getOrgId();
// }
}

View File

@ -1,12 +1,15 @@
package com.cdzy.user.config;
import com.cdzy.user.component.MyTenantFactory;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.audit.AuditManager;
import com.mybatisflex.core.dialect.DbType;
import com.mybatisflex.core.dialect.DialectFactory;
import com.mybatisflex.core.tenant.TenantFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**

View File

@ -0,0 +1,23 @@
package com.cdzy.user.config;
import com.cdzy.user.component.MyTenantFactory;
import com.cdzy.user.component.TenantInterceptor;
import com.mybatisflex.core.tenant.TenantFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Bean
public TenantFactory tenantFactory(){
return new MyTenantFactory();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new TenantInterceptor());
}
}

View File

@ -37,6 +37,7 @@ public class Staff implements Serializable {
private String password;
@Column(tenantId = true)
private Long orgId;
@Column(onInsertValue = "0")