简体   繁体   English

使用spring-boot和spring-data全局启用hibernate过滤器

[英]Enable hibernate filter globally with spring-boot & spring-data

I am trying to implement a multi-tenancy by discriminator implementation with Spring Boot and Spring Data. 我试图通过Spring Boot和Spring Data实现鉴别器实现的多租户。

I have made an abstract class to represent a multi-tenant entity. 我创建了一个抽象类来表示一个多租户实体。 Something similar to this: 与此类似的东西:

@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;
    }
}

How do I enable the multi-tenant hibernate filter globally? 如何全局启用多租户休眠过滤器?

Using hibernate filters its easy to implement multitenantcies even for Row level ACL as well possible in our application. 使用hibernate过滤器,即使对于行级ACL也很容易实现多重性,在我们的应用程序中也是如此。 Instead of discrimators you could use AOP and different filters configurable in your db. 您可以在数据库中使用AOP和可配置的不同过滤器,而不是使用描述符。 Before calling your request method based on the accessing user apply the filter that is enable the hibernate session filter and exeute the request and after successfully request processing disable the filters. 在根据访问用户调用请求方法之前,应用启用hibernate会话过滤器的过滤器并执行请求,并在成功请求处理后禁用过滤器。 thats it. 而已。 Using this way you could add any number of filters to any number entities that are going to be operated by the current user and you could do the perfect resource (Entity and CRUD) management with this. 使用这种方式,您可以将任意数量的过滤器添加到将由当前用户操作的任何数字实体,您可以使用此方法执行完美的资源(实体和CRUD)管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM