feat: 引入jpa多租户

初步引入jpa多租户
V0.5.x
tiger 2023-07-15 13:12:33 +08:00
parent 130581bc0c
commit 8c887eb350
9 changed files with 30 additions and 167 deletions

View File

@ -9,25 +9,30 @@
*/
package cc.iotkit.data.model;
import cc.iotkit.common.tenant.dao.AbstractBaseEntity;
import cc.iotkit.common.tenant.dao.TenantAware;
import cc.iotkit.common.tenant.listener.TenantListener;
import cc.iotkit.model.product.Product;
import io.github.linpeilie.annotations.AutoMapper;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.ParamDef;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.*;
import javax.validation.constraints.Size;
@Data
@Entity
@ApiModel(value = "产品")
@Table(name = "product")
@AutoMapper(target = Product.class)
public class TbProduct extends AbstractBaseEntity {
@FilterDef(name = "tenantFilter", parameters = {@ParamDef(name = "tenantId", type = "string")})
@Filter(name = "tenantFilter", condition = "tenant_id = :tenantId")
@EntityListeners(TenantListener.class)
public class TbProduct implements TenantAware {
@Id
@GeneratedValue(generator = "SnowflakeIdGenerator")
@ -35,6 +40,10 @@ public class TbProduct extends AbstractBaseEntity {
@ApiModelProperty(value = "产品id")
private Long id;
@Size(max = 30)
@Column(name = "tenant_id")
private String tenantId;
@ApiModelProperty(value = "产品key")
private String productKey;

View File

@ -27,6 +27,10 @@
<groupId>cc.iotkit</groupId>
<artifactId>iot-common-satoken</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>

View File

@ -1,45 +0,0 @@
package cc.iotkit.common.tenant.Config;
import cc.iotkit.common.tenant.util.TenantContext;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.request.WebRequestInterceptor;
/**
* ...
*
* @author Tiger Chen
* created on 2023/7/14 21:51
*/
@Component
public class TenantConfig implements WebRequestInterceptor {
public static final String TENANT_ID = "tenantId";
@Override
public void preHandle(WebRequest request) {
System.out.println("dsdfsd");
if (request.getHeader(TENANT_ID) != null) {
String tenantId = request.getHeader(TENANT_ID);
TenantContext.setTenantId(tenantId);
}
}
@Override
public void postHandle(@NonNull WebRequest request, ModelMap model) {
TenantContext.clear();
}
@Override
public void afterCompletion(@NonNull WebRequest request, Exception ex) {
}
}

View File

@ -1,7 +1,7 @@
package cc.iotkit.common.tenant.aspect;
import cc.iotkit.common.tenant.util.TenantContext;
import cc.iotkit.common.satoken.utils.LoginHelper;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@ -24,7 +24,7 @@ public class TenantFilterAspect {
@AfterReturning(pointcut = "openSession()", returning = "session")
public void afterOpenSession(Object session) {
if (session != null && Session.class.isInstance(session)) {
final String tenantId = TenantContext.getTenantId();
String tenantId = LoginHelper.getTenantId();
if (tenantId != null) {
org.hibernate.Filter filter = ((Session) session).enableFilter("tenantFilter");
filter.setParameter("tenantId", tenantId);

View File

@ -1,44 +0,0 @@
package cc.iotkit.common.tenant.dao;
import cc.iotkit.common.tenant.listener.TenantListener;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.ParamDef;
import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import javax.validation.constraints.Size;
import java.io.Serializable;
/**
* ...
*
* @author Tiger Chen
* created on 2023/7/14 20:47
*/
@MappedSuperclass
@Getter
@Setter
@NoArgsConstructor
@FilterDef(name = "tenantFilter", parameters = {@ParamDef(name = "tenantId", type = "string")})
@Filter(name = "tenantFilter", condition = "tenant_id = :tenantId")
@EntityListeners(TenantListener.class)
public abstract class AbstractBaseEntity implements TenantAware, Serializable {
private static final long serialVersionUID = 1L;
@Size(max = 64)
@Column(name = "tenant_id")
private String tenantId;
public AbstractBaseEntity(String tenantId) {
this.tenantId = tenantId;
}
}

View File

@ -1,41 +0,0 @@
package cc.iotkit.common.tenant.interceptor;
import cc.iotkit.common.tenant.util.TenantContext;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.request.WebRequestInterceptor;
/**
* ...
*
* @author Tiger Chen
* created on 2023/7/14 20:51
*/
@Component
public class TenantInterceptor implements WebRequestInterceptor {
public static final String TENANT_ID = "tenantId";
@Override
public void preHandle(WebRequest request) {
System.out.println("TenantInterceptor: preHandle");
if (request.getHeader(TENANT_ID) != null) {
String tenantId = request.getHeader(TENANT_ID);
TenantContext.setTenantId(tenantId);
}
}
@Override
public void postHandle(@NonNull WebRequest request, ModelMap model) {
TenantContext.clear();
}
@Override
public void afterCompletion(@NonNull WebRequest request, Exception ex) {
}
}

View File

@ -1,27 +0,0 @@
package cc.iotkit.common.tenant.util;
/**
* ...
*
* @author Tiger Chen
* created on 2023/7/14 20:49
*/
public class TenantContext {
private TenantContext() {
}
private static final InheritableThreadLocal<String> currentTenant = new InheritableThreadLocal<>();
public static void setTenantId(String tenantId) {
currentTenant.set(tenantId);
}
public static String getTenantId() {
return currentTenant.get();
}
public static void clear() {
currentTenant.remove();
}
}

View File

@ -4,6 +4,7 @@ import cc.iotkit.common.web.enums.CaptchaCategory;
import cc.iotkit.common.web.enums.CaptchaType;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
*
@ -12,6 +13,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
*/
@Data
@ConfigurationProperties(prefix = "captcha")
@Component
public class CaptchaProperties {
private Boolean enable;

View File

@ -206,10 +206,15 @@
<dependency>
<groupId>cc.iotkit</groupId>
<artifactId>iot-data-serviceImpl-cache</artifactId>
<artifactId>iot-data-serviceImpl-cache</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>5.8.3</version>
<scope>compile</scope>
</dependency>
</dependencies>