Data authority control method and device

By intercepting and modifying the SQL statements generated by Hibernate, precise permission control of data rows is achieved, which solves the problem that the RBAC model cannot manage specific data, reduces code intrusion and development complexity, and enhances the stability and robustness of data permission control. sex.

CN112463792BActive Publication Date: 2024-11-29GUANGDONG FINEST PLANNING INFORMATION TECH CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202011318043.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-11-20
Publication Date
2024-11-29
Estimated Expiration
2040-11-20

AI Technical Summary

Technical Problem

The existing role-based access control (RBAC) model is difficult to manage permissions on specific data, and cannot achieve precise control of data rows, resulting in users that may access and modify information that should not be accessed.

Method used

By intercepting the SQL statements generated by Hibernate, segmenting and parsing SQL statements using regular expressions, filtering out table names and adding permission conditions, and reconnecting SQL statement segments to achieve data permission control.

Benefits of technology

It realizes precise permission control over data rows, ensuring that users can only query and manage user information under their own department, reduces code intrusion and development complexity, and enhances the stability and robustness of data permission control.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN112463792B_ABST
    Figure CN112463792B_ABST
Patent Text Reader

Abstract

The present invention discloses a data authority control method and device, and relates to the field of electronic information technology. The method comprises obtaining SQL statements; segmenting SQL statements with defined data authority policies to obtain a plurality of SQL statement segments; traversing the SQL statement segments one by one, and judging whether the SQL statement segment starting with select is the first element after segmentation; if not, segmenting the SQL statement segment again; if so, parsing the table name of the SQL statement segment, screening out the SQL statement segment whose table name defines the data authority policy; replacing the table name of the SQL statement segment whose table name defines the data authority policy with a select statement with added authority conditions, and obtaining a plurality of modified SQL statement segments; reconnecting the modified SQL statement segments to obtain the target SQL statement segment. The present invention intercepts the SQL statement generated by Hibernates and modifies it to achieve data authority management, performs authority management on a certain type of data, and enables users to query and manage only the user information under their own department.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of electronic information technology, and in particular to a data authority control method and device. Background Art

[0002] Data permission control has always been a very important part of the middle and back-end management system. A reasonable permission management strategy can bring great convenience to the system. At present, most developers choose to create permission management modules based on the role-based access control (RBAC) permission model. RBAC is an effective access control method for implementing enterprise security policies. Its basic idea is that various permissions for system operations are not directly granted to specific users, but a role set is established between the user set and the permission set. Each role corresponds to a set of corresponding permissions. Once a user is assigned an appropriate role, the user has all the operation permissions of this role. The advantage of this is that you don't have to assign permissions every time you create a user. You only need to assign the user the corresponding role. In addition, the permission changes of roles are much less than the permission changes of users. This will simplify user permission management and reduce system overhead.

[0003] However, the RBAC model can only manage menu directories or other permissions saved in the database (such as permissions for adding, deleting, modifying, and checking functions), and cannot manage permissions for certain types of data. For example, if a user has the permission for the "User Management" menu, the user can query the information of all users and modify it at will, but ideally the user should only be able to manage the user information of his or her department. Summary of the invention

[0004] The purpose of the present invention is to provide a data authority control method, which can realize accurate control of data rows according to configurable data policies, intercept SQL statements generated by Hibernate and modify SQL statements according to actual configuration to achieve the purpose of data authority control.

[0005] To achieve the above-mentioned purpose, an embodiment of the present invention provides a data permission control method, comprising: obtaining an SQL statement; segmenting an SQL statement with a defined data permission policy to obtain a plurality of SQL statement segments; traversing the SQL statement segments one by one, and determining whether the SQL statement segment starting with select is the first element after segmentation; if not, performing a second segmentation on the SQL statement segment; if so, parsing the table name of the SQL statement segment, and filtering out the SQL statement segment whose table name defines the data permission policy; replacing the table name of the SQL statement segment whose table name defines the data permission policy with a select statement with a permission condition added to obtain a plurality of modified SQL statement segments; and reconnecting the modified SQL statement segments to obtain the target SQL statement segment.

