Multitenancy with Spring security JPA

我怕爱的太早我们不能终老 提交于 2019-12-01 09:06:50
gargii

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.

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