我正在尝试使用Spring Boot和Spring Data实现基于鉴别器的多租户。
我创建了一个抽象类来表示多租户实体,类似于以下内容:
@MappedSuperclass
@FilterDefs({@FilterDef(name = "multi-tenant", parameters = {@ParamDef(name = "tenant", type = "string")})})
@Filter(name = "multi-tenant", condition = "tenant = :tenant")
public abstract class MultiTenantEntity extends GenericEntity {
@Transient
private transient String savedTenant;
@PostLoad
private void onLoad() throws Exception {
this.savedTenant = this.tenant;
onEntityModification();
}
@PrePersist
private void onPersist() {
if (getId() == null || getId().equals(0l)) {
tenant = SecurityUtil.getCurrentTenant();
}
}
@PreUpdate
@PreRemove
private void onEntityModification() throws Exception {
String currentTenant = SecurityUtil.getCurrentTenant();
if (!currentTenant.equals(tenant) || !savedTenant.equals(tenant)) {
throw new Exception();
}
}
@NotNull
private String tenant;
public String getTenant() {
return tenant;
}
}
如何在全局启用 多租户 hibernate 过滤器?