[0006] Specifically, parsing the table name of the SQL statement segment and filtering out the SQL statement segment whose table name defines the data permission policy includes: finding and marking the starting position, interval position and end position of the table in the SQL statement segment; wherein the starting position includes the position of the from keyword, and the end position includes the last index of the SQL statement segment; combining the starting position, interval position and end position to filter out the SQL statement segment whose table name defines the data permission policy.

[0007] Specifically, the searching and marking of the starting position, the interval position and the ending position of the table in the SQL statement segment include: searching for the from keyword using a regular expression; searching for the separator keyword or the separator character using a regular expression; the separator keyword includes left, join and / or on, and the separator character includes a comma; searching for the where, group by, order by and / or having keywords using a regular expression; using 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 where, group by, order by and / or having keywords as the ending position of the table; when any of the where, group by, order by and having keywords is not found, using the last index of the SQL statement segment as the ending position of the table.

[0008] Specifically, the obtaining of SQL statements includes: creating an interceptor to implement the EmptyInterceptor interface provided by Hibernate, and overriding the onPrePareStatement method to intercept the generated SQL statements.

[0009] Specifically, the SQL statement with defined data permission policy is segmented to obtain several SQL statement segments, and the SQL statement with defined data permission policy is segmented. The regular expression used includes the regular expression for judging the start of the subquery: (?!\A)\(\s*[sS][eE][lL][eE][cC][tT][\s|?=\(|*].

[0010] The embodiment of the present invention also provides a data permission control device, including: an acquisition module, used to acquire SQL statements; a segmentation module, used to segment SQL statements with defined data permission policies to obtain a plurality of SQL statement segments; a judgment module, used to traverse the SQL statement segments one by one, and judge whether the SQL statement segment starting with select is the first element after segmentation; a loop module, used to make the SQL statement segment starting with select and being the first element enter the loop for secondary segmentation; a screening module, used to parse the table name of the SQL statement segment that starts with select and is not the first element, and screen out the SQL statement segment whose table name defines the data permission policy; a replacement module, used to replace the table name of the SQL statement segment whose table name defines the data permission policy with a select statement with permission conditions added, to obtain a plurality of modified SQL statement segments; a reshaping module, used to reconnect the modified SQL statement segments to obtain the target SQL statement segment.

[0011] Specifically, the screening module includes: a search and marking module, used to find and mark the starting position, interval position and end position of the table in the SQL statement segment; wherein the starting position includes the position of the from keyword, and the end position includes the last index of the SQL statement segment; a union module, used to combine the starting position, interval position and end position to filter out the SQL statement segment whose table name defines the data permission policy.

[0012] Specifically, the search mark module includes: a starting position search module, which is used to use a regular expression to search for a from keyword; an interval position search module, which is used to use a regular expression to search for a separator keyword or a separator character; the separator keyword includes left, join and / or on, and the separator character includes a comma; an end position search module, which is used to use a regular expression to search for where, group by, order by and / or having keywords; a position mark module, which is used to use 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 where, group by, order by and / or having keywords as the end position of the table; when any of the where, group by, order by and having keywords is not found, the last index of the SQL statement segment is used as the end position of the table.

[0013] An embodiment of the present invention also provides an electronic device, which includes a processor, a memory and a bus; the bus is used to connect the processor and the memory; the memory is used to store operation instructions; and the processor is used to execute the above-mentioned data permission control method by calling the operation instructions.

[0014] An embodiment of the present invention further provides a computer storage medium, which is used to store computer instructions. When the computer storage medium is run on a computer, the computer can execute the above-mentioned data authority control method.

[0015] The embodiments of the present invention have the following beneficial effects:

[0016] The present invention provides a data permission control method and device according to an embodiment of the present invention, wherein the method comprises: obtaining an SQL statement; segmenting an SQL statement with a defined data permission policy to obtain a plurality of SQL statement segments; traversing the SQL statement segments one by one, and determining whether an SQL statement segment starting with select is the first element after segmentation; if not, adding a data permission policy to the SQL statement segment, and segmenting it again; if so, parsing the table name of the SQL statement segment, and filtering out the SQL statement segment whose table name defines the data permission policy; replacing the table name of the SQL statement segment whose table name defines the data permission policy with a select statement with a permission condition added to obtain a plurality of modified SQL statement segments; and reconnecting the modified SQL statement segments to obtain a target SQL statement segment.

[0017] Compared with the prior art, the present invention intercepts the SQL statements generated by Hibernates and modifies them to achieve data permission management, performs permission management on a certain type of data, and enables users to query and manage only the user information under their own department. The code invasiveness of the permission control strategy of the present invention is low, and the method of adding and removing the control strategy is simple, without modifying the original code; the present invention can reduce the complexity of data permission control development in WEB applications, add data permission control after development is completed, and adjust data control permissions at any time with little or no need to modify the application code; the present invention can enhance the stability and robustness of data permission control. After the data permission control scheme code is tested, the subsequent projects only need to test and adjust the defined data strategy for data permission control. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] In order to more clearly illustrate the technical solution of the present invention, the drawings required for use in the implementation mode will be briefly introduced below. Obviously, the drawings described below are only some implementation modes of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0019] Figure 1 It is a flowchart of a data authority control method provided by a certain embodiment of the present invention;

