Micro-service anti-replay implementation method, system and electronic device
By using a one-time anti-replay token mechanism between the API gateway and the client, the problem of coupling between replay attacks and anti-replay logic in existing technologies is solved, achieving more secure and efficient anti-replay protection.
Patent Information
- Application Number
- CN202611170988.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-08-04
- Publication Date
- 2026-11-10
AI Technical Summary
Existing technologies cannot effectively prevent replay attacks, and there is a risk of replay within a time window. The anti-replay verification logic is highly coupled with the business code, lacks a unified control entry point, and the unique identifier requested is easily stolen, resulting in insufficient security.
A one-time anti-replay token mechanism is adopted. The API gateway intercepts the request and performs anti-replay verification. The client pre-fetches the token and carries it in the request. The API gateway verifies the token based on the session identifier and the token set to ensure the uniqueness and security of the token.
It achieves more thorough replay protection, reduces development and modification costs, improves high-concurrency performance, meets high-security business needs, and prevents replay attacks.
Smart Images

Figure CN123074772A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of network security technology, specifically relating to a microservice anti-replay implementation method, system, and electronic device. Background Technology
[0002] In digital business scenarios within the financial industry, operations such as fund transfers, payments, and data submissions are typically completed through messages transmitted between the client and the server. Since these messages may be intercepted by attackers during network transmission, attackers can repeatedly send the intercepted messages to the server to launch a replay attack. If the server lacks effective replay protection, it will process duplicate requests repeatedly, leading to security issues such as user financial losses, duplicate data submissions, and abnormal business logic.
[0003] Currently, there are existing technologies that provide anti-replay verification for interface requests. These technologies work by including a timestamp and a unique request identifier in the request. The server then performs a combined verification of the validity of the timestamp and whether the unique request identifier has already been used, thus achieving basic anti-replay protection.
[0004] However, the aforementioned existing technologies cannot guarantee that a request will be processed only once, and there is still a risk of replay within the time window, resulting in insufficient security strength against replay. Furthermore, their anti-replay verification logic is highly coupled with business code, lacking a unified control entry point and an effective identifier consumption mechanism, leading to problems such as high implementation and maintenance costs, easy omissions in protection, and easy theft of unique request identifiers. Summary of the Invention
[0005] To address the aforementioned problems in the existing technology, this invention provides a microservice replay protection method, system, and electronic device. The technical problem to be solved by this invention is achieved through the following technical solution: In a first aspect, the present invention provides a microservice anti-replay implementation method, applied to an API gateway, wherein the API gateway is deployed on the server side; The method includes: Intercept the received requests and determine the target request from them, and obtain the anti-replay token and session identifier carried in the target request; Based on the session identifier, a target token set associated with the session identifier is determined; wherein, the target token set includes: multiple one-time anti-replay tokens generated by the server in response to the client's token acquisition request, based on the session identifier; Remove the one-time anti-replay token that matches the anti-replay token from the target token set; If the removal is successful, the target request is forwarded to the business service; if the removal fails, the target request is intercepted.
[0006] In one embodiment of the present invention, the step of removing a one-time anti-replay token that matches the anti-replay token from the target token set includes: One-time anti-replay tokens that match the anti-replay token are removed from the target token set through atomic operations.
[0007] In one embodiment of the present invention, the step of intercepting a received request and determining a target request therefrom includes: Intercept received requests; For each request, determine whether its request path matches the preset anti-replay path list; If the requested path does not match the preset anti-replay path list, the request will be forwarded to the business service. If the request path matches the preset anti-replay path list, the request is identified as the target request, and the anti-replay token and session identifier carried in the target request are obtained.
[0008] In one embodiment of the present invention, the target token set is a Set type set stored in Redis, and the key corresponding to the Set type set is formed by concatenating the session identifier and a preset identifier; wherein... The preset identifier is a fixed string used to identify the anti-replay tokens stored in the Set type collection, and the multiple one-time anti-replay tokens are universally unique identification codes generated according to a preset number.
[0009] Secondly, this invention provides a microservice anti-replay implementation method, applied to a client, comprising: After sending a token acquisition request carrying a session identifier to the server, the system receives multiple one-time anti-replay tokens generated and returned by the server based on the session identifier, and stores the multiple one-time anti-replay tokens into the token pool. In response to the initiation of the request, determine whether the request path of the request matches the preset anti-replay path list; If the request path matches the preset anti-replay path list, the request is identified as the target request, a one-time anti-replay token is retrieved from the token pool, and the retrieved one-time anti-replay token is deleted from the token pool. The retrieved one-time anti-replay token is carried in the target request and sent to the server so that the server can perform anti-replay verification on the target request based on the one-time anti-replay token.
[0010] In one embodiment of the present invention, the token pool is stored in Session Storage; The step of retrieving a one-time anti-replay token from the token pool includes: retrieving a one-time anti-replay token from the token pool through a locking mechanism.
[0011] In one embodiment of the invention, before the step of retrieving a one-time anti-replay token from the token pool, the method further includes: If no available one-time anti-replay tokens are available in the token pool, multiple one-time anti-replay tokens are acquired and stored in the token pool.
[0012] In one embodiment of the present invention, the step of sending the retrieved one-time anti-replay token to the server along with the target request includes: Write the retrieved one-time anti-replay token into the rev field of the request header of the target request, and send the target request to the server.
[0013] Thirdly, the present invention provides a microservice anti-replay implementation system, comprising: The token service module is used to generate multiple one-time anti-replay tokens and associate the generated one-time anti-replay tokens with the session identifier to store them as a target token set; The client interaction module is used to obtain multiple one-time anti-replay tokens generated by the token service module and store them in the token pool; in response to the initiation of a request, it determines whether the request path of the request matches the preset anti-replay path list; if the request path matches the preset anti-replay path list, the request is identified as the target request, a one-time anti-replay token is retrieved from the token pool, and the retrieved one-time anti-replay token is deleted from the token pool; the retrieved one-time anti-replay token is written into the request header field of the target request, and the session identifier is carried in the target request and sent to the API gateway module; The API gateway module is used to intercept received requests, obtain the anti-replay token and session identifier carried in the target request; determine the target token set associated with the session identifier based on the session identifier; remove a one-time anti-replay token that matches the anti-replay token from the target token set; if the removal is successful, forward the target request to the business service; if the removal fails, intercept the target request.
[0014] Fourthly, the present invention also provides an electronic device, comprising: a memory and a processor, wherein the memory stores a computer program, and the computer program, when executed by the processor, implements the microservice anti-replay implementation method as described in the first aspect, or the microservice anti-replay implementation method as described in the second aspect.
[0015] Compared with the prior art, the beneficial effects of the present invention are as follows: This application employs a one-time token approach that is destroyed immediately upon use, effectively suppressing replay attacks based on used tokens. Compared to existing technologies, this provides more thorough and secure protection. Anti-replay verification is performed through an API gateway, achieving zero-intrusion, low-coupling, and easy maintenance, significantly reducing development and modification costs. Session isolation is achieved by binding the anti-replay token to session storage; the number of interactions is reduced through client-side batch pre-fetching and token pool caching; and global unified management is achieved through a pre-defined anti-replay path list. This results in superior high-concurrency performance and more unified management, meeting the needs of high-security business scenarios (such as the financial industry).
[0016] The present invention will be further described in detail below with reference to the accompanying drawings and embodiments. Attached Figure Description
[0017] Figure 1 This is a timing diagram illustrating the interaction between the server and the client provided in an embodiment of the present invention; Figure 2 This is a flowchart of a microservice anti-replay implementation method for API gateways provided in an embodiment of the present invention; Figure 3 This is a flowchart of a microservice anti-replay implementation method applied to a client, provided by an embodiment of the present invention; Figure 4 This is a schematic diagram of the microservice anti-replay implementation system provided in an embodiment of the present invention; Figure 5 This is a schematic diagram of the structure of the electronic device provided in an embodiment of the present invention. Detailed Implementation
[0018] To facilitate understanding of this application, existing anti-replay implementation schemes will be introduced first.
[0019] Existing technologies use a combination of timestamps and unique request identifiers to prevent API replay attacks. The specific implementation process is as follows: On the client side, when a client initiates a request, it first generates a timestamp for the current request, which can be accurate to the second or millisecond. Then, it generates a one-time random unique identifier for the request, which is usually a universally unique identifier, a random string, or an auto-incrementing sequence number. Next, it puts the timestamp and the unique request identifier into the request header or request parameters, or it adds the timestamp, the unique request identifier, and business parameters together in the signature to ensure the integrity of the message.
[0020] On the server side, after receiving a request, the server first obtains the timestamp in the request and compares it with the server's current time. If the difference between the two exceeds a preset time window (e.g., 30 seconds, 60 seconds, or 5 minutes), the request is rejected directly. Otherwise, if the timestamp passes the verification, the server queries the cache or database to see if the unique identifier of the request already exists. If it exists, the request is determined to be a replay request and rejected. If it does not exist, the unique identifier of the request is stored in the cache or database and marked as "used," and then the corresponding business logic is executed.
[0021] However, during the research process, the inventors discovered that the above solution had at least the following problems: 1. Timestamp verification can only limit the validity of the request within a preset time window. Attackers can still send the same request repeatedly within the time window, which cannot guarantee that the request is processed only once, and the security strength of the anti-replay function is insufficient. 2. The anti-replay verification logic needs to be implemented intrusively in the business layer of each business service, which is highly coupled with the business code, resulting in high development and maintenance costs. 3. Each business service implements anti-replay verification independently, lacking a unified entry point to intercept requests, making it impossible to achieve global unified control over all interfaces, which can easily lead to protection omissions. 4. The unique identifier requested does not have a one-time consumption mechanism after use and is not associated with or isolated from the user session, which poses a risk of being stolen across sessions and results in insufficient security.
[0022] To address the aforementioned issues, this invention provides a method for preventing replay attacks in microservices.
[0023] Figure 1 This is a timing diagram illustrating the interaction between the server and the client provided in an embodiment of the present invention. For example... Figure 1As shown, the method comprises four stages: In the first stage, when the page initializes or is idle, the client pre-obtains a set of one-time anti-replay tokens from the server and stores them in a local token pool, such as in the browser's session storage. In the second stage, when the client initiates a target request, it first determines whether the request path of the target request matches the preset anti-replay path list. If it does, it retrieves a one-time anti-replay token from the local token pool, adds it to the request header, immediately deletes the local record, and then sends the request to the server. It should be noted that the target request refers to a business request initiated by the client, such as a transfer request, payment request, or order submission request in the financial industry, and does not include token acquisition requests initiated by the client to obtain a one-time anti-replay token. In the third stage, after the API gateway intercepts the target request, it determines the corresponding target token set based on the session identifier in the target request and checks whether the anti-replay token exists in the target token set. Finally, if the verification passes, the API gateway immediately destroys the anti-replay token from the cached target token set to ensure that it cannot be used a second time, and then forwards the target request to the downstream business service for processing; otherwise, if the verification fails, the target request is directly intercepted.
[0024] The specific implementation details on the API gateway side and the client side are explained in detail below.
[0025] Figure 2 This is a flowchart of a microservice anti-replay implementation method applied to an API gateway, provided by an embodiment of the present invention. For example... Figure 1 and Figure 2 As shown, this embodiment of the invention provides a microservice anti-replay method, applied to an API gateway, which is deployed on the server side; The above methods include: Step S101: Intercept the received request and determine the target request from it, and obtain the anti-replay token and session identifier carried in the target request.
[0026] Specifically, the API gateway serves as the unified entry point for receiving client requests from the server. It intercepts received requests and, for each request, determines whether its request path matches a pre-defined anti-replay path list, which is read from the gateway configuration. If the request path does not match the pre-defined anti-replay path list, it means the request does not require anti-replay protection, and the API gateway does not perform anti-replay verification on the request, directly forwarding it to the business service. If the request path matches the pre-defined anti-replay path list, the request is identified as the target request, and the anti-replay token and session identifier carried in the target request are obtained for subsequent verification.
[0027] Furthermore, the API gateway needs to perform parameter integrity verification when obtaining the anti-replay token and session identifier: It obtains the value of the `rev` field in the request header of the target request as the anti-replay token. If the target request lacks the anti-replay token (i.e., the `rev` field in the request header is empty), the API gateway intercepts the target request and can return an HTTP 400 error indicating "Missing anti-replay credential"; it obtains the `LSESSION` in the cookie of the target request as the session identifier. If the target request lacks the session identifier, the API gateway intercepts the target request and can return an HTTP 401 error indicating "User not logged in".
[0028] Step S102: Determine the target token set associated with the session identifier based on the session identifier; wherein, the target token set includes: multiple one-time anti-replay tokens generated by the server in response to the client's token acquisition request based on the session identifier.
[0029] Specifically, the API gateway generates keys corresponding to the target token set according to preset rules based on the session identifier carried in the target request. The key is obtained by concatenating the actual value of the session identifier with the preset identifier. For example, the API gateway concatenates the actual value of the session identifier with the preset identifier ":uuid_set" to generate the key corresponding to the target token set, and then locates the Set type set in Redis based on the generated key. This Set type set is the target token set associated with the session identifier. The preset identifier ":uuid_set" is a fixed string used to identify the target token set. By concatenating the actual value of the session identifier with the preset identifier, the target token sets corresponding to different sessions can be isolated from each other, and conflicts between the keys corresponding to the target token set and the keys corresponding to other business caches can be avoided.
[0030] The target token set is established by the server's token service through processing token acquisition requests sent by clients. For example... Figure 1As shown, the token service receives token acquisition requests from clients, such as calls to the interface " / api / get-tokens", and retrieves the session identifier from the request. The token service determines the associated target token set based on the session identifier. If the target token set does not exist, or the number of available one-time anti-replay tokens in the target token set is lower than a preset replenishment threshold, the service generates multiple one-time anti-replay tokens (e.g., 10) and writes them into the target token set. The one-time anti-replay token can be a universally unique identifier. Subsequently, the token service returns the generated one-time anti-replay tokens to the client, allowing the client to store the received tokens in the token pool, thus completing the acquisition and replenishment of one-time anti-replay tokens. Because the key of the target token set contains the session identifier, target token sets corresponding to different sessions are isolated from each other. A one-time anti-replay token for any given session can only be verified through the target token set corresponding to that session identifier. Finally, the token service sets a set validity period for the target token set, such as 30 minutes; the target token set and its corresponding key are automatically cleared when the set validity period expires, thereby preventing the infinite accumulation of cached data from causing memory consumption, and limiting the usable period of one-time anti-replay tokens.
[0031] It should be noted that the specific values of the validity period, preset quantity, and preset identifier mentioned above are all examples. Those skilled in the art can adjust them according to actual business and security needs. This embodiment does not limit them in this regard.
[0032] Step S103: Remove the one-time anti-replay token that matches the anti-replay token from the target token set.
[0033] Specifically, matching the anti-replay token means that the token values are equal, that is, the target token set contains a UUID string that is the same as the anti-replay token carried in the target request. If the one-time anti-replay token that matches the anti-replay token is successfully removed, it indicates that the one-time anti-replay token exists in the target token set and is being used for the first time; conversely, if the removal fails, it means that the anti-replay token is invalid, its target token set has been cleared, or the anti-replay token has already been used, and the target request may be a replay request. In this case, the API gateway intercepts the target request and returns the corresponding error response.
[0034] In one embodiment, step S103 is executed through an atomic operation. An atomic operation is an indivisible operation, meaning that it cannot be interrupted by other concurrent operations during execution, and its execution result has only two states: "completed" and "not executed at all." For example, Redis's SREM command is an atomic operation used to remove specified elements from a Set type collection and return the actual number of elements removed. Based on the SREM command, one can directly attempt to remove a one-time anti-replay token that matches the anti-replay token, and determine whether the removal was successful based on the returned removal count. Alternatively, the instructions "determine if a matching one-time anti-replay token exists in the target token set" and "remove a one-time anti-replay token if it exists" can be written in the same Lua script and executed by Redis in one go. Since Redis's execution of Lua scripts is atomic, the script as a whole also constitutes an atomic operation. It is important to emphasize that by removing elements through atomic operations, it is guaranteed that only one of the target requests carrying the same anti-replay token will be successfully removed when two target requests arrive concurrently.
[0035] Step S104: If the removal is successful, the target request is forwarded to the business service; if the removal fails, the target request is intercepted.
[0036] In step S104, if the removal is successful, it means that the anti-replay token is genuine and valid and is being used for the first time. The target request is a legitimate request that has arrived for the first time. Therefore, the API gateway forwards the target request to the downstream specific business service for processing. If the removal fails, it means that the anti-replay token is invalid, expired, or has been used before. The target request is a replay request or an illegal request. In this case, the API gateway intercepts the target request and returns an HTTP 403 error, indicating "illegal request or duplicate submission".
[0037] Therefore, in this embodiment, once the anti-replay token passes verification, it is removed from the target token set, ensuring that the same one-time anti-replay token can only pass once, thereby reducing the risk that the same one-time anti-replay token will be submitted repeatedly and the corresponding target request will repeatedly enter the business service.
[0038] Figure 3 This is a flowchart of a microservice anti-replay implementation method applied to a client, provided by an embodiment of the present invention. For example... Figure 3 As shown, this embodiment of the invention provides a microservice anti-replay method, applied to a client, including: Step S201: After sending a token acquisition request carrying a session identifier to the server, receive multiple one-time anti-replay tokens generated and returned by the server based on the session identifier, and store the multiple one-time anti-replay tokens into the token pool.
[0039] Specifically, when the page initializes or is idle, the client sends a token acquisition request carrying the current session identifier to the server, for example, by calling the token acquisition interface " / api / get-tokens" of the token service. Upon receiving the token acquisition request, the server determines the associated target token set based on the current session identifier. If the target token set does not exist, or the number of available one-time anti-replay tokens in the target token set is lower than a preset replenishment threshold, the server generates multiple one-time anti-replay tokens according to a preset number, writes the generated one-time anti-replay tokens into the target token set, and returns the generated one-time anti-replay tokens to the client. The client receives the one-time anti-replay tokens returned by the server and stores them in a token pool, for example, in an array in the browser's session storage with the key "uuid_token_pool". The one-time anti-replay tokens generated by the server are associated with and stored in the target token set corresponding to the current session identifier for anti-replay verification of subsequent target requests. In addition, the client can send a configuration retrieval request to the server when the page loads to obtain a list of request paths for which anti-replay verification needs to be enabled, such as " / api / transfer" and " / api / order". The client can then store the list of request paths as a preset anti-replay path list in the browser's session storage, for example, with "replay_protect_urls" as the key.
[0040] Step S202: In response to the initiation of the request, determine whether the request path matches the preset anti-replay path list.
[0041] Specifically, the client uses a request interceptor (such as an Axios interceptor or a Fetch interceptor) to intercept the request before it is sent and determines whether the request path matches a preset anti-replay path list. If the request path does not match the preset anti-replay path list, it means that the request does not require anti-replay protection, and the client directly allows the request; if the request path matches the preset anti-replay path list, the request is identified as the target request, and the process proceeds to step S203.
[0042] Step S203: If the request path matches the preset anti-replay path list, the request is identified as the target request, a one-time anti-replay token is retrieved from the token pool, and the retrieved one-time anti-replay token is deleted from the token pool.
[0043] The client checks the status of the token pool in Session Storage: If a usable one-time anti-replay token is available in the token pool, the client retrieves one from the token pool, for example, by popping a UUID from the end of the array as the credential for the target request. At the same time, the retrieved token is deleted from the token pool and the array in Session Storage is updated. If no usable one-time anti-replay token is available in the token pool (e.g., the array does not exist or the array is empty), the client first obtains multiple one-time anti-replay tokens from the server and stores them in the token pool, and then retrieves one one-time anti-replay token from the token pool. This ensures that a one-time anti-replay token will not be used twice on the client's local machine.
[0044] In one embodiment, the client retrieves a one-time anti-replay token from the token pool using a locking mechanism. The locking mechanism is a synchronization mechanism used in concurrent scenarios to ensure that the same resource is accessed by only one execution unit at a time, ensuring that multiple tabs or multiple concurrent requests do not retrieve the same one-time anti-replay token from the token pool.
[0045] Step S204: Send the retrieved one-time anti-replay token along with the target request to the server so that the server can perform anti-replay verification on the target request based on the one-time anti-replay token.
[0046] Specifically, the client writes the retrieved one-time anti-replay token into the rev field of the request header of the target request. At the same time, the browser automatically carries the LSESSION (i.e., session identifier) in the cookie in the target request. Then, the client sends the target request to the server, which performs anti-replay verification on the target request based on the one-time anti-replay token. The specific verification process is described in the API gateway side implementation example, and will not be repeated here.
[0047] Furthermore, in actual operation, the interaction between the client and the server may be affected by factors such as the remaining quantity of one-time anti-replay tokens and page status, leading to some edge cases. The following sections, using specific scenarios, illustrate how embodiments of the present invention handle various edge cases.
[0048] Firstly, regarding the exhaustion of one-time replay protection tokens: If the client's token pool has run out of one-time replay protection tokens, in response to the initiation of the next target request, the client will wait synchronously, first retrieving multiple one-time replay protection tokens from the server and storing them in the token pool, then retrieving one-time replay protection tokens from the token pool before sending the target request. To prevent synchronous calls from blocking for too long, a maximum timeout for retrieving one-time replay protection tokens can be set. After the timeout, the request will be treated as a failure and the user will be notified.
[0049] Secondly, regarding page refresh or closing. Since the token pool is stored in Session Storage, the one-time replay protection tokens are automatically cleared when the page is closed. Reopening the page triggers a new one-time replay protection token acquisition process; furthermore, because the keys corresponding to the old target token set in Redis have expired and been cleared, or the old token set is stored bound to the old session, the old one-time replay protection tokens cannot be reused in the new session, thus avoiding the risk of one-time replay protection tokens being leaked and exploited.
[0050] Thirdly, regarding multi-tab concurrency. If multiple tabs are opened in the same browser, they may simultaneously retrieve one-time replay protection tokens from the token pool, posing a risk of obtaining the same token. To address this, the client retrieves the one-time replay protection token from the token pool using the aforementioned locking mechanism, ensuring that different tabs and concurrent requests do not obtain the same UUID. On the server side, since the API gateway's removal operation is atomic, even in extreme cases where the same one-time replay protection token is carried by multiple concurrent requests, only one request will successfully remove it.
[0051] Figure 4 This is a schematic diagram of the microservice anti-replay implementation system provided in an embodiment of the present invention. Figure 4 As shown, this embodiment of the invention also provides a microservice anti-replay implementation system 400, comprising: The token service module 401 is used to generate multiple one-time anti-replay tokens and associate the generated multiple one-time anti-replay tokens with the session identifier and store them as a target token set; The client interaction module 402 is used to obtain multiple one-time anti-replay tokens generated by the token service module and store them in the token pool; in response to the initiation of a request, it determines whether the request path of the request matches the preset anti-replay path list; if the request path matches the preset anti-replay path list, the request is identified as the target request, a one-time anti-replay token is retrieved from the token pool, and the retrieved one-time anti-replay token is deleted from the token pool; the retrieved one-time anti-replay token is written into the request header field of the target request, and the session identifier is carried in the target request and sent to the API gateway module 403; API Gateway Module 403 is used to intercept received requests, obtain the anti-replay token and session identifier carried in the target request; determine the target token set associated with the session identifier based on the session identifier; remove the one-time anti-replay token that matches the anti-replay token from the target token set; if the removal is successful, forward the target request to the business service; if the removal fails, intercept the target request.
[0052] The client interaction module 402 is deployed on the client side, which may be a browser front-end application running on a user's terminal device. The client interaction module 402 is responsible for maintaining the anti-replay path list; this list refers to a pre-configured collection of request paths that require anti-replay verification, such as interface paths like " / api / transfer" and " / api / order". Requests whose paths match this list need to undergo anti-replay verification, while requests that do not match are allowed to proceed. The client interaction module 402 also manages a local token pool, which is a storage area maintained locally on the client side for caching multiple one-time anti-replay tokens obtained in batches from the server. For example, it may be an array stored in the browser's SessionStorage with the key "uuid_token_pool". Session Storage is a session-level local storage mechanism provided by the browser, and the stored data is valid only within the current browser session. The client interaction module 402's handling of the token pool includes preloading, retrieving, and automatically replenishing one-time anti-replay tokens when they are exhausted. In addition, the client interaction module 402 is responsible for injecting an anti-replay token into the request header of the HTTP request. The anti-replay token is a credential used to prevent the request from being processed repeatedly. It is generated by the server and carried by the client when initiating the request. The server uses this token to determine whether the request is arriving for the first time. For example, the anti-replay token can be a universally unique identifier UUID "550e8400-e29b-41d4-a716-446655440000". In this embodiment of the invention, the anti-replay token is specifically written into the rev field of the request header and is used to transmit the anti-replay token to the server.
[0053] The token service module 401 is deployed on the server side and is responsible for generating batch one-time anti-replay tokens bound to specific user sessions. It also provides token storage, status query, and automatic expiration cleanup functions. A one-time anti-replay token is a token that can only be used once; once used (and verified), it is destroyed and cannot be used a second time. This invention achieves one-time token consumption by removing it from the set, rather than simply marking it as "used" as in existing technologies. It should be noted that the token service can be deployed independently of the API module 403 gateway, for example, as a standalone microservice; or it can be integrated as a functional module into the API module 403 gateway or other server-side components. This embodiment of the invention does not limit the specific deployment form of the token service. In this embodiment, "server-side" refers to the client-side counterpart, encompassing various server-side components such as the API gateway, token service, and business services.
[0054] The API gateway module 403 is deployed on the server side, serving as the unified entry point for receiving client requests. The API gateway module 403 intercepts all requests arriving at the server, parsing the session identifier and anti-replay token carried in the request. The session identifier is a unique identifier for a user session, used to distinguish between different users or different login sessions of the same user. For example, it can be an LSESSION (user session ID) stored in a browser cookie, automatically carried by the browser when the client initiates the request, and used by the server to identify the session to which the request belongs. The API gateway module 403 executes atomicity verification logic and decides whether to forward the request to downstream business services or block it directly based on the verification result. Business services are server-side components that handle specific business logic, such as the transfer microservice and order microservice in a microservice architecture.
[0055] In the aforementioned system 400, the client interaction module 402 interacts with the token service module 401 through the token acquisition interface, and the client interaction module 402 interacts with the API gateway module 403 through business requests. The token set generated by the token service module 401 is stored in the server cache (e.g., Redis) for the API gateway module to access during verification.
[0056] This invention also provides an electronic device, such as... Figure 5 As shown, it includes a processor 501, a communication interface 502, a memory 503, and a communication bus 504, wherein the processor 501, the communication interface 502, and the memory 503 communicate with each other through the communication bus 504. Memory 503 is used to store computer programs; When processor 501 executes the program stored in memory 503, it performs the following steps: Intercept the received requests and determine the target request from them, and obtain the anti-replay token and session identifier carried in the target request; Based on the session identifier, a target token set associated with the session identifier is determined; wherein, the target token set includes multiple one-time anti-replay tokens pre-generated by the server for the session corresponding to the session identifier; Remove the one-time anti-replay token that matches the anti-replay token from the target token set; If the removal is successful, the target request is forwarded to the business service; if the removal fails, the target request is intercepted.
[0057] or, Obtain multiple one-time anti-replay tokens from the server and store them in the token pool; In response to the initiation of the request, determine whether the request path of the request matches the preset anti-replay path list; If the request path matches the anti-replay path list, the request is identified as the target request, a one-time anti-replay token is retrieved from the token pool, and the retrieved one-time anti-replay token is deleted from the token pool. The retrieved one-time anti-replay token is carried in the target request and sent to the server so that the server can perform anti-replay verification on the target request based on the one-time anti-replay token.
[0058] The communication bus mentioned in the above electronic devices can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. This communication bus can be divided into address bus, data bus, control bus, etc. For ease of illustration, only one thick line is used to represent it in the diagram, but this does not mean that there is only one bus or one type of bus.
[0059] The communication interface is used for communication between the aforementioned electronic devices and other devices.
[0060] The memory may include random access memory (RAM) or non-volatile memory (NVM), such as at least one disk storage device. Optionally, the memory may also be at least one storage device located remotely from the aforementioned processor.
[0061] The processors mentioned above can be general-purpose processors, including central processing units (CPUs), network processors (NPs), etc.; they can also be digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components.
[0062] The method provided in this invention can be applied to electronic devices. Specifically, the electronic device can be a desktop computer, a portable computer, a smart mobile terminal, a server, etc. No limitation is made herein; any electronic device that can implement this invention falls within the protection scope of this invention.
[0063] In the description of this invention, the terms "one embodiment," "some embodiments," "example," "specific example," or "some examples," etc., refer to a specific feature, structure, material, or characteristic described in connection with that embodiment or example, which is included in at least one embodiment or example of the invention. In this specification, the illustrative expressions of the above terms do not necessarily refer to the same embodiment or example. Furthermore, the specific features, structures, materials, or characteristics described may be combined in any suitable manner in one or more embodiments or examples. In addition, those skilled in the art can combine and integrate the different embodiments or examples described in this specification.
[0064] Although this application has been described herein in conjunction with various embodiments, other variations of the disclosed embodiments can be understood and implemented by those skilled in the art in carrying out the claimed application by reviewing the accompanying drawings, the disclosure, and the appended claims.
[0065] The above description, in conjunction with specific preferred embodiments, provides a further detailed explanation of the present invention. It should not be construed that the specific implementation of the present invention is limited to these descriptions. For those skilled in the art, various simple deductions or substitutions can be made without departing from the concept of the present invention, and all such modifications and substitutions should be considered within the scope of protection of the present invention.
Claims
1. A method for preventing replay attacks in microservices, characterized in that, This is applied to an API gateway, which is deployed on the server side. The method includes: Intercept the received requests and determine the target request from them, and obtain the anti-replay token and session identifier carried in the target request; Based on the session identifier, a target token set associated with the session identifier is determined; wherein, the target token set includes: multiple one-time anti-replay tokens generated by the server in response to the client's token acquisition request, based on the session identifier; Remove the one-time anti-replay token that matches the anti-replay token from the target token set; If the removal is successful, the target request is forwarded to the business service; if the removal fails, the target request is intercepted.
2. The method according to claim 1, characterized in that, The step of removing a one-time anti-replay token that matches the anti-replay token from the target token set includes: One-time anti-replay tokens that match the anti-replay token are removed from the target token set through atomic operations.
3. The method according to claim 1, characterized in that, The steps of intercepting received requests and determining the target request include: Intercept received requests; For each request, determine whether its request path matches the preset anti-replay path list; If the requested path does not match the preset anti-replay path list, the request will be forwarded to the business service. If the request path matches the preset anti-replay path list, the request is identified as the target request, and the anti-replay token and session identifier carried in the target request are obtained.
4. The method according to claim 1, characterized in that, The target token set is a Set type set stored in Redis, and the key corresponding to the Set type set is formed by concatenating the session identifier and a preset identifier; wherein... The preset identifier is a fixed string used to identify the anti-replay tokens stored in the Set type collection, and the multiple one-time anti-replay tokens are universally unique identification codes generated according to a preset number.
5. A method for implementing microservice replay protection, characterized in that, Applied to the client side, including: After sending a token acquisition request carrying a session identifier to the server, the system receives multiple one-time anti-replay tokens generated and returned by the server based on the session identifier, and stores the multiple one-time anti-replay tokens into the token pool. In response to the initiation of the request, determine whether the request path of the request matches the preset anti-replay path list; If the request path matches the preset anti-replay path list, the request is identified as the target request, a one-time anti-replay token is retrieved from the token pool, and the retrieved one-time anti-replay token is deleted from the token pool. The retrieved one-time anti-replay token is carried in the target request and sent to the server so that the server can perform anti-replay verification on the target request based on the one-time anti-replay token.
6. The method according to claim 5, characterized in that, The token pool is stored in SessionStorage; The step of retrieving a one-time anti-replay token from the token pool includes: retrieving a one-time anti-replay token from the token pool through a locking mechanism.
7. The method according to claim 6, characterized in that, Before the step of retrieving a one-time anti-replay token from the token pool, the method further includes: If no available one-time anti-replay tokens are available in the token pool, multiple one-time anti-replay tokens are acquired and stored in the token pool.
8. The method according to claim 5, characterized in that, The step of sending the retrieved one-time replay protection token to the server along with the target request includes: Write the retrieved one-time anti-replay token into the rev field of the request header of the target request, and send the target request to the server.
9. A microservice anti-replay implementation system, characterized in that, include: The token service module is used to generate multiple one-time anti-replay tokens and associate the generated one-time anti-replay tokens with the session identifier to store them as a target token set; The client interaction module is used to obtain multiple one-time anti-replay tokens generated by the token service module and store them in the token pool; In response to the initiation of the request, determine whether the request path of the request matches the preset anti-replay path list; If the request path matches the preset anti-replay path list, the request is identified as the target request, a one-time anti-replay token is retrieved from the token pool, and the retrieved one-time anti-replay token is deleted from the token pool. Write the retrieved one-time anti-replay token into the request header field of the target request, and send the session identifier along with the target request to the API gateway module; The API gateway module is used to intercept received requests and obtain the anti-replay token and session identifier carried in the target request. Based on the session identifier, determine the target token set associated with the session identifier; Remove the one-time anti-replay token that matches the anti-replay token from the target token set; If the removal is successful, the target request will be forwarded to the business service; If removal fails, the target request is intercepted.
10. An electronic device, characterized in that, include: The device includes a memory and a processor, wherein the memory stores a computer program that, when executed by the processor, implements the microservice anti-replay method as described in any one of claims 1 to 4, or the microservice anti-replay method as described in any one of claims 5 to 8.
