租户模式-数据隔离
This commit is contained in:
parent
67ef5d181a
commit
9754a57679
@ -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};
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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();
|
||||||
|
// }
|
||||||
|
}
|
||||||
@ -1,12 +1,15 @@
|
|||||||
package com.cdzy.user.config;
|
package com.cdzy.user.config;
|
||||||
|
|
||||||
|
import com.cdzy.user.component.MyTenantFactory;
|
||||||
import com.mybatisflex.annotation.KeyType;
|
import com.mybatisflex.annotation.KeyType;
|
||||||
import com.mybatisflex.core.FlexGlobalConfig;
|
import com.mybatisflex.core.FlexGlobalConfig;
|
||||||
import com.mybatisflex.core.audit.AuditManager;
|
import com.mybatisflex.core.audit.AuditManager;
|
||||||
import com.mybatisflex.core.dialect.DbType;
|
import com.mybatisflex.core.dialect.DbType;
|
||||||
import com.mybatisflex.core.dialect.DialectFactory;
|
import com.mybatisflex.core.dialect.DialectFactory;
|
||||||
|
import com.mybatisflex.core.tenant.TenantFactory;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
23
ebike-user/src/main/java/com/cdzy/user/config/WebConfig.java
Normal file
23
ebike-user/src/main/java/com/cdzy/user/config/WebConfig.java
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -37,6 +37,7 @@ public class Staff implements Serializable {
|
|||||||
|
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
|
@Column(tenantId = true)
|
||||||
private Long orgId;
|
private Long orgId;
|
||||||
|
|
||||||
@Column(onInsertValue = "0")
|
@Column(onInsertValue = "0")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user