diff --git a/iot-common/iot-common-satoken/pom.xml b/iot-common/iot-common-satoken/pom.xml index 22600dc9..7059acc1 100644 --- a/iot-common/iot-common-satoken/pom.xml +++ b/iot-common/iot-common-satoken/pom.xml @@ -39,7 +39,11 @@ cc.iotkit iot-common-redis - + + + cc.iotkit + iot-common-tenant + cc.iotkit iot-common-web diff --git a/iot-common/iot-common-satoken/src/main/java/cc/iotkit/common/satoken/config/SaTokenConfig.java b/iot-common/iot-common-satoken/src/main/java/cc/iotkit/common/satoken/config/SaTokenConfig.java index 1482b56e..0fd86b86 100644 --- a/iot-common/iot-common-satoken/src/main/java/cc/iotkit/common/satoken/config/SaTokenConfig.java +++ b/iot-common/iot-common-satoken/src/main/java/cc/iotkit/common/satoken/config/SaTokenConfig.java @@ -2,12 +2,15 @@ package cc.iotkit.common.satoken.config; import cc.iotkit.common.satoken.core.dao.PlusSaTokenDao; 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.jwt.StpLogicJwtForSimple; import cn.dev33.satoken.stp.StpInterface; import cn.dev33.satoken.stp.StpLogic; +import lombok.RequiredArgsConstructor; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.context.annotation.Bean; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** @@ -15,8 +18,12 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; * * @author Lion Li */ + +@RequiredArgsConstructor @AutoConfiguration public class SaTokenConfig implements WebMvcConfigurer { + + private final TenantInterceptor tenantInterceptor; @Bean public StpLogic getStpLogicJwt() { @@ -40,4 +47,10 @@ public class SaTokenConfig implements WebMvcConfigurer { return new PlusSaTokenDao(); } + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addWebRequestInterceptor(tenantInterceptor); + } + + } diff --git a/iot-common/iot-common-tenant/pom.xml b/iot-common/iot-common-tenant/pom.xml index a1b98af1..81715368 100644 --- a/iot-common/iot-common-tenant/pom.xml +++ b/iot-common/iot-common-tenant/pom.xml @@ -27,6 +27,10 @@ cc.iotkit iot-common-satoken + + org.springframework.boot + spring-boot-starter-data-jpa + @@ -39,6 +43,7 @@ ${java.version} ${java.version} + diff --git a/iot-common/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/dao/entiry/TenantAware.java b/iot-common/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/dao/entiry/TenantAware.java new file mode 100644 index 00000000..72e1ac18 --- /dev/null +++ b/iot-common/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/dao/entiry/TenantAware.java @@ -0,0 +1,5 @@ +package cc.iotkit.common.tenant.dao.entiry; + +public interface TenantAware { + void setTenantId(String tenantId); +} diff --git a/iot-common/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/interceptor/TenantInterceptor.java b/iot-common/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/interceptor/TenantInterceptor.java new file mode 100644 index 00000000..78068c5a --- /dev/null +++ b/iot-common/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/interceptor/TenantInterceptor.java @@ -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) { + + } +} diff --git a/iot-common/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/listener/TenantListener.java b/iot-common/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/listener/TenantListener.java new file mode 100644 index 00000000..c7b74729 --- /dev/null +++ b/iot-common/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/listener/TenantListener.java @@ -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); + } +} diff --git a/iot-common/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/util/TenantContext.java b/iot-common/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/util/TenantContext.java new file mode 100644 index 00000000..79aaa68c --- /dev/null +++ b/iot-common/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/util/TenantContext.java @@ -0,0 +1,21 @@ +package cc.iotkit.common.tenant.util; + +public class TenantContext { + private TenantContext() { + } + + private static final ThreadLocal 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(); + } + +}