[0020] Figure 2 It is a structural diagram of a data authority control device provided by a certain embodiment of the present invention;

[0021] Figure 3 It is a structural schematic diagram of an electronic device provided by a certain embodiment of the present invention. DETAILED DESCRIPTION

[0022] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0023] It should be understood that the step numbers used in this document are only for convenience of description and are not intended to limit the order in which the steps are executed.

[0024] It should be understood that the terms used in the present specification are only for the purpose of describing specific embodiments and are not intended to limit the present invention. As used in the present specification and the appended claims, unless the context clearly indicates otherwise, the singular forms "a", "an" and "the" are intended to include plural forms.

[0025] The terms “include” and “comprising” indicate the presence of described features, integers, steps, operations, elements and / or components, but do not exclude the presence or addition of one or more other features, integers, steps, operations, elements, components and / or combinations thereof.

[0026] The term "and / or" means and includes any and all possible combinations of one or more of the associated listed items.

[0027] In order to facilitate those skilled in the art to understand the embodiments of the present invention, the relevant technical contents of the embodiments of the present invention are described below.

[0028] The data permission policy indicates what kind of restriction conditions are added to the subject (table or view). For example, when querying the user table, adding the department ID of the department where the currently logged-in user belongs is a data permission policy.

[0029] Hibernate is an open source object-relational mapping framework. It provides a very lightweight object encapsulation for JDBC. It establishes a mapping relationship between POJO and database tables. It is a fully automatic orm framework. Hibernate can automatically generate SQL statements and automatically execute them, allowing Java programmers to use object programming thinking to manipulate the database at will. Hibernate can be used in any occasion where JDBC is used. It can be used in Java client programs and in Servlet / JSP Web applications. The most revolutionary thing is that Hibernate can replace CMP in the JaveEE architecture that uses EJB to complete the task of data persistence.

[0030] Regular expressions (regular expressions, also known as regular expressions, often abbreviated as regex, regexp or RE in the code) are usually used to search and replace text that conforms to a certain pattern (rule). Regular expressions are a logical formula for string operations. They use some pre-defined specific characters and the combination of these specific characters to form a regular string. This regular string is used to express a filtering logic for the string.

[0031] The following are the rule strings used when analyzing SQL statements:

[0032] 1. Regular expression for brackets: [\(|\)]

[0033] 2. Regular expression for table name: [a-zA-Z_.0-9]+

[0034] 3. Regular expression to determine whether the SQL statement starts with SELECT:

