使用 spring-boot 和 spring-data 全局启用休眠过滤器
2022-09-04 23:32:45
我正在尝试通过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;
}
}
如何全局启用多租户休眠筛选器?