Multitenancy with Spring security JPA

馋奶兔 提交于 2019-12-19 09:38:25

问题


This is follow up of this Multitenancy with Spring JPA

I chose to use the 'AbstractRoutingDataSource'. But the problem is now datasource and entitymanager bean initialized at startup. is there anyway to configure this in spring which way that it will initialize after user is authenticated?

Another problem which i can think of will be how to handle concurrency. I put tenantId in this class

public class ThreadLocalContextUtil {
 private static final ThreadLocal<String> contextHolder =
            new ThreadLocal<String>();

   public static void setTenantId(String tenantId) {
      Assert.notNull(tenantId, "customerType cannot be null");
      contextHolder.set(tenantId);
   }

   public static String getTenantId() {
      return (String) contextHolder.get();
   }

   public static void clearTenant() {
      contextHolder.remove();
   }
}

The solution i can think of will be to remove the tenantId after the datasource is initialized. is that correct?


回答1:


I've solved similar problem. I implemented my own TenantAwareDataSource based on Spring's AbstractDataSource. It takes tenantId from a session-scoped bean named tenantContext. This bean is updated every time the incoming request is processed. It is done by using Spring Security's security filter:

<security:http auto-config='false' >
    <security:custom-filter before="FIRST" ref="tenantFilter" />
    <!-- ...more security stuff... -->
</security:http>

My TenantAwareDataSource is initialized in the startup time, but it does not matter because it is created empty - it contains no tenant datasources (e.g. pooled JDBC datasources or JPA entity manager). They are created lazily when the getConnection() is called for the first time for the selected tenant.

So, my TenantAwareDataSource maintains its own dynamic datasource map while AbstractRoutingDataSource expects static initializitaion of the datasource map done in the startup time.

Read more detailed description in this article.



来源:https://stackoverflow.com/questions/9720449/multitenancy-with-spring-security-jpa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!