Disclosure of Invention
The invention aims to provide a data authority control method, which is used for realizing accurate control on data lines according to a configurable data strategy, intercepting SQL sentences generated by Hibernate and modifying the SQL sentences according to actual configuration so as to achieve the aim of controlling the data authority.
In order to achieve the above purpose, the embodiment of the invention provides a data authority control method, which comprises the steps of obtaining an SQL sentence, dividing the SQL sentence with a defined data authority strategy to obtain a plurality of SQL sentence segments, traversing the SQL sentence segments one by one, judging whether the SQL sentence segments starting with a select are the first element after division, if not, carrying out secondary division on the SQL sentence segments, if so, analyzing the table names of the SQL sentence segments, screening out the SQL sentence segments with the table names defining the data authority strategy, replacing the table names of the SQL sentence segments with the table names defining the data authority strategy to be select sentences with authority conditions added, obtaining a plurality of modified SQL sentence segments, and reconnecting the modified SQL sentence segments to obtain a target SQL sentence segment.
The method comprises the steps of analyzing table names of SQL statement sections, screening out the SQL statement sections with the table names defining a data authority policy, and screening out the SQL statement sections with the table names defining the data authority policy by searching and marking the starting positions, the interval positions and the cut-off positions of the SQL statement sections, wherein the starting positions comprise positions of from keywords, the cut-off positions comprise final indexes of the SQL statement sections, and the SQL statement sections with the table names defining the data authority policy are screened out by combining the starting positions, the interval positions and the cut-off positions.
The method comprises the steps of searching and marking the starting position, the interval position and the cut-off position of a table in an SQL statement segment, searching a from keyword by using a regular expression, searching a separator keyword or a separation character by using the regular expression, wherein the separator keyword comprises left, join and/or on, the separation character comprises comma, searching a window, group by, order by and/or a division keyword by using the regular expression, using the position of the from keyword as the starting position of the table, using the position of the separator keyword and/or the separation character as the interval position of the table, using the position of the separator keyword and/or the separation character as the cut-off position of the table, and using the final index of the SQL statement segment as the cut-off position of the table when any keyword of the window, group by, order by and division is not searched.
Specifically, the method for acquiring the SQL statement comprises the steps of creating EmptyInterceptor interfaces provided by an interceptor for realizing Hibernate, and rewriting onPrePareStatement the generated SQL statement.
Specifically, the SQL statement with the defined data authority policy is divided, so that the SQL statement with the defined data authority policy is divided in a plurality of SQL statement segments, and the used regular expression comprises a regular expression for judging the start of sub-query (.
The embodiment of the invention also provides a data authority control device which comprises an acquisition module, a segmentation module, a judgment module, a circulation module, a screening module and a replacement module, wherein the acquisition module is used for acquiring an SQL sentence, the segmentation module is used for segmenting the SQL sentence with a defined data authority strategy to obtain a plurality of SQL sentence segments, the judgment module is used for traversing the SQL sentence segments one by one and judging whether the SQL sentence segments started by a select are the first elements after segmentation, the circulation module is used for enabling the SQL sentence segments started by the select and being the first elements to enter circulation for secondary segmentation, the screening module is used for analyzing the table names of the SQL sentence segments started by the select and not being the first elements and screening out the SQL sentence segments with the table names defining the data authority strategy, the replacement module is used for replacing the table names of the SQL sentence segments with the table names defining the data authority strategy to be the selected sentence segments with the authority conditions added, and the reshaping module is used for reconnecting the modified SQL sentence segments to obtain the target SQL sentence segments.
The screening module comprises a searching and marking module and a combining module, wherein the searching and marking module is used for searching and marking the starting position, the interval position and the cut-off position of the table in the SQL sentence segment, the starting position comprises the position of the from key word, the cut-off position comprises the last index position of the SQL sentence segment, and the combining module is used for combining the starting position, the interval position and the cut-off position to screen the SQL sentence segment with the table name defining the data authority strategy.
The search marking module comprises a starting position search module, an interval position search module and a position marking module, wherein the starting position search module is used for searching a from keyword by using a regular expression, the interval position search module is used for searching a separator keyword or a separator character by using the regular expression, the separator keyword comprises left, join and/or on, the separator character comprises comma, the ending position search module is used for searching a where, group by, order by and/or a moving keyword by using the regular expression, the position marking module is used for taking the position of the from keyword as the starting position of a table, the separator keyword and/or the separator character as the interval position of the table, the position of the where, group by, order by and/or the moving keyword are taken as the ending position of the table, and when any keyword of the where, group by, order by and moving keyword is not searched, the last index of the SQL segment is taken as the ending position of the table.
The embodiment of the invention also provides electronic equipment, which comprises a processor, a memory and a bus, wherein the bus is used for connecting the processor and the memory, the memory is used for storing operation instructions, and the processor is used for executing the data authority control method by calling the operation instructions.
The embodiment of the invention also provides a computer storage medium for storing computer instructions which, when run on a computer, enable the computer to perform the data authority control method.
The embodiment of the invention has the following beneficial effects:
The embodiment of the invention provides a data authority control method and device, wherein the method comprises the steps of obtaining an SQL sentence, dividing the SQL sentence with a defined data authority strategy to obtain a plurality of SQL sentence segments, traversing the SQL sentence segments one by one, judging whether the SQL sentence segments starting with a select are the first divided element, adding the data authority strategy for the SQL sentence segments if not, dividing again, analyzing the table names of the SQL sentence segments, screening out the SQL sentence segments with the table names defining the data authority strategy, replacing the table names of the SQL sentence segments with the table names defining the data authority strategy for select sentences with authority conditions, obtaining a plurality of changed SQL sentence segments, and reconnecting the changed SQL sentence segments to obtain a target SQL sentence segment.
Compared with the prior art, the invention can achieve data authority management by intercepting and modifying the SQL statement generated by Hibernates, and can realize authority management on certain types of data, so that a user can only inquire and manage user information under the department where the user is located. The invention can reduce the complexity of data authority control development in WEB application, add the control of the data authority after the development is completed, adjust the data control authority at any time and rarely even do not need to modify the applied code, and can enhance the stability and the robustness of the data authority control. After the data authority control scheme code completes the test, only the defined data strategy is required to be tested and adjusted for the data authority control in the subsequent project.
Detailed Description
The following description of the embodiments of the present invention will be made with reference to the accompanying drawings, in which it is evident that the embodiments described are only some embodiments of the present invention, but not all embodiments. All other embodiments, which can be made by those skilled in the art based on the embodiments of the invention without making any inventive effort, are intended to be within the scope of the invention.
It should be understood that the step numbers used herein are for convenience of description only and are not limiting as to the order in which the steps are performed.
It is to be understood that the terminology used in the description of the invention is for the purpose of describing particular embodiments only and is not intended to be limiting of the invention. As used in this specification and the appended claims, the singular forms "a," "an," and "the" are intended to include the plural forms as well, unless the context clearly indicates otherwise.
The terms "comprises" and "comprising" indicate the presence of stated features, integers, steps, operations, elements, and/or components, but do not preclude the presence or addition of one or more other features, integers, steps, operations, elements, components, and/or groups thereof.
The term "and/or" refers to any and all possible combinations of one or more of the associated listed items, and includes such combinations.
In order to facilitate understanding of the embodiments of the present invention by those skilled in the art, the following description will explain the related art content of the embodiments of the present invention.
The data authority policy indicates what kind of constraint is added to the main body (table or view), for example, when the user table is queried, the department ID is added to the department where the current login user is located, that is, the data authority policy.
The Hibernate is an object relation mapping framework of open source codes, which encapsulates the JDBC with very light weight, builds the mapping relation between POJO and database table, is a fully automatic orm framework, hibernate can automatically generate SQL sentences and automatically execute, so that Java programmers can operate the database by using object programming thinking as desired. The Hibernate can be applied to any occasion using JDBC, can be used in Java client programs, and can also be used in Web applications of servlets/JSP, and most revolutionarily, the Hibernate can replace CMP in JaveEE architecture applying EJB to finish the re-conception of data persistence.
Regular expressions, (Regular Expression, also known as regular expressions, often abbreviated in code as regex, regex p, or RE), are commonly used to retrieve, replace text that conforms to a certain pattern (rule). A regular expression is a logic formula for operating on a character string, and a rule character string is formed by a plurality of specific characters defined in advance and a combination of the specific characters, and the rule character string is used for expressing a filtering logic for the character string.
The following list of rule strings used in parsing SQL statements:
1. the regular expression between brackets [ - (- ])
2. The regular expression of the table name is [ a-zA-Z_ 0-9] +
3. Judging whether the SQL statement starts with a regular expression of SELECT:
\A(\s*|\()\s*[sS][eE][lL][eE][cC][tT][\s|?=\(|*]
4. judging a regular expression of sub-query start:
(?!\A)\(\s*[sS][eE][lL][eE][cC][tT][\s|?=\(|*]
5. regular expressions matching FROM keywords:
(\s+|[*])[fF][rR][oO][mM](\s+|(?=\())
6. Regular expressions matching UPDATE keywords:
\s*[uU][pP][dD][aA][tT][eE]\s+(?!(\s|[(]))
7. regular expressions matching DELETE keywords:
\s*[dD][eE][lL][eE][tT][eE]\s+(?!(\s|[(]))
8. regular expressions matching DELETE FROM keywords:
[dD][eE][lL][eE][tT][eE]\s+[fF][rR][oO][mM]\s+(?!(\s|[(]))
9. regular expressions matching the WHERE key:
(\)|\s)[wW][hH][eE][rR][eE](\s+|(?=\())
10. Regular expressions matching the HAVING keywords:
(\)|\s)[hH][aA][vV][iI][nN][gG](\s+|(?=\())
11. Regular expressions matching ORDERBY keywords:
(\)|\s)[oO][rR][dD][eE][rR]\s+[bB][yY](\s+|(?=\())
12. regular expressions matching GROUP BY keywords:
(\)|\s)[gG][rR][oO][uU][pP]\s+[bB][yY](\s+|(?=\())
13. Regular expressions matching FOR UPDATE keywords:
(\)|\s+)[fF][oO][rR]\s+[uU][pP][dD][aA][tT][eE](\s+|(?=\())
14. regular expressions matching the UNION keywords:
(\)|\s+)[uU][nN][iI][oO][nN](\s+|(?=\())
15. regular expressions matching the UNION ALL keywords:
(\)|\s+)[uU][nN][iI][oO][nN]\s+[aA][lL][lL](\s+|(?=\())
16. regular expressions matching MINUS keywords:
(\)|\s+)[mM][iI][nN][uU][sS](\s+|(?=\())
17. Regular expressions matching the INTERSECT keywords:
(\)|\s+)[iI][nN][tT][eE][rR][sS][eE][cC][tT](\s+|(?=\())
18. Regular expressions matching ON keywords:
(\)|\s+)[oO][nN]\s*
19. regular expressions matching JOIN keywords:
(\)|\s+)[jJ][oO][iI][nN]\s+(?!(\s|[(]))
20. regular expressions matching the LEFT JOIN key:
(\)|\s+)[lL][eE][fF][tT]\s+[jJ][oO][iI][nN](\s+|(?=\())
21. regular expressions matching RIGHT JOIN keywords:
(\)|\s+)[rR][iI][gG][hH][tT]\s+[jJ][oO][iI][nN](\s+|(?=\())
22. regular expressions matching FULL JOIN keywords:
(\)|\s+)[fF][uU][lL][lL]\s+[jJ][oO][iI][nN](\s+|(?=\())
23. regular expressions matching the LEFT output JOIN key:
(\)|\s+)[lL][eE][fF][tT]\s+[oO][uU][tT][eE][rR]\s+[jJ][oO][iI][nN](\s+|(?=\())
24. Regular expressions matching RIGHT OUTER JOIN keywords:
(\)|\s+)[rR][iI][gG][hH][tT]\s+[oO][uU][tT][eE][rR]\s+[jJ][oO][iI][nN](\s+|(? =\())
25. Regular expressions matching the FULL OUTER JOIN key:
(\)|\s+)[fF][uU][lL][lL]\s+[oO][uU][tT][eE][rR]\s+[jJ][oO][iI][nN](\s+|(?=\())
26. regular expressions matching INNER JOIN keywords:
(\)|\s+)[iI][nN][nN][eE][rR]\s+[jJ][oO][iI][nN](\s+|(?=\())
the data authority control method provided by the embodiment of the invention will be described and illustrated in detail through several specific embodiments.
Referring to fig. 1, fig. 1 is a flowchart of a data authority control method according to an embodiment of the invention. To achieve the above object, an embodiment of the present invention provides a data authority control method, including:
step S110, an SQL sentence is acquired.
The means for obtaining the SQL statement comprises creating EmptyInterceptor interfaces provided by the interceptor for realizing Hibernate, and rewriting onPrePareStatement the SQL statement generated by interception.
Step S120, the SQL sentence with the defined data authority strategy is segmented to obtain a plurality of SQL sentence segments.
In this embodiment, a regular expression is used to analyze an SQL statement, and the SQL statement having a defined data authority policy is segmented, where the used regular expression includes a regular expression for determining the start of a sub-query:
(?!\A)\(\s*[sS][eE][lL][eE][cC][tT][\s|?=\(|*]。
For example, an SQL statement "select ename,job,sal from emp where job=(select job from emp where ename='SCOTT')and sal>(select sal from emp where ename='JAMES')", is analyzed by regular expression and then partitioned into five sections, namely a first section "SELECT ENAME, job, sal from emp where job = (", a second section "select job from EMP WHERE ENAME = 'SCOTT'" third section ") and sal > (", a fourth section "SELECT SAL from EMP WHERE ENAME = 'JAMES'", a fifth section ")". The first segment is the first element after segmentation.
Step S130, traversing the SQL sentence segments one by one, and judging whether the SQL sentence segment starting with the select is the first element after segmentation.
If the SQL statement segment is started with select but not the first element after segmentation, the process returns to step S120, and if the SQL statement segment is started with select and the first element after segmentation, step S140 is executed.
The first element after segmentation, representing the first of several segments of statements obtained by segmentation using regular expression analysis, performs this determination because the SQL statement segment from which the second selection begins may also have sub-queries in it.
And step S140, analyzing the table name of the SQL statement section, and screening out the SQL statement section with the table name defining the data authority policy.
The method comprises the steps of firstly, searching and marking a starting position, an interval position and a cut-off position of a table in an SQL statement section by judging whether the table name of the SQL statement section defines a data authority strategy or not, wherein the starting position comprises a position of a from keyword, the cut-off position comprises a last index position of the SQL statement section, and the starting position, the interval position and the cut-off position are combined to screen the SQL statement section with the table name defining the data authority strategy.
Step S150, replacing the SQL statement segment with the table name defining the data authority policy with the table name of the selected statement added with the authority condition to obtain a plurality of modified SQL statement segments. The sphere clause of this select statement is a well defined data authority policy for the current table.
And step S160, reconnecting the modified SQL sentence segment to obtain a target SQL sentence segment. As a data authority control strategy based on the Hibernate, the target SQL statement is finally provided to the Hibernate.
The method comprises the steps of searching and marking the starting position, the interval position and the cut-off position of a table in an SQL sentence segment, searching a from keyword by using a regular expression, searching a separator keyword or a separator character by using the regular expression, wherein the separator keyword comprises left, join and/or on, the separator character comprises comma (,), searching a where, a group by, an order by and/or a having keyword by using the regular expression, taking the position of the from keyword as the starting position of the table, taking the position of the separator keyword and/or the separator character as the interval position of the table, taking the position of the where, the group by, the order by and/or the having keyword as the cut-off position of the table, and taking the last index of the SQL sentence segment as the cut-off position of the table when any keyword of where the word is found is not found.
The regular expression used for searching the from keyword is as follows:
(\s+|[*])[fF][rR][oO][mM](\s+|(?=\())。
The purpose of searching the keywords of where, group by, order by, have, etc. is to search the cut-off position of the table which may exist, and the regular expression used includes:
Regular expressions matching the WHERE key:
(\)|\s)[wW][hH][eE][rR][eE](\s+|(?=\())
regular expressions matching the HAVING keywords:
(\)|\s)[hH][aA][vV][iI][nN][gG](\s+|(?=\())
Regular expressions matching ORDERBY keywords:
(\)|\s)[oO][rR][dD][eE][rR]\s+[bB][yY](\s+|(?=\())
Regular expressions matching GROUP BY keywords:
(\s) |gG ] [ rR ] [ oO ] [ uU ] [ pP ] [ bB ] [ yY ] (\s+ | (.
After searching the from keyword, other regular expressions need to be searched instead, including:
Regular expressions matching the UNION keywords:
(\)|\s+)[uU][nN][iI][oO][nN](\s+|(?=\())
Regular expressions matching the UNION ALL keywords:
(\)|\s+)[uU][nN][iI][oO][nN]\s+[aA][lL][lL](\s+|(?=\())
regular expressions matching MINUS keywords:
(\)|\s+)[mM][iI][nN][uU][sS](\s+|(?=\())
regular expressions matching the INTERSECT keywords:
(\)|\s+)[iI][nN][tT][eE][rR][sS][eE][cC][tT](\s+|(?=\())
Regular expressions matching ON keywords:
(\)|\s+)[oO][nN]\s*
regular expressions matching JOIN keywords:
(\)|\s+)[jJ][oO][iI][nN]\s+(?!(\s|[(]))
Regular expressions matching the LEFT JOIN key:
(\)|\s+)[lL][eE][fF][tT]\s+[jJ][oO][iI][nN](\s+|(?=\())
Regular expressions matching RIGHT JOIN keywords:
(\)|\s+)[rR][iI][gG][hH][tT]\s+[jJ][oO][iI][nN](\s+|(?=\())
regular expressions matching FULL JOIN keywords:
(\)|\s+)[fF][uU][lL][lL]\s+[jJ][oO][iI][nN](\s+|(?=\())
Regular expression of LEFT output JOIN key:
(\)|\s+)[lL][eE][fF][tT]\s+[oO][uU][tT][eE][rR]\s+[jJ][oO][iI][nN](\s+|(?=\())
regular expression of RIGHT OUTER JOIN key:
(\)|\s+)[rR][iI][gG][hH][tT]\s+[oO][uU][tT][eE][rR]\s+[jJ][oO][iI][nN](\s+|(? =\())
regular expression of FULL OUTER JOIN key:
(\)|\s+)[fF][uU][lL][lL]\s+[oO][uU][tT][eE][rR]\s+[jJ][oO][iI][nN](\s+|(?=\())
regular expression of INNER JOIN keywords:
(\)|\s+)[iI][nN][nN][eE][rR]\s+[jJ][oO][iI][nN](\s+|(?=\())
Referring to fig. 2, fig. 2 is a schematic diagram of a data authority control device according to an embodiment of the invention. The same parts as those of the above embodiment in this embodiment are not described here again. The embodiment of the invention also provides a data authority control device, which comprises:
An obtaining module 210, configured to obtain an SQL statement;
the segmentation module 220 is configured to segment an SQL statement having a defined data authority policy to obtain a plurality of SQL statement segments;
A judging module 230, configured to traverse the SQL statement segments one by one, and judge whether the SQL statement segment started with select is the first element after segmentation;
A loop module 240 for entering the SQL statement segment beginning at select and being the first element back to the partition module 220 for secondary partitioning;
the filtering module 250 is configured to parse a table name of an SQL statement segment that starts with a select and is not the first element, and filter out the SQL statement segment whose table name defines the data authority policy;
A replacing module 260, configured to replace a selected statement whose table name defines an SQL statement segment of the data authority policy, where the table name is a select statement added with an authority condition, to obtain a plurality of modified SQL statement segments;
and the remodelling module 270 is configured to reconnect the modified SQL statement segment to obtain a target SQL statement segment.
The screening module 250 comprises a searching and marking module 251 and a combining module 252, wherein the searching and marking module 251 is used for searching and marking the starting position, the interval position and the cut-off position of the table in the SQL sentence segment, the starting position comprises the position of the from keyword, the cut-off position comprises the last index position of the SQL sentence segment, and the combining module 252 is used for combining the starting position, the interval position and the cut-off position to screen the SQL sentence segment with the table name defining the data authority strategy.
The lookup marking module 251 comprises a starting position lookup module for searching a from keyword by using a regular expression, an interval position lookup module for searching a separator keyword or a separator character by using the regular expression, wherein the separator keyword comprises left, join and/or on, the segmentation character comprises comma, a cut-off position lookup module for searching a where, group by, order by and/or a moving keyword by using the regular expression, and a position marking module for taking the position of the from keyword as the starting position of the table, the separator keyword and/or the separator character as the interval position of the table, and the position of the separator keyword and/or the separator keyword as the cut-off position of the table, and taking the last index of the SQL sentence segment as the cut-off position of the table when any keyword of where, group by, order by and/or moving is not searched.
In an alternative embodiment, an electronic device is provided, as shown in FIG. 3, the electronic device 300 shown in FIG. 3 comprising a processor 310 and a memory 330. Wherein the processor 310 is coupled to the memory 330, such as via a bus 320. Optionally, the electronic device 300 may also include a transceiver 340. It should be noted that, in practical applications, the transceiver 340 is not limited to one, and the structure of the electronic device 300 is not limited to the embodiment of the present application.
The processor 310 may be a CPU, general-purpose processor, DSP, ASIC, FPGA or other programmable logic device, transistor logic device, hardware component, or any combination thereof. Which may implement or perform the various exemplary logic blocks, modules and circuits described in connection with this disclosure. Processor 310 may also be a combination that performs computing functions, e.g., including one or more microprocessor combinations, a combination of a DSP and a microprocessor, etc.
Bus 320 may include a path that communicates information between the components. Bus 320 may be a PCI bus, an EISA bus, or the like. The bus 320 may be classified into an address bus, a data bus, a control bus, and the like. For ease of illustration, only one thick line is shown in fig. 3, but not only one bus or one type of bus.
Memory 330 may be, but is not limited to, a ROM or other type of static storage device that can store static information and instructions, a RAM or other type of dynamic storage device that can store information and instructions, an EEPROM, a CD-ROM or other optical disk storage, optical disk storage (including compact disks, laser disks, optical disks, digital versatile disks, blu-ray disks, etc.), magnetic disk storage media or other magnetic storage devices, or any other medium that can be used to carry or store desired program code in the form of instructions or data structures and that can be accessed by a computer.
Memory 330 is used to store application code for performing the execution of aspects of the present application and is controlled by processor 310. Processor 310 is operative to execute application code stored in memory 330 to implement what is shown in any of the method embodiments described previously.
Among them, the electronic devices include, but are not limited to, mobile terminals such as mobile phones, notebook computers, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablet computers), PMPs (portable multimedia players), car terminals (e.g., car navigation terminals), and the like, and stationary terminals such as digital TVs, desktop computers, and the like.
Yet another embodiment of the present application provides a computer storage medium having a computer program stored thereon, which when run on a computer, causes the computer to perform the corresponding method embodiments described above. Compared with the prior art, the application can realize the authority management of certain data by intercepting the SQL statement generated by Hibernates and modifying the SQL statement so as to realize the authority management of certain data, and realize that the user can only inquire and manage the user information under the department where the user is located. The application can reduce the complexity of data authority control development in WEB application, add the control of the data authority after the development is completed, adjust the data control authority at any time and rarely even do not need to modify the applied code, and can enhance the stability and the robustness of the data authority control. After the data authority control scheme code completes the test, only the defined data strategy is required to be tested and adjusted for the data authority control in the subsequent project.
While the foregoing is directed to the preferred embodiments of the present invention, it will be appreciated by those skilled in the art that changes and modifications may be made without departing from the principles of the invention, such changes and modifications are also intended to be within the scope of the invention.