feat: 引入jpa多租户

初步引入jpa多租户
V0.5.x
晨辉 2023-07-13 22:54:35 +08:00
parent d5d606b0d3
commit c149282006
7 changed files with 101 additions and 1 deletions

View File

@ -40,6 +40,10 @@
<artifactId>iot-common-redis</artifactId> <artifactId>iot-common-redis</artifactId>
</dependency> </dependency>
<dependency>
<groupId>cc.iotkit</groupId>
<artifactId>iot-common-tenant</artifactId>
</dependency>
<dependency> <dependency>
<groupId>cc.iotkit</groupId> <groupId>cc.iotkit</groupId>
<artifactId>iot-common-web</artifactId> <artifactId>iot-common-web</artifactId>

View File

@ -2,12 +2,15 @@ package cc.iotkit.common.satoken.config;
import cc.iotkit.common.satoken.core.dao.PlusSaTokenDao; import cc.iotkit.common.satoken.core.dao.PlusSaTokenDao;
import cc.iotkit.common.satoken.core.service.SaPermissionImpl; import cc.iotkit.common.satoken.core.service.SaPermissionImpl;
import cc.iotkit.common.tenant.interceptor.TenantInterceptor;
import cn.dev33.satoken.dao.SaTokenDao; import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.jwt.StpLogicJwtForSimple; import cn.dev33.satoken.jwt.StpLogicJwtForSimple;
import cn.dev33.satoken.stp.StpInterface; import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpLogic; import cn.dev33.satoken.stp.StpLogic;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/** /**
@ -15,9 +18,13 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
* *
* @author Lion Li * @author Lion Li
*/ */
@RequiredArgsConstructor
@AutoConfiguration @AutoConfiguration
public class SaTokenConfig implements WebMvcConfigurer { public class SaTokenConfig implements WebMvcConfigurer {
private final TenantInterceptor tenantInterceptor;
@Bean @Bean
public StpLogic getStpLogicJwt() { public StpLogic getStpLogicJwt() {
// Sa-Token 整合 jwt (简单模式) // Sa-Token 整合 jwt (简单模式)
@ -40,4 +47,10 @@ public class SaTokenConfig implements WebMvcConfigurer {
return new PlusSaTokenDao(); return new PlusSaTokenDao();
} }
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addWebRequestInterceptor(tenantInterceptor);
}
} }

View File

@ -27,6 +27,10 @@
<groupId>cc.iotkit</groupId> <groupId>cc.iotkit</groupId>
<artifactId>iot-common-satoken</artifactId> <artifactId>iot-common-satoken</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies> </dependencies>
@ -39,6 +43,7 @@
<source>${java.version}</source> <source>${java.version}</source>
<target>${java.version}</target> <target>${java.version}</target>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>

View File

@ -0,0 +1,5 @@
package cc.iotkit.common.tenant.dao.entiry;
public interface TenantAware {
void setTenantId(String tenantId);
}

View File

@ -0,0 +1,34 @@
package cc.iotkit.common.tenant.interceptor;
import cc.iotkit.common.tenant.util.TenantContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.request.WebRequestInterceptor;
@Component
@Slf4j
public class TenantInterceptor implements WebRequestInterceptor {
public static final String TENANT_ID = "TENANT-ID";
@Override
public void preHandle(WebRequest request) {
if (request.getHeader(TENANT_ID) != null) {
String tenantId = request.getHeader(TENANT_ID);
TenantContext.setTenantId(tenantId);
}
}
@Override
public void postHandle(WebRequest request, ModelMap model) {
TenantContext.clear();
}
@Override
public void afterCompletion(WebRequest request, Exception ex) {
}
}

View File

@ -0,0 +1,18 @@
package cc.iotkit.common.tenant.listener;
import cc.iotkit.common.tenant.dao.entiry.TenantAware;
import cc.iotkit.common.tenant.util.TenantContext;
import javax.persistence.PrePersist;
import javax.persistence.PreRemove;
import javax.persistence.PreUpdate;
public class TenantListener {
@PreUpdate
@PreRemove
@PrePersist
public void setTenant(TenantAware entity) {
final String tenantId = TenantContext.getTenantId();
entity.setTenantId(tenantId);
}
}

View File

@ -0,0 +1,21 @@
package cc.iotkit.common.tenant.util;
public class TenantContext {
private TenantContext() {
}
private static final ThreadLocal<String> tenantInfoHolder = new InheritableThreadLocal<>();
public static void setTenantId(String tenantId) {
tenantInfoHolder.set(tenantId);
}
public static String getTenantId() {
return tenantInfoHolder.get();
}
public static void clear() {
tenantInfoHolder.remove();
}
}