A unified authentication optimization method for cross-language heterogeneous microservices based on OpenResty
Patent Information
- Application Number
- CN202310294722.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-03-24
- Publication Date
- 2024-11-19
- Estimated Expiration
- 2043-03-24
AI Technical Summary
[0005](1)Kong,基于配置文件方式进行配置,不够灵活;需要较高的开发代价才能和目前已有的Java微服务治理体系打通;内置的Http Basic认证无法与已有用户系统打通
[0042] 1. By forwarding the authentication operation from Spring cloud Gateway or the authentication service to the OpenResty reverse proxy server, the pressure on the Web back-end service is reduced, and the authentication control of the Web front-end static page is realized;
Smart Images

Figure CN116455613B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of microservice technology, and in particular to a cross-language heterogeneous microservice unified authentication optimization method based on OpenResty. Background Art
[0002] Microservices are a system architecture design that splits an originally single system into multiple small services. These small services are independent of each other. Microservices managed by different teams use different technology stacks, including Spring MVC and PlayFramework based on the HTTP protocol, and Motan and Bubbo based on the TCP RPC technology stack. The integration between microservices is often the integration of cross-language heterogeneous systems.
[0003] Different microservices generally have different network addresses. Many microservices have implemented their own authentication logic, but external clients may need to call multiple microservice interfaces to complete a business requirement. In this way, when multiple microservices are called at the same time, how to ensure efficient and secure authentication is a difficult problem, which also brings a great burden to the expansion and maintenance of services. Therefore, unified authentication processing is urgently needed.
[0004] Generally, unified microservice authentication is achieved by extracting the authentication logic scattered in microservices and putting it into a global gateway. The shortcomings of the gateway solutions currently on the market are also quite obvious, as shown below:
[0005] (1) Kong is based on configuration files, which is not flexible enough. It requires a high development cost to integrate with the existing Java microservice governance system. The built-in Http Basic authentication cannot be integrated with the existing user system. It is impossible to dynamically obtain the API information of microservices, and fine-grained control is difficult.
[0006] (2) Spring Cloud Gateway does not provide any authentication function by itself and needs to be integrated with other authentication components, which will cause the project size to become larger and less flexible. After Spring Cloud Gateway is integrated with OAuth2, the business microservices still need to call the authentication service to authenticate the access token, which is too intrusive to the business microservices. In addition, Spring Cloud Gateway is developed based on JVM. If the authentication operation is placed on Spring CloudGateway, the performance is not optimistic. In addition, Spring Cloud Gateway cannot meet the control of front-end page permissions in applications with separated front-end and back-end.
[0007] The modern information office system adopts a development and deployment mode with front-end and back-end separation, and has strict permission control. User permissions need to be controlled at the level of pages, buttons, interfaces, etc. The above open source Kong gateway cannot easily and conveniently implement the control of interface permissions, and the gateway based on Spring Cloud Gateway cannot control the front-end page permissions in the front-end and back-end separation scenario.
[0008] The announcement number is CN108901022B, which discloses a unified authentication method and gateway for microservices. The authentication operation of the request needs to be forwarded to the authentication center for authentication. This operation is time-consuming and the pressure of the request is on the authentication center. Although the patent points out that the authentication result will be cached for a valid period, in the above-mentioned actual strict control permission scenario, the cache of the authentication result is very ineffective. Summary of the invention
[0009] To overcome the shortcomings of the prior art, the purpose of the present invention is to provide a unified authentication optimization method for cross-language heterogeneous microservices based on OpenResty, which pre-places the authentication operation from Spring cloud Gateway or from the authentication service to the OpenResty reverse proxy server; obtains the permission list corresponding to the URI from the security management microservice, and authenticates the request, and determines whether the user's request is authenticated according to the authentication result, and whether to forward the request to the backend microservice.
[0010] To achieve the above object, the present invention is implemented through the following technical solutions:
[0011] A unified authentication optimization method for cross-language heterogeneous microservices based on OpenResty includes users, gateways, and microservice systems. Users access the microservice system through the gateway. The gateway includes an OpenResty reverse proxy server and an OpenResty authentication plug-in. The microservice system includes a security management microservice and a common application microservice. Users make microservice system requests through the gateway. Specifically, the following contents are included:
[0012] 1) The redis memory database is used for shared sessions between the security management microservice and the general application microservice, and HTTP Cookie is used to pass user credentials in the HTTP request header;
[0013] 2) The security management microservice parses the WebAPI of microservices in different languages;
[0014] 3) The security management microservice collects WebAPI interface information after parsing the WebAPI of microservices in different languages, implements user authentication, user, role, and permission management, and provides a full list of URI permissions. After the user logs in and authenticates, the permission information is written into the session. The permissions include API permissions, page permissions, and menu button permissions.
[0015] 4) In the OpenResty reverse proxy server, implement the authentication gateway and pre-place the authentication operation from Spring cloudGateway to the Openresty authentication plug-in in the OpenResty reverse proxy server;
[0016] 5) The OpenResty authentication plug-in authenticates the microservice system request based on the user credentials.
[0017] Security management microservices include the following:
[0018] a) Manage user information and store user information and permission information in redis when the user logs in;
[0019] b) Obtain the WebAPI interfaces published by all microservices in the application microservice system and save relevant information;
[0020] c) Administrators can authorize API interface permissions to users;
[0021] d) Provide a WebAPI interface for querying permissions, which returns the URIs and corresponding permission lists of all WebAPI interfaces published by all microservices in the microservice system.
[0022] The OpenResty authentication plug-in authenticates the HTTP request initiated by the user to the WebAPI interface, which includes the following steps:
[0023] S1. When the OpenResty reverse proxy server receives an HTTP request initiated by a user, the authentication plug-in parses the HTTP request URI and HTTP request header;
[0024] S2. Get the full URI and its permission list, as follows:
[0025] Query all URIs and their corresponding permission lists from the cache. If the cache does not exist or is expired, call the query permission WebAPI interface of the security management microservice to query and cache the validity period;
[0026] S3. Get the permission list corresponding to the URI, the content is as follows:
[0027] Get the full URI and its permission list from step S2, and filter out the permission list of the URI of the HTTP request;
[0028] S4. Determine whether the request URI is public. The content is as follows:
[0029] According to the permission list obtained in step S3, determine whether the URI corresponding to the current HTTP request is public. If it is public, directly release it. If it is not public, execute step S5;
[0030] S5. Determine whether the user is logged in. The content is as follows:
[0031] The user credentials, i.e., the session ID, are obtained from the HTTP request header parsed in step S1. If the user credentials are obtained, it means that the user has logged in. Otherwise, if the user has not logged in, the HTTP request is terminated and the HTTP status code 401 is returned. The Web front-end program jumps to the login page.
[0032] S6. Get user permission information, the content is as follows:
[0033] According to the user credentials, the user session information is obtained from the redis memory database, and the corresponding permissions are parsed, and the user permissions are cached with a validity period; if the acquisition fails, the HTTP request is terminated and the HTTP status code 401 is returned; the Web front-end program jumps to the login page;
[0034] S7. Determine whether the user has the authority to access the URI of the current HTTP request. The content is as follows:
[0035] A judgment is made based on the permission list corresponding to the URI of the current HTTP request in step S3 and the user permission information obtained in step S6. If the user permission list contains the permission list of the URI of the current HTTP request, the current user has the permission to access the current UR and the request is forwarded to the web backend; otherwise, if there is no permission, the HTTP request is terminated and the HTTP status code 403 is returned; the Web front-end program prompts the user that there is no unauthorized access.
[0036] User credentials refer to the session ID passed through cookies, which are data stored on the user's local terminal.
[0037] Ordinary application microservices are the various business microservices that constitute the Web application system.
[0038] The microservice system is a web application system. The OpenResty reverse proxy server forwards user requests to all microservices, but only forwards one specific microservice at a time.
[0039] The authentication operation adopts the whitelist mode. In the whitelist mode, the user must have the permission corresponding to the current URI to access the URI, otherwise the access is denied.
[0040] The web front end refers to the static pages and static files running on the browser; the web back end refers to the server program of the website, which can be a server-side program developed in Java or Python.
[0041] Compared with the prior art, the present invention has the following beneficial effects:
[0042] 1. By moving the authentication operation from Spring cloud Gateway or authentication service to the OpenResty reverse proxy server, the pressure on the Web backend service is reduced, and the authentication control of the Web front-end static pages is realized;
[0043] 2. Implement the parsing of API interfaces of microservices in different languages;
[0044] 3. The API interface of the microservice will be automatically registered in the security management microservice, which can achieve fine-grained authentication and improve the security of the entire system;
[0045] 4. The security management microservice implements the management of users and permissions, and provides a URI permission query interface. When a user logs in, the user's permissions are written into the session;
[0046] 5. The OpenResty authentication plug-in implements authentication of requests when users initiate requests. BRIEF DESCRIPTION OF THE DRAWINGS
[0047] Figure 1 This is a structural diagram of the unified authentication optimization method for cross-language heterogeneous microservices based on OpenResty.
[0048] Figure 2 This is the workflow of the authentication plug-in.
[0049] Figure 3 Schematic diagram of services started in the embodiment.
[0050] Figure 4 Schematic diagram of the permission management page of the project runtime security management microservice in the embodiment.
[0051] Figure 5 It is a schematic diagram of a sub-page of the permission management page of the security management microservice in the embodiment.
[0052] Figure 6 It is a schematic diagram of the Web Api interface management page provided by the imported microservice in the security management microservice in the embodiment.
[0053] Figure 7 It is a schematic diagram of the Web Api interface details page provided by each microservice in the security management microservice in the embodiment.
[0054] Figure 8 This is a schematic diagram of a role management page in a security management microservice in an embodiment. DETAILED DESCRIPTION
[0055] The present invention is described in detail below in conjunction with the accompanying drawings, but it should be noted that the implementation of the present invention is not limited to the following embodiments.
[0056] The following examples are implemented on the premise of the technical solution of the present invention, and provide detailed implementation methods and specific operation processes, but the protection scope of the present invention is not limited to the following examples. The methods used in the following examples are conventional methods unless otherwise specified.
[0057] [Example 1]
[0058] See Figure 1 , Figure 2, a unified authentication optimization method for cross-language heterogeneous microservices based on OpenResty, which is implemented through users, gateways, and microservice systems. Users access the microservice system through the gateway, which includes an OpenResty reverse proxy server and an OpenResty authentication plug-in. The OpenResty reverse proxy server is used to accept client connection requests, then forward the requests to the server where the application is deployed, and return the results obtained from the server where the application is deployed to the client; OpenResty is a high-performance Web platform based on Nginx and Lua, which integrates a large number of sophisticated Lua libraries, third-party modules, and most dependencies; the microservice system includes security management microservices and ordinary application microservices. The microservice system is a Web application system. OpenResty will forward requests to all microservices, but only forward a specific microservice at a time. Ordinary application microservices are the various business microservices that constitute the Web application system. The redis memory database is used to share session information between microservices, and the session information contains the permissions of the current user; users make microservice requests through the gateway. The security management microservice implements the management of users and permissions, and provides a URI permission query interface. When a user logs in, the user's permissions are written into the session. When the user initiates a request, the OpenResty authentication plug-in performs authentication based on the user's credentials (user credentials refer to the session ID transmitted through HTTP cookies). It mainly obtains the permission list of the full URI (the full URI refers to the interface URI and the front-end static page URI provided by all microservices in the application system) from the security management microservice, and then filters out the permissions of the current request. If it is a non-public web page or interface, the current user's information is obtained from the redis memory database and the permission information is extracted for authentication. The authentication operation adopts the whitelist mode, that is, the user must have the permission corresponding to the current URI to access the URI, otherwise access is denied.
[0059] Use the following scheme:
[0060] 1) The redis memory database is used for shared sessions between the security management microservice and the general application microservice, and HTTP Cookie is used to pass user credentials in the HTTP request header. HTTP Cookie is also called Web Cookie or browser Cookie, which is a small piece of data sent by the reverse proxy server to the user's browser and saved locally;
[0061] 2) The security management microservice parses the WebAPI of microservices in different languages;
[0062] 3) The security management microservice collects the Web API interface information after parsing the microservice's Web API, implements user authentication, and manages users, roles, and permissions. It also provides a full list of URI permissions. After the user logs in and authenticates, the permission information is written into the session. The permissions include API permissions, page permissions, and menu button permissions.
[0063] 4) In the OpenResty reverse proxy server, implement the authentication gateway and pre-place the authentication operation from Spring cloudGateway to the Openresty authentication plug-in in the OpenResty reverse proxy server;
[0064] 5) The OpenResty authentication plug-in authenticates the microservice system request based on the user credentials.
[0065] The security management microservice manages and authenticates users in the microservice system, registers and manages the Web API interfaces and static page permissions and authorizations published by all microservices in the microservice system, and provides a permission list interface for permission URIs. User access requests pass through the gateway, which authenticates through the authentication plug-in. If authentication is passed, it is forwarded to the Web backend microservice. If authentication fails, it is returned to the user.
[0066] The specific contents of the security management microservice are as follows:
[0067] a) Collect, store and manage Web API interface information provided by its own microservices and common application microservices;
[0068] b) Manage users, roles, menus, permissions, and perform corresponding authorizations;
[0069] c) User login authentication, which can be database user authentication or integration with a third-party authentication center;
[0070] d) After the user successfully logs in, the corresponding permissions are written into the session information;
[0071] e) Provide a query interface for permissions corresponding to the full URI.
[0072] See Figure 2 , the OpenResty authentication plug-in performs URI parsing and authentication on the HTTP request initiated by the user. The specific steps are as follows:
[0073] S1. When OpenResty receives an HTTP request initiated by a user, the authentication plug-in parses the HTTP request URI and HTTP request header;
[0074] S2. Get the full URI and its permission list, as follows:
[0075] Query all URIs and their corresponding permission lists from the cache. If the cache does not exist or is expired, call the query permission Web API interface of the security management microservice to query and cache the validity period;
[0076] S3. Get the permission list corresponding to the URI (URI stands for Uniform Resource Identifier), the content is as follows:
[0077] Get the full URI and its permission list from step S2, and filter out the permission list of the current URI;
[0078] S4. Determine whether the request URI is public. The content is as follows:
[0079] According to the permission list obtained in step S3, determine whether the URI corresponding to the current HTTP request is public. If it is public, directly release it. If it is not public, execute step S5;
[0080] S5. Determine whether the user is logged in. The content is as follows:
[0081] Get the user credentials. If the user credentials are obtained, it means that the user has logged in. Otherwise, if the user has not logged in, the HTTP request is terminated and the HTTP status code 401 is returned (HTTP status code 401 means that the user has not logged in for authentication). The web front-end program jumps to the login page.
[0082] S6. Get user permission information, the content is as follows:
[0083] According to the user credentials, the user session information is obtained from the redis memory database, and the corresponding permissions are parsed, and the user permissions are cached with a validity period; otherwise, if the user is not logged in, the HTTP request is terminated and the HTTP status code 401 is returned; the web front-end program jumps to the login page;
[0084] S7. Determine whether the user has the authority to access the current URI. The content is as follows:
[0085] According to the permission list corresponding to the URI obtained in step S3 and the user permission information obtained in step S6, if the user permission list contains the permission list of the URI, the current user has the permission to access the current URI and the request is forwarded to the web backend; otherwise, if there is no permission, the HTTP request is terminated and the HTTP status code 403 is returned (HTTP status code 403 indicates that the user has logged in and authenticated, but is not authorized); the web front-end program prompts the user that there is no unauthorized access.
[0086] [Example 2]
[0087] In this embodiment, a unified authentication optimization method for cross-language heterogeneous microservices based on OpenResty is the same as that in Embodiment 1, on the basis of which a description of the working process is added.
[0088] The working process is as follows:
[0089] See Figure 3 , platform-iam-api is the backend interface service of the security management microservice, platform-iam-web is the frontend web service of the security management microservice, and the other itsm-backend and itsm-web are specific business microservices of the application system. Figure 4 , the administrator configures the menu on this page and assigns permissions. Figure 5 , used for menu binding backend Web Api interface page.
[0090] The administrator first Figure 6 and Figure 7 Import the Web API interface of the started microservice in and configure whether the interface is a public interface; then Figure 4 Create a new menu in Figure 5 In the menu, bind the menu to the backend interface permissions; finally, the administrator can Figure 8 In, create a new role and bind it to the role Figure 3 Assign roles to users based on the permissions defined in
[0091] When deploying OpenResty, first load the developed authentication plug-in and configure the parameters of the authentication plug-in.
[0092] When a user initiates an HTTP request and the request reaches OpenResty, the authentication plug-in performs authentication judgment. The specific steps are as follows:
[0093] S1. Determine whether it is a static file, such as js, css file. If it is a static file, release it;
[0094] S2. According to the current Http request, obtain the permissions of the URI corresponding to the Http request, as follows:
[0095] First, get it from the cache. If it is not in the cache, call the interface of the security management microservice to get it.
[0096] S3. According to the URI authority obtained in step S2, first determine whether it is a public Web Api interface, see Figure 7 In the configuration, if it is public, it is released;
[0097] S4. Get the session ID according to the current Http request. If the acquisition fails, it means that the current user is not logged in, and the current request is terminated.
[0098] S5. Get the permissions of the current user based on the session ID. Figure 8 Authorization, if the permission acquisition fails, the current request is terminated and HTTP status 401 is returned, indicating that openresty authentication failed: Failed to obtain the permission in the session according to the session ID;
[0099] S6. Determine whether the user has the permission based on the user permission list and the URI permission list. If not, the HTTP status is 403; if the user has the permission, the request is forwarded to the corresponding microservice.
[0100] The present invention reduces the pressure of Web backend services by prepending authentication operations from Spring cloud Gateway or authentication services to the OpenResty reverse proxy server, realizes authentication control of Web front-end static pages, realizes parsing of API interfaces of microservices in different languages, and the API interfaces of microservices are automatically registered in the security management microservice, which can realize fine-grained authentication and improve the security of the entire system. The security management microservice realizes the management of users and permissions, provides a URI permission query interface, and writes the permissions owned by the user into the session when the user logs in. The OpenResty authentication plug-in realizes the authentication of the request when the user initiates the request.
Claims
1. A unified authentication optimization method for cross-language heterogeneous microservices based on OpenResty, including users, gateways, and microservice systems. Users access the microservice system through the gateway, characterized in that: The gateway includes the OpenResty reverse proxy server and the OpenResty authentication plug-in. The microservice system includes security management microservices and common application microservices. Users make microservice system requests through the gateway. Specifically, it includes the following: 1) The security management microservice and the general application microservice use the redis memory database for shared sessions, and use HTTP Cookie to pass user credentials in the HTTP request header; 2) The security management microservice parses the WebAPI of microservices in different languages; 3) The security management microservice collects WebAPI interface information after parsing the WebAPI of microservices in different languages, implements user authentication, user, role, and permission management, and provides a full list of URI permissions. After the user logs in and authenticates, the permission information is written into the session. The permissions include API permissions, page permissions, and menu button permissions. 4) In the OpenResty reverse proxy server, implement the authentication gateway and pre-place the authentication operation from Spring cloudGateway to the Openresty authentication plug-in in the OpenResty reverse proxy server; 5) The OpenResty authentication plug-in authenticates the microservice system request based on the user credentials; The OpenResty authentication plug-in authenticates the HTTP request initiated by the user to the WebAPI interface, specifically including the following steps: S1. When the OpenResty reverse proxy server receives an HTTP request initiated by a user, the authentication plug-in parses the HTTP request URI and HTTP request header; S2. Get the full URI and its permission list, as follows: Query all URIs and their corresponding permission lists from the cache. If the cache does not exist or is expired, call the query permission WebAPI interface of the security management microservice to query and cache the validity period; S3. Get the permission list corresponding to the URI, the content is as follows: Get the full URI and its permission list from step S2, and filter out the permission list of the URI of the HTTP request; S4. Determine whether the request URI is public. The content is as follows: According to the permission list obtained in step S3, determine whether the URI corresponding to the current HTTP request is public. If it is public, directly release it. If it is not public, execute step S5; S5. Determine whether the user is logged in. The content is as follows: The user credentials, i.e., the session ID, are obtained from the HTTP request header parsed in step S1. If the user credentials are obtained, it means that the user has logged in. Otherwise, if the user has not logged in, the HTTP request is terminated and the HTTP status code 401 is returned. The Web front-end program jumps to the login page. S6. Get user permission information, the content is as follows: According to the user credentials, the user session information is obtained from the redis memory database, and the corresponding permissions are parsed, and the user permissions are cached with a validity period; if the acquisition fails, the HTTP request is terminated and the HTTP status code 401 is returned; the Web front-end program jumps to the login page; S7. Determine whether the user has the authority to access the URI of the current HTTP request. The content is as follows: A judgment is made based on the permission list corresponding to the URI of the current HTTP request in step S3 and the user permission information obtained in step S6. If the user permission list contains the permission list of the URI of the current HTTP request, the current user has the permission to access the current UR and the request is forwarded to the Web backend; otherwise, if there is no permission, the HTTP request is terminated and the HTTP status code 403 is returned; the Web front-end program prompts the user that there is no unauthorized access.
2. According to claim 1, a unified authentication optimization method for cross-language heterogeneous microservices based on OpenResty is characterized in that: The security management microservice specifically includes the following contents: a) Manage user information and store user information and permission information in redis when the user logs in; b) Obtain the WebAPI interfaces published by all microservices in the microservice system and save relevant information; c) Administrators can authorize API interface permissions to users; d) Provide a WebAPI interface for querying permissions, which returns the URIs and corresponding permission lists of all WebAPI interfaces published by all microservices in the microservice system.
3. According to the OpenResty-based cross-language heterogeneous microservice unified authentication optimization method of claim 1, it is characterized in that: The user credentials are session IDs transmitted via cookies, which are data stored on the user's local terminal.
4. According to the OpenResty-based cross-language heterogeneous microservice unified authentication optimization method of claim 1, it is characterized in that: The common application microservices are the various business microservices that constitute the Web application system.
5. According to the OpenResty-based cross-language heterogeneous microservice unified authentication optimization method of claim 1, it is characterized in that: The microservice system is a Web application system. The OpenResty reverse proxy server forwards user-initiated requests for all microservices, but only forwards one specific microservice at a time.
6. According to the OpenResty-based cross-language heterogeneous microservice unified authentication optimization method of claim 1, it is characterized in that: The authentication operation adopts a whitelist mode, in which a user must have the authority corresponding to the current URI to access the URI, otherwise access is denied.
7. According to claim 1, a unified authentication optimization method for cross-language heterogeneous microservices based on OpenResty is characterized in that: The Web front end refers to the static pages and static files running on the browser side; the Web back end refers to the server program of the website, which can be a server-side program developed in Java or Python.
Citation Information
Patent Citations
A unified authentication method and gateway for microservices
CN108901022B
An e-commerce platform implementing system and method based on micro-service technology
CN109284089A
Storage device cluster based on micro-service architecture
CN110381112A
Application service access method and device
CN111209578A
System for requesting micro-service authentication optimization by upper application
CN112333201A
