Tenant-based Data Isolation Method and System
By arranging tenant configuration in SaaS services, using Spring Jdbc and Mybatis mechanisms, multi-tenant data isolation is achieved, solving the flexibility and configuration problems of data isolation in a multi-tenant environment, and improving the universality and robustness of the system.
Patent Information
- Application Number
- CN202211128048.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-09-16
- Publication Date
- 2025-07-29
- Estimated Expiration
- 2042-09-16
AI Technical Summary
In the prior art, SaaS services are difficult to effectively achieve data isolation in a multi-tenant environment, resulting in data interaction between different tenants, lacking flexibility and configuration, and more code intervention is involved when new tenants are accessed.
Through the front-end and back-end convention tenant configuration, the HTTP protocol HEADER is used to pass the tenant identity, combined with the Spring Jdbc and Mybatis mechanisms, the data isolation of multiple libraries and single libraries + tenant fields is realized, and SQL calls are intercepted using AbstractRoutingDataSource and Interceptor, and the tenant fields are replaced with special placeholders.
It realizes the universality and flexibility of the multi-tenant system, reduces code intrusion, adapts to the personalized needs of different tenants, and improves the robustness and configuration of the system.
Smart Images

Figure CN115481435B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of data security, and specifically, to a tenant-based data isolation method and system. Background Art
[0002] SaaS service providers deploy reusable software services on servers, and customers purchase them on demand and obtain the required services through network requests. In this CS mode, a common scenario involves N tenants using a set of SaaS services. To avoid the mutual influence of data between different tenants, a data storage scheme based on multi-tenant data isolation plays a crucial role. Summary of the Invention
[0003] In view of the deficiencies in the prior art, the present invention provides a tenant-based data isolation method and system.
[0004] According to the tenant-based data isolation method and system provided by the present invention, the solution is as follows:
[0005] In the first aspect, a tenant-based data isolation method is provided, and the method includes:
[0006] Step S1: Configure tenant settings through an agreement between the front end and the back end, where different tenant identifiers in the tenant settings correspond to different database information;
[0007] Step S2: The front-end request carries the tenant identifier and is placed in the HEADER of the HTTP protocol. The back-end request obtains the tenant identifier through the HEADER and saves it in a thread-local variable;
[0008] Step S3: The back end inherits the AbstractRoutingDataSource class provided by the Spring Jdbc package, redefines the method of obtaining the data source when executing SQL, obtains the data source according to the tenant value corresponding to the tenant configuration, and simultaneously processes multi-database data isolation and single-database + tenant field data isolation;
[0009] Step S4: Through the Interceptor mechanism provided by Mybatis, intercept the prepare method of the StatementHandler.
[0010] Preferably, the process of request processing in step S2 corresponds to multiple methods of a single thread, the tenant identifier can be reused, and it needs to be cleared after the request processing is completed.
[0011] Preferably, for the single-database + tenant field data isolation, the tenant field is filled with a special placeholder to replace the real tenant value.
[0012] Preferably, step S3 specifically includes:
[0013] Step S3.1: When executing SQL, obtain the tenant identifier in the thread local variable, and according to the tenant configuration information, obtain the data source corresponding to the tenant identifier in the cache. If the data source exists, directly return the data source; if the data source does not exist, execute step S3.2;
[0014] Step S3.2: Initialize a new data source through the basic data source information in the tenant configuration, save it in the cache, and at the same time, the system intercepts the processing of malicious requests, considers them illegal requests, and needs to record logs and return corresponding error prompts.
[0015] Preferably, step S4 specifically includes:
[0016] Step S4.1: Through the interceptor of StatementHandler, intercept the call of SQL, identify the specific tenant identifier, and use the tenant value of the thread local variable for replacement;
[0017] Step S4.2: Through the interceptor of StatementHandler, intercept the call of SQL, for the single database + tenant field data isolation, identify whether the tenant field is used in the SQL, record the SQL that does not use the tenant field, and prompt the identification risk.
[0018] In a second aspect, a tenant-based data isolation system is provided, and the system includes:
[0019] Module M1: Agree on tenant configuration through the front end and the back end, and different tenant identifiers in the tenant configuration correspond to different database information;
[0020] Module M2: The front-end request carries the tenant identifier and places it in the HEADER of the HTTP protocol, and the back-end request obtains the tenant identifier through the HEADER and saves it in the thread local variable;
[0021] Module M3: The back end inherits the AbstractRoutingDataSource class provided by the Spring Jdbc package, redefines the method of obtaining the data source when executing SQL, obtains the data source according to the tenant value corresponding to the tenant configuration, and simultaneously processes multi-database data isolation and single database + tenant field data isolation;
[0022] Module M4: Through the Interceptor mechanism provided by Mybatis, intercept the prepare method of StatementHandler.
[0023] Preferably, the process of request handling in the module M2 corresponds to multiple methods of a single thread, which can reuse the tenant identifier and needs to be cleared after the request is processed.
[0024] Preferably, for the single database + tenant field data isolation, the tenant field is filled with a special placeholder to replace the real tenant value.
[0025] Preferably, the module M3 specifically includes:
[0026] Module M3.1: When executing SQL, obtain the tenant identifier in the thread local variable, and according to the tenant configuration information, obtain the data source corresponding to the tenant identifier in the cache. If the data source exists, directly return the data source; if the data source does not exist, execute Module M3.2;
[0027] Module M3.2: Initialize a new data source through the basic information of the data source in the tenant configuration, save it in the cache, and at the same time, the system intercepts the malicious request handling, considers it an illegal request, and needs to record the log and return the corresponding error prompt.
[0028] Preferably, the module M4 specifically includes:
[0029] Module M4.1: Through the interceptor of StatementHandler, intercept the call of SQL, identify the specific tenant identifier, and replace it with the tenant value of the thread local variable;
[0030] Module M4.2: Through the interceptor of StatementHandler, intercept the call of SQL. For the single database + tenant field data isolation, identify whether the tenant field is used in the SQL, record the SQL that does not use the tenant field, and prompt the identification risk.
[0031] Compared with the prior art, the present invention has the following beneficial effects:
[0032] 1. The present invention has universality. Multiple tenants use the same set of systems, and most functions of the system are applicable to each tenant;
[0033] 2. The present invention has flexibility. Through personalized configuration, it can adapt to the differences and personalized needs of different tenants;
[0034] 3. The present invention has configurability. The tenant access information can be configured, reducing the code intervention when a new tenant is connected;
[0035] 4. The single database tenant data isolation solution of the present invention fills the tenant field with a special placeholder to replace the real tenant value, avoiding the repeated processing of tenant parameters between multiple methods in the entire call stack, and achieving the effect of minimizing code intrusion to transfer tenant parameters. Brief Description of the Drawings
[0036] Other features, objects, and advantages of the present invention will become more apparent from the following detailed description of non - limiting embodiments with reference to the accompanying drawings:
[0037] Figure 1 This is the overall structural block diagram of the present invention. Detailed Embodiments
[0038] The present invention will be described in detail below in conjunction with specific embodiments. The following embodiments will help those skilled in the art to further understand the present invention, but do not limit the present invention in any form. It should be noted that for those of ordinary skill in the art, without departing from the concept of the present invention, several changes and improvements can still be made. These all fall within the protection scope of the present invention.
[0039] An embodiment of the present invention provides a tenant - based data isolation method. Referring to Figure 1 as shown, the method specifically includes:
[0040] Step S1: Configure tenants through an agreement between the front - end and the back - end. Different tenant identifiers correspond to different database information.
[0041] Step S2: The front - end request carries the tenant identifier, which can be placed in the HEADER of the HTTP protocol. The back - end request obtains the tenant identifier through the HEADER and saves it in a thread - local variable. The process of request processing corresponds to multiple methods of a single thread, and the tenant identifier can be reused. After the request processing is completed, it needs to be cleared to avoid affecting the processing of the next thread.
[0042] Step S3: The back - end inherits the AbstractRoutingDataSource class provided by the Spring Jdbc package and re - defines the way to obtain the data source when executing SQL. Obtain the data source according to the tenant value corresponding to the tenant configuration. It can handle both the multi - database data isolation and the single - database + tenant - field data isolation solutions simultaneously.
[0043] Specifically, this step includes:
[0044] 1) When executing SQL, first obtain the tenant identifier in the thread - local variable, and according to the tenant configuration information, obtain the data source corresponding to the tenant identifier in the cache. If the data source exists, directly return the data source; if the data source does not exist, proceed to the next step.
[0045] 2) Initialize a new data source based on the configured basic information of the data source (such as requesting to determine the database connection address, port, account, password, etc. corresponding to the tenant), save it in the cache to achieve the effect of reuse. At the same time, the system also needs to intercept malicious request processing. For example, when requesting a tenant value that does not exist in the tenant configuration, it is considered an illegal request, and logging needs to be performed and corresponding error prompts need to be returned to enhance the robustness of the system.
[0046] Step S4: Intercept the prepare method of StatementHandler through the Interceptor mechanism provided by Mybatis. For the single-database + tenant field data isolation solution, fill the tenant field with a special placeholder to replace the real tenant value, avoiding repeated processing of tenant parameters among multiple methods in the entire call stack, and achieving the effect of passing tenant parameters with minimal code intrusion.
[0047] Specifically, this step includes:
[0048] Through the interceptor of StatementHandler, intercept the SQL call, identify specific keywords, such as the tenant identifier @TENANT@ in select a from employee where zh=‘@TENANT@’, and replace it with the tenant value of the thread local variable.
[0049] Step S4.2: Through the interceptor of StatementHandler, intercept the SQL call. For the single-database + tenant field data isolation solution, it can identify whether the tenant field is used in the SQL, record the SQL that does not use the tenant field, prompt the identification risk, and provide the robustness of the system.
[0050] The embodiment of the present invention provides a tenant-based data isolation method and system. Based on the extension mechanisms of industry-recognized components such as Spring MVC interceptors, Spring JDBC, and Mybatis, a set of tenant identification mechanisms is customized. Through flexible configuration, it can support not only one set of services + one set of databases, but also one set of services + multiple sets of databases and other two different data isolation solutions. Provide a system-level tenant data source switching solution with minimal code intrusion, and at the same time provide a simple tool to check whether the tenant field is added to the SQL operation of the DB, and uniformly identify and replace the tenant field in the SQL operation. The present invention will configure a default data source to store tenant-related configuration information, and the addition of the data source is insensitive to the business.
[0051] Those skilled in the art know that, in addition to implementing the system and its various devices, modules, and units provided by the present invention in the form of pure computer-readable program code, it is entirely possible to logically program the method steps so that the system and its various devices, modules, and units provided by the present invention are implemented in the form of logic gates, switches, application-specific integrated circuits, programmable logic controllers, and embedded microcontrollers, etc., to achieve the same functions. Therefore, the system and its various devices, modules, and units provided by the present invention can be considered as a kind of hardware component, and the devices, modules, and units included therein for implementing various functions can also be regarded as the structures within the hardware component; the devices, modules, and units for implementing various functions can also be regarded as either software modules for implementing the method or structures within the hardware component.
[0052] The specific embodiments of the present invention have been described above. It should be understood that the present invention is not limited to the above specific embodiments, and those skilled in the art can make various changes or modifications within the scope of the claims, which do not affect the essence of the present invention. Without conflict, the embodiments of the present application and the features in the embodiments can be combined with each other arbitrarily.
Claims
1. A tenant-based data isolation method, characterized in that, Including: Step S1: Configure tenant settings through agreement between the front end and the back end. Different tenant identifiers in the tenant settings correspond to different database information; Step S2: The front-end request carries the tenant identifier and is placed in the HEADER of the HTTP protocol. The back-end request obtains the tenant identifier through the HEADER and saves it in a thread-local variable; Step S3: The back end inherits the AbstractRoutingDataSource class provided by the Spring Jdbc package, redefines the way to obtain the data source when executing SQL, obtains the data source according to the tenant value corresponding to the tenant configuration, and simultaneously processes multi-database data isolation and single-database + tenant field data isolation; Step S4: Through the Interceptor mechanism provided by Mybatis, intercept the prepare method of StatementHandler; The specific content of Step S4 includes: Step S4.1: Through the interceptor of StatementHandler, intercept the call of SQL, identify the specific tenant identifier, and replace it with the tenant value of the thread-local variable; Step S4.2: Through the interceptor of StatementHandler, intercept the call of SQL. For the single-database + tenant field data isolation, identify whether the tenant field is used in the SQL, record the SQL that does not use the tenant field, and prompt the identification risk.
2. The data isolation method based on tenants according to claim 1, wherein, The process of request processing in Step S2 corresponds to multiple methods of a single thread, and the tenant identifier can be reused. After the request processing is completed, it needs to be cleared.
3. The data isolation method based on tenants according to claim 1, characterized in that, For the single-database + tenant field data isolation, fill the tenant field with a special placeholder to replace the real tenant value.
4. The data isolation method based on tenants according to claim 1, wherein The specific content of Step S3 includes: Step S3.1: When executing SQL, obtain the tenant identifier in the thread-local variable. According to the tenant configuration information, obtain the data source corresponding to the tenant identifier in the cache. If the data source exists, directly return the data source; if the data source does not exist, execute Step S3.2; Step S3.2: Initialize a new data source through the data source basic information in the tenant configuration, save it in the cache, and at the same time, the system intercepts malicious request processing, considers it an illegal request, and needs to record the log and return the corresponding error prompt.
5. A tenant-based data isolation system, characterized in that, Including: Module M1: Configure tenant settings through agreement between the front end and the back end. Different tenant identifiers in the tenant settings correspond to different database information; Module M2: The front-end request carries the tenant identifier and is placed in the HEADER of the HTTP protocol. The back-end request obtains the tenant identifier through the HEADER and saves it in a thread-local variable; Module M3: The back end inherits the AbstractRoutingDataSource class provided by the Spring Jdbc package, redefines the way to obtain the data source when executing SQL, obtains the data source according to the tenant value corresponding to the tenant configuration, and simultaneously processes multi-database data isolation and single-database + tenant field data isolation; Module M4: Intercept the prepare method of StatementHandler through the Interceptor mechanism provided by Mybatis; The specific content of Module M4 includes: Module M4.1: Through the interceptor of StatementHandler, intercept the SQL call, identify the specific tenant identifier, and replace it with the tenant value of the thread local variable; Module M4.2: Through the interceptor of StatementHandler, intercept the SQL call. For the single database + tenant field data isolation, identify whether the tenant field is used in the SQL, record the SQL that does not use the tenant field, and prompt the identification risk.
6. The tenant-based data isolation system according to claim 5, wherein The process of request handling in Module M2 corresponds to multiple methods of a single thread, which can reuse the tenant identifier, and the tenant identifier needs to be cleared after the request handling is completed.
7. The tenant-based data isolation system according to claim 5, wherein For the single database + tenant field data isolation, fill the tenant field with a special placeholder to replace the real tenant value.
8. The data isolation system based on tenants according to claim 5, wherein The specific content of Module M3 includes: Module M3.1: When executing SQL, obtain the tenant identifier in the thread local variable, and according to the tenant configuration information, obtain the data source corresponding to the tenant identifier in the cache. If the data source exists, directly return the data source; if the data source does not exist, execute Module M3.2; Module M3.2: Initialize a new data source through the data source basic information in the tenant configuration, save it in the cache, and at the same time, the system intercepts the malicious request handling, considers it an illegal request, and needs to record the log and return the corresponding error prompt.
Citation Information
Patent Citations
SaaS multi-tenant data isolation access method and device, electronic equipment and storage medium
CN111491012A
Multi-tenant database management and control method and system and storage medium
CN113535723A