基于用户的可变数据源 [英] Variable datasource based on user

查看:202
本文介绍了基于用户的可变数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个后端,第一次运行安全法则等等,它使我的数据库设计变得复杂: 规格

包含有限用户信息(user_id,电子邮件,密码(哈希和盐渍))的数据库应用程序的中央服务器可以位于任何位置。



使用我们服务的组织要求将所有其他信息存储在内部,因此该特定组织的数据库位于其建筑物中。

我们中央数据库中的用户ID由这些组织数据库中的多种类型的用户使用,其中存储了有关该用户的更多信息(电话号码,姓名,地址)。 ..)

问题



使用Spring Boot时,我需要这样做,所使用的数据源由哪个用户提出请求。我将用户映射到中央服务器中相应组织的数据库,以便信息存在,但我不确定如何制作此变量。

据我所知,有些方法涉及在application.properties文件中添加另一个数据库配置。但据我所知,一旦服务器部署完成并且没有完全重新部署就不能改变(容易),并且我希望以这种方式建立它,以便添加另一个组织只需要设置它们db,并将其他数据库详细信息添加到中央服务器。

额外的细节



我想用CrudRepository和hibernate实体来做这件事。我计划只在中央服务器上生成用户标识。



任何指针都很棒。

谢谢!

解决方案

租赁。对于多租户有多种策略:不同的数据库,同一数据库中的不同模式,以及具有定义的鉴别器的一个数据库上的相同模式。

您基本上创建了一个DataSourceBasedMultiTenantConnectionProviderImpl类,该类提供与租户请求它的数据源的连接,以及一个CurrentTenantIdentifierResolverImpl类,该类标识谁是请求租户。

您可以阅读更多有关它的信息这里。由于您的租户各自拥有自己的数据库,因此您可能需要关注多租户独立数据库方法。当我实现它时,它与CrudRepository一起工作良好。你也可能想找到自己的方式来创建租户地图,因为我有2个租户,并且无需在任何时候添加更多。



下面是一个示例连接提供程序从我实现这个时:

$ pre $ public $ DataSourceBasedMultiTenantConnectionProviderImpl extends AbstractDataSourceBasedMultiTenantConnectionProviderImpl {

private static final String DEFAULT_TENANT_ID =A;

@Autowired
私有DataSource datasourceA;

@Autowired
私有DataSource datasourceB;

私人地图< String,DataSource>地图;

@PostConstruct
public void load(){$ b $ map = new HashMap<>();
map.put(A,datasourceA);
map.put(B,datasourceB);
}

@Override
protected DataSource selectAnyDataSource(){
return map.get(DEFAULT_TENANT_ID);
}

@Override
protected DataSource selectDataSource(String tenantIdentifier){
return map.get(tenantIdentifier);
}
}


I'm currently developing a back end and having my first run in with security laws etc. and it has complicated the design of my DB slightly:

Specification

Central server for app with DB containing limited user information (user_id, email, password (hashed and salted)) can be anywhere.

Organisations making use of our service require that all other information be stored in-house, so the database for that particular organisation is in their building.

The user IDs in our central database are used by multiple types of users in these organisations databases, where more info about that user is stored (phone number, name, address...)

Problem

With Spring Boot, I need to make it so the datasource used is determined by which user makes the request. I map users to their corresponding organisation's database within the central server so the information is there, but I'm not sure how to make this variable.

I understand there are methods involving adding another database config in the application.properties file. But as far as I'm aware this can't be changed (easily) once the server is deployed and running without a full redeploy, and I'm hoping to build this in such a way that adding another organisation only involves setting up their db, and adding another database details to the central server.

Extra detail

I'd like to use CrudRepository with hibernate entities for this. I plan on only generating user IDs on the central server.

Any pointers would be awesome.

Thanks!

解决方案

The terminology for this is database multi-tenancy. There are multiple strategies for multi-tenancy: different databases, different schemas in the same database, and the same schema on one database with a defined discriminator.

You basically create a DataSourceBasedMultiTenantConnectionProviderImpl class which provides the connection to a datasource based on which tenant is requesting it, and a CurrentTenantIdentifierResolverImpl class which identifies who is the requesting tenant.

You can read more about it here. Since your tenants each have their own database, you would probably want to focus on the multi-tenancy separate database approach. It worked fine with CrudRepository when I implemented it. You also might want to find your own way of creating the tenant map, since I had 2 tenants and no need to add more at any point.

Heres a sample of the connection provider from when I implemented this:

public class DataSourceBasedMultiTenantConnectionProviderImpl extends AbstractDataSourceBasedMultiTenantConnectionProviderImpl {

    private static final String DEFAULT_TENANT_ID = "A";

    @Autowired
    private DataSource datasourceA;

    @Autowired
    private DataSource datasourceB;

    private Map<String, DataSource> map;

    @PostConstruct
    public void load() {
        map = new HashMap<>();
        map.put("A", datasourceA);
        map.put("B", datasourceB);
    }

    @Override
    protected DataSource selectAnyDataSource() {
        return map.get(DEFAULT_TENANT_ID);
    }

    @Override
    protected DataSource selectDataSource(String tenantIdentifier) {
        return map.get(tenantIdentifier);
    }
}

这篇关于基于用户的可变数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