[0035] \A(\s*|\()\s*[sS][eE][lL][eE][cC][tT][\s|?=\(|*]

[0036] 4. Regular expression to determine the start of a subquery:

[0037] (?!\A)\(\s*[sS][eE][lL][eE][cC][tT][\s|?=\(|*]

[0038] 5. Regular expression matching the FROM keyword:

[0039] (\s+|[*])[fF][rR][oO][mM](\s+|(?=\())

[0040] 6. Regular expression matching the UPDATE keyword:

[0041] \s*[uU][pP][dD][aA][tT][eE]\s+(?!(\s|[(]))

[0042] 7. Regular expression matching the DELETE keyword:

[0043] \s*[dD][eE][lL][eE][tT][eE]\s+(?!(\s|[(]))

[0044] 8. Regular expression matching DELETE FROM keyword:

[0045] [dD][eE][lL][eE][tT][eE]\s+[fF][rR][oO][mM]\s+(?!(\s|[(]))

[0046] 9. Regular expression matching the WHERE keyword:

[0047] (\)|\s)[wW][hH][eE][rR][eE](\s+|(?=\())

[0048] 10. Regular expression matching the HAVING keyword:

[0049] (\)|\s)[hH][aA][vV][iI][nN][gG](\s+|(?=\())

[0050] 11. Regular expression to match the ORDERBY keyword:

[0051] (\)|\s)[oO][rR][dD][eE][rR]\s+[bB][yY](\s+|(?=\())

[0052] 12. Regular expression matching the GROUP BY keyword:

[0053] (\)|\s)[gG][rR][oO][uU][pP]\s+[bB][yY](\s+|(?=\())

[0054] 13. Regular expression matching FOR UPDATE keyword:

[0055] (\)|\s+)[fF][oO][rR]\s+[uU][pP][dD][aA][tT][eE](\s+|(?=\())

[0056] 14. Regular expression matching UNION keyword:

[0057] (\)|\s+)[uU][nN][iI][oO][nN](\s+|(?=\())

[0058] 15. Regular expression matching UNION ALL keywords:

[0059] (\)|\s+)[uU][nN][iI][oO][nN]\s+[aA][lL][lL](\s+|(?=\())

[0060] 16. Regular expression to match the MINUS keyword:

[0061] (\)|\s+)[mM][iI][nN][uU][sS](\s+|(?=\())

[0062] 17. Regular expression matching the INTERSECT keyword:

[0063] (\)|\s+)[iI][nN][tT][eE][rR][sS][eE][cC][tT](\s+|(?=\())

[0064] 18. Regular expression to match ON keyword:

[0065] (\)|\s+)[oO][nN]\s*

[0066] 19. Regular expression matching JOIN keyword:

[0067] (\)|\s+)[jJ][oO][iI][nN]\s+(?!(\s|[(]))

[0068] 20. Regular expression matching LEFT JOIN keyword:

[0069] (\)|\s+)[lL][eE][fF][tT]\s+[jJ][oO][iI][nN](\s+|(?=\())

[0070] 21. Regular expression matching the RIGHT JOIN keyword:

[0071] (\)|\s+)[rR][iI][gG][hH][tT]\s+[jJ][oO][iI][nN](\s+|(?=\())

[0072] 22. Regular expression matching FULL JOIN keyword:

[0073] (\)|\s+)[fF][uU][lL][lL]\s+[jJ][oO][iI][nN](\s+|(?=\())

[0074] 23. Regular expression matching LEFT OUTER JOIN keyword:

[0075] (\)|\s+)[lL][eE][fF][tT]\s+[oO][uU][tT][eE][rR]\s+[jJ][oO][iI][nN](\s+|(?=\())

[0076] 24. Regular expression to match the RIGHT OUTER JOIN keyword:

[0077] (\)|\s+)[rR][iI][gG][hH][tT]\s+[oO][uU][tT][eE][rR]\s+[jJ][oO][iI][nN](\s+|(? =\())

[0078] 25. Regular expression matching FULL OUTER JOIN keyword:

[0079] (\)|\s+)[fF][uU][lL][lL]\s+[oO][uU][tT][eE][rR]\s+[jJ][oO][iI][nN](\s+|(?=\())

[0080] 26. Regular expression matching INNER JOIN keyword:

[0081] (\)|\s+)[iI][nN][nN][eE][rR]\s+[jJ][oO][iI][nN](\s+|(?=\())

[0082] The data permission control method provided by the embodiment of the present invention will be introduced and explained in detail through several specific embodiments below.

[0083] See also Figure 1 , Figure 1 1 is a flow chart of a data permission control method provided by an embodiment of the present invention. To achieve the above-mentioned purpose, an embodiment of the present invention provides a data permission control method, including:

[0084] Step S110, obtaining SQL statements.

[0085] The means of obtaining SQL statements include: creating an interceptor to implement the EmptyInterceptor interface provided by Hibernate, and overriding the onPrePareStatement method to intercept the generated SQL statements.

[0086] Step S120, segmenting the SQL statement with defined data authority policy to obtain a plurality of SQL statement segments.

[0087] In this embodiment, a regular expression is used to analyze SQL statements and segment SQL statements that define data permission policies. The regular expression used includes a regular expression for determining the start of a subquery:

[0088] (?!\A)\(\s*[sS][eE][lL][eE][cC][tT][\s|?=\(|*].

[0089] For example, a SQL statement is "select ename,job,sal from emp where job=(select jobfrom emp where ename='SCOTT')and sal>(select sal from emp where ename='JAMES')". After being analyzed by regular expression, it is segmented into five segments: the first segment is "select ename,job,salfrom emp where job=("; the second segment is "select job from emp where ename='SCOTT'"; the third segment is ")and sal>("; the fourth segment is "select sal from emp where ename='JAMES'"; the fifth segment is ")". The first segment is the first element after segmentation.

[0090] Step S130, traverse the SQL statement segments one by one, and determine whether the SQL statement segment starting with select is the first element after segmentation.

[0091] If the SQL statement segment starts with select but is not the first element after segmentation, return to step S120; if the SQL statement segment starts with select and is the first element after segmentation, execute step S140.

[0092] The first element after segmentation indicates the first statement among several segments obtained by segmentation using regular expression analysis. This judgment is performed because the SQL statement segment starting with the second select may contain a subquery.

[0093] Step S140, parsing the table name of the SQL statement segment, and filtering out the SQL statement segment whose table name defines the data permission policy.

[0094] First, by determining whether the table name of the SQL statement segment defines a data permission policy, find and mark the start position, interval position, and end position of the table in the SQL statement segment; the start position includes the position of the from keyword, and the end position includes the last index of the SQL statement segment; the start position, interval position, and end position are combined to filter out the SQL statement segments whose table names define data permission policies.

[0095] Step S150, replace the table name of the SQL statement segment that defines the data permission policy with a select statement with permission conditions added, and obtain several modified SQL statement segments. The where clause of this select statement is the data permission policy defined for the current table.

[0096] Step S160, reconnect the modified SQL statement segment to obtain the target SQL statement segment. As a data authority control strategy based on Hibernate, the target SQL statement segment is finally given to Hibernate.

[0097] The method of searching and marking the starting position, the interval position and the end position of the table in the SQL statement segment includes: searching the from keyword by using a regular expression; searching the separator keyword or the separator character by using a regular expression; the separator keyword includes left, join and / or on, and the separator character includes a comma (,); searching the where, group by, order by and / or having keywords by using a regular expression; 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 where, group by, order by and / or having keywords as the end position of the table; and when any of the where, group by, order by and having keywords is not found, taking the last index of the SQL statement segment as the end position of the table.

[0098] The regular expression used to find the from keyword is:

[0099] (\s+|[*])[fF][rR][oO][mM](\s+|(?=\()).

[0100] The purpose of searching for keywords such as where, group by, order by, and having is to find the cutoff position of the possible table. The regular expressions used include:

[0101] Regular expression matching the WHERE keyword:

[0102] (\)|\s)[wW][hH][eE][rR][eE](\s+|(?=\())

[0103] Regular expression matching the HAVING keyword:

[0104] (\)|\s)[hH][aA][vV][iI][nN][gG](\s+|(?=\())

[0105] Regular expression to match the ORDERBY keyword:

[0106] (\)|\s)[oO][rR][dD][eE][rR]\s+[bB][yY](\s+|(?=\())

[0107] Regular expression to match the GROUP BY keyword:

[0108] (\)|\s)[gG][rR][oO][uU][pP]\s+[bB][yY](\s+|(?=\())and so on.

[0109] After finding the from keyword, you need to replace and search for other regular expressions, including:

[0110] Regular expression to match the UNION keyword:

[0111] (\)|\s+)[uU][nN][iI][oO][nN](\s+|(?=\())

[0112] Regular expression to match the UNION ALL keyword:

[0113] (\)|\s+)[uU][nN][iI][oO][nN]\s+[aA][lL][lL](\s+|(?=\())

[0114] Regular expression to match the MINUS keyword:

[0115] (\)|\s+)[mM][iI][nN][uU][sS](\s+|(?=\())

[0116] Regular expression to match the INTERSECT keyword:

[0117] (\)|\s+)[iI][nN][tT][eE][rR][sS][eE][cC][tT](\s+|(?=\())

[0118] Regular expression to match the ON keyword:

[0119] (\)|\s+)[oO][nN]\s*

[0120] Regular expression matching the JOIN keyword:

[0121] (\)|\s+)[jJ][oO][iI][nN]\s+(?!(\s|[(]))

[0122] Regular expression that matches the LEFT JOIN keyword:

[0123] (\)|\s+)[lL][eE][fF][tT]\s+[jJ][oO][iI][nN](\s+|(?=\())

[0124] Regular expression that matches the RIGHT JOIN keyword:

[0125] (\)|\s+)[rR][iI][gG][hH][tT]\s+[jJ][oO][iI][nN](\s+|(?=\())

[0126] Regular expression that matches the FULL JOIN keyword:

[0127] (\)|\s+)[fF][uU][lL][lL]\s+[jJ][oO][iI][nN](\s+|(?=\())

[0128] Regular expression for LEFT OUTER JOIN keyword:

[0129] (\)|\s+)[lL][eE][fF][tT]\s+[oO][uU][tT][eE][rR]\s+[jJ][oO][iI][nN](\s+|(?=\())

[0130] Regular expression for the RIGHT OUTER JOIN keyword:

[0131] (\)|\s+)[rR][iI][gG][hH][tT]\s+[oO][uU][tT][eE][rR]\s+[jJ][oO][iI][nN](\s+|(? =\())

[0132] Regular expression for FULL OUTER JOIN keyword:

[0133] (\)|\s+)[fF][uU][lL][lL]\s+[oO][uU][tT][eE][rR]\s+[jJ][oO][iI][nN](\s+|(?=\())

[0134] Regular expression for INNER JOIN keyword:

[0135] (\)|\s+)[iI][nN][nN][eE][rR]\s+[jJ][oO][iI][nN](\s+|(?=\())

[0136] See also Figure 2 , Figure 2 1 is a schematic diagram of the structure of a data authority control device provided by an embodiment of the present invention. The same parts as the above embodiments in this embodiment are not repeated here. The embodiment of the present invention also provides a data authority control device, including:

[0137] The acquisition module 210 is used to acquire SQL statements;

[0138] A segmentation module 220 is used to segment the SQL statement with defined data permission policy to obtain a plurality of SQL statement segments;

[0139] A judgment module 230, used to traverse the SQL statement segments one by one, and judge whether the SQL statement segment starting with select is the first element after segmentation;

[0140] A loop module 240 is used to make the SQL statement segment starting with select and being the first element enter back to the segmentation module 220 for secondary segmentation;

[0141] The screening module 250 is used to parse the table name of the SQL statement segment that starts with select and is not the first element, and screen out the SQL statement segment whose table name defines the data permission policy;

[0142] A replacement module 260 is used to replace the table name of the SQL statement segment in which the table name defines the data permission policy with a select statement to which the permission condition is added, to obtain a plurality of modified SQL statement segments;

[0143] The reshaping module 270 is used to reconnect the modified SQL statement segments to obtain target SQL statement segments.

[0144] The screening module 250 includes a search and marking module 251 and a union module 252, wherein the search and marking module 251 is used to search and mark the starting position, interval position and end position of the table in the SQL statement segment; wherein the starting position includes the position of the from keyword, and the end position includes the last index of the SQL statement segment; the union module 252 is used to combine the starting position, interval position and end position to screen out the SQL statement segment whose table name defines the data permission policy.

[0145] The search mark module 251 includes: a start position search module, which is used to use a regular expression to search for the from keyword; an interval position search module, which is used to use a regular expression to search for a separator keyword or a separator character; the separator keyword includes left, join and / or on, and the separator character includes a comma; an end position search module, which is used to use a regular expression to search for where, group by, order by and / or having keywords; a position mark module, which is used to use the position of the from keyword as the start position of the table, the separator keyword and / or the separator character as the interval position of the table, and the position of the where, group by, order by and / or having keywords as the end position of the table; when any of the where, group by, order by and having keywords is not found, the last index of the SQL statement segment is used as the end position of the table.

[0146] In an alternative embodiment, an electronic device is provided, such as Figure 3 As shown, Figure 3 The electronic device 300 shown includes: a processor 310 and a memory 330. The processor 310 and the memory 330 are connected, such as through a bus 320. Optionally, the electronic device 300 may also include a transceiver 340. It should be noted that in actual applications, the transceiver 340 is not limited to one, and the structure of the electronic device 300 does not constitute a limitation on the embodiments of the present application.

[0147] The processor 310 may be a CPU, a general-purpose processor, a DSP, an ASIC, an FPGA or other programmable logic device, a transistor logic device, a hardware component or any combination thereof. It may implement or execute various exemplary logic blocks, modules and circuits described in conjunction with the disclosure of this application. The processor 310 may also be a combination that implements computing functions, such as a combination of one or more microprocessors, a combination of a DSP and a microprocessor, etc.

[0148] The bus 320 may include a path to transmit information between the above components. The bus 320 may be a PCI bus or an EISA bus, etc. The bus 320 may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 3 Only one thick line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.

[0149] The memory 330 can be 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, or an EEPROM, a CD-ROM or other optical disk storage, an optical disc storage (including a compressed optical disc, a laser disc, an optical disc, a digital versatile disc, a Blu-ray disc, etc.), a magnetic disk storage medium or other magnetic storage device, or any other medium that can be used to carry or store the desired program code in the form of instructions or data structures and can be accessed by a computer, but is not limited to this.

[0150] The memory 330 is used to store application code for executing the solution of the present application, and the execution is controlled by the processor 310. The processor 310 is used to execute the application code stored in the memory 330 to implement the content shown in any of the above method embodiments.

[0151] Among them, electronic devices include but are not limited to: mobile terminals such as mobile phones, laptops, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablet computers), PMPs (portable multimedia players), vehicle-mounted terminals (such as vehicle-mounted navigation terminals), etc., and fixed terminals such as digital TVs, desktop computers, etc.

[0152] Another embodiment of the present application provides a computer storage medium, on which a computer program is stored, and when the computer is run on a computer, the computer can execute the corresponding content in the aforementioned method embodiment. Compared with the prior art, the present invention intercepts the SQL statement generated by Hibernates and modifies it to achieve data permission management, performs permission management on a certain type of data, and enables users to only query and manage user information under their own department. The code invasiveness of the permission control strategy of the present invention is low, and the method of adding and removing the control strategy is simple, and there is no need to modify the original code; the present invention can reduce the complexity of data permission control development in WEB applications, add data permission control after development is completed, and adjust data control permissions at any time with little or no need to modify the application code; the present invention can enhance the stability and robustness of data permission control. After the data permission control scheme code is tested, the subsequent project only needs to test and adjust the defined data policy for data permission control.

[0153] The above is a preferred embodiment of the present invention. It should be pointed out that a person skilled in the art can make several improvements and modifications without departing from the principle of the present invention. These improvements and modifications are also considered to be within the scope of protection of the present invention.

Claims

1. A data authority control method, characterized in that: include: Get SQL statements; Split the SQL statement with defined data permission policy to obtain several SQL statement segments; The SQL statement with defined data permission policy is segmented to obtain a plurality of SQL statement segments, wherein the SQL statement with defined data permission policy is segmented, and the regular expression used includes a regular expression for determining the start of a subquery: (?!\A)\(\s*[sS][eE][lL][eE][cC][tT][\s|?=\(|*]; Traverse the SQL statement segments one by one, and determine whether the SQL statement segment starting with select is the first element after segmentation; If not, split the SQL statement segment twice; If yes, parse the table name of the SQL statement segment and filter out the SQL statement segment whose table name defines the data permission policy; The table name of the SQL statement segment that defines the data permission policy is replaced with a select statement that adds permission conditions, thereby obtaining several changed SQL statement segments; Reconnect the modified SQL statement segments to obtain the target SQL statement segments; The parsing of the table name of the SQL statement segment to filter out the SQL statement segment whose table name defines the data permission policy includes: Find and mark the starting position, interval position and end position of the table in the SQL statement segment; wherein the starting position includes the position of the from keyword, and the end position includes the last index of the SQL statement segment; The step of searching and marking the start position, the interval position and the end position of the table in the SQL statement segment includes: Use regular expressions to find the from keyword; Using a regular expression to search for a separator keyword or a separator character; the separator keyword includes left, join and / or on, and the separator character includes a comma; Use regular expressions to find where, group by, order by, and / or having keywords; The position of the from keyword is used as the starting position of the table, the delimiter keyword and / or the delimiter character is used as the interval position of the table, and the position of the where, group by, order by and / or having keywords is used as the end position of the table; when any of the where, group by, order by and having keywords is not found, the last index of the SQL statement segment is used as the end position of the table; The start position, the interval position and the end position are combined to filter out the SQL statement segment whose table name defines the data permission policy.

2. The data authority control method according to claim 1, characterized in that: The obtaining of SQL statements includes: creating an interceptor to implement the EmptyInterceptor interface provided by Hibernate, and overriding the onPrePareStatement method to intercept the generated SQL statements.

3. A data authority control device, characterized in that: include: Acquisition module, used to obtain SQL statements; The segmentation module is used to segment the SQL statement with defined data permission policy to obtain several SQL statement segments; The SQL statement with defined data permission policy is segmented to obtain a plurality of SQL statement segments, wherein the SQL statement with defined data permission policy is segmented, and the regular expression used includes a regular expression for determining the start of a subquery: (?!\A)\(\s*[sS][eE][lL][eE][cC][tT][\s|?=\(|*]; A judgment module, used for traversing the SQL statement segments one by one, and judging whether the SQL statement segment starting with select is the first element after segmentation; The loop module is used to split the SQL statement segment that starts with select and is the first element twice; The filtering module is used to parse the table name of the SQL statement segment that starts with select and is not the first element, and filter out the SQL statement segment whose table name defines the data permission policy; A replacement module is used to replace the table name of the SQL statement segment that defines the data permission policy with a select statement that adds permission conditions, and obtain a number of changed SQL statement segments; A reshaping module is used to reconnect the modified SQL statement segments to obtain a target SQL statement segment; The screening module specifically includes: A search and marking module, used to search and mark the starting position, interval position and end position of the table in the SQL statement segment; wherein the starting position includes the position of the from keyword, and the end position includes the last index of the SQL statement segment; The tag search module comprises: The starting position search module is used to search for the from keyword using regular expressions; A separation position search module, used to search for a separator keyword or a separator character using a regular expression; the separator keyword includes left, join and / or on, and the separator character includes a comma; The cut-off position search module is used to search for where, group by, order by and / or having keywords using regular expressions; a position marking module, used to use 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 where, group by, order by and / or having keywords as the end position of the table; when any of the where, group by, order by and having keywords is not found, the last index of the SQL statement segment is used as the end position of the table; The combining module is used to combine the starting position, the interval position and the ending position to filter out the SQL statement segment whose table name defines the data permission policy.

4. An electronic device, characterized in that: It includes: processor, memory, and bus; The bus is used to connect the processor and the memory; The memory is used to store operation instructions; The processor is used to execute the data authority control method described in any one of claims 1 to 2 by calling the operation instruction.

5. A computer storage medium, characterized in that: The computer storage medium is used to store computer instructions, and when it is run on a computer, it enables the computer to execute the data authority control method described in any one of claims 1-2.

Citation Information

Patent Citations

  • Data authority control method based on SQL (Structured Query Language)

    CN104484621A