Method for refreshing token in multi-concurrent request scene
Through the unified interception and synchronous queue management mechanism, the serial failure problem of token token refresh in multiple concurrent request scenarios is solved, and the senseless automatic refresh and re-request are realized, which improves security and performance, and ensures the effectiveness of token tokens.
Patent Information
- Application Number
- CN202410552540.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-07
- Publication Date
- 2024-12-06
AI Technical Summary
In the multi-concurrent request scenario, when the token token of the Flutter client is invalid, multiple refresh token requests will be triggered, resulting in the wrong token token being reused, causing serial failure problems, affecting performance and security.
By uniformly intercepting the server that requests network tasks, the result is returned, and the token token is invalid and put it in the synchronization queue and refresh it in turn, ensuring that the global token token is valid and re-requested, avoiding multiple refresh operations.
Without affecting independent service requests, the token token automatically refreshes the token token in multiple concurrent request scenarios, which solves the problem of token token refresh in multiple concurrent request scenarios, improves security and performance, and the code is non-invasive and does not increase network request costs. .
Smart Images

Figure CN119094148A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer application technology, and in particular to a method for refreshing tokens in a multi-concurrent request scenario. Background Art
[0002] Token means a token in computer identity authentication, which is an object that represents the right to perform certain operations. Using tokens for user identity authentication can prevent unauthorized users from accessing restricted resources, improve security, reduce network transmission and server load, improve performance, and reduce server burden.
[0003] Flutter is an open source UI (user interface) software development kit launched by Google for developing cross-platform applications. It allows developers to use a set of code libraries to build high-performance, beautiful applications that run on Android, iOS, Web, and desktop platforms.
[0004] When the Flutter network client requests data from the server, it will include a token in the request for server authentication. To ensure security, the token must be time-sensitive. To prevent the token from being maliciously attacked or expired, the client needs to send a request to refresh the token to obtain a new token when the server informs the client that the token has expired after sending the request, and then re-request the previously failed request.
[0005] As the functions of Flutter application clients become more and more complex, multiple asynchronous requests may be issued simultaneously during the use of Flutter clients. When the token expires, multiple requests to refresh the token will be triggered, resulting in multiple asynchronous refreshes of the token at the same time. However, only one token will be valid in the end. Multiple requests will be repeated with the wrong token, which will cause the token to expire and an error message will be reported, causing a series of problems.
[0006] Based on the above situation, the present invention proposes a method for refreshing tokens in a multi-concurrent request scenario. Summary of the invention
[0007] In order to overcome the defects of the prior art, the present invention provides a simple and efficient method for refreshing tokens in a multi-concurrent request scenario.
[0008] The present invention is achieved through the following technical solutions:
[0009] A method for refreshing a token in a multi-concurrent request scenario, characterized in that it includes the following steps:
[0010] Step S1, uniformly intercept the server-side return result Response of the network request task, and determine whether the token Token returned by the current request task is invalid; if the returned token Token is invalid, refresh the token Token; there may be multiple asynchronous request tasks that need to refresh the token Token at the same time;
[0011] Step S2: Put all intercepted asynchronous request tasks that need to refresh the token into a Flutter synchronization queue, execute the refresh tasks in sequence, and continuously add new tasks to the synchronization queue;
[0012] Step S3: In the synchronization queue Queue, execute the refresh tasks in sequence;
[0013] Step S4: Take out the first task in the queue and determine whether the global token Token is valid:
[0014] If the global token Token is valid, re-request the latest global token Token;
[0015] If the global token Token is invalid, a request is triggered to obtain a new global token Token;
[0016] Step S5: Repeat step S4 until all tasks in the queue are completed and the queue is cleared.
[0017] In step S4, the first task in the queue is taken out to determine whether the token Token used in the previous task request is the same as the current latest global token Token:
[0018] If they are different, it means that the global token Token has been refreshed to a valid state. Re-request with the latest global token Token and record it as a re-requested task. Then delete the corresponding task from the synchronization queue Queue.
[0019] If they are the same, it means that the current global token Token is still invalid, triggering a request to obtain a new token Token.
[0020] In step S4, if the request to obtain the new token Token fails, the request task directly returns the response data Response of the failure to obtain the new token Token;
[0021] If the request to obtain a new token Token is successful, the global token Token is updated, and then the new global token Token is used to request again, and it is recorded as a re-requested task, and then the corresponding task is deleted from the synchronization queue Queue.
[0022] In the step S4, the task of re-requesting the global token Token is recorded, and when the task is successfully requested again, the corresponding re-request record is deleted;
[0023] When the task request fails again, if it is determined that the task is a previously requested task, the refresh token operation will no longer be performed, and the response data Response of the failure to obtain a new token will be directly returned.
[0024] A system for refreshing tokens in a multi-concurrent request scenario, characterized by comprising a task interception module, a synchronization queue management module and a token refreshing module;
[0025] The task interception module is responsible for uniformly intercepting the server-side return result Response of the network request task, and judging whether the token returned by the current request task is invalid; if the token returned is invalid, the token is refreshed;
[0026] The synchronization queue management module is responsible for placing all intercepted asynchronous request tasks that need to refresh the token Token into a Flutter synchronization queue Queue, executing the refresh tasks in sequence, and continuously adding new tasks to the synchronization queue Queue;
[0027] The token refresh module is responsible for executing refresh tasks in the synchronization queue Queue in sequence;
[0028] Take out the first task in the queue and determine whether the global token Token is valid:
[0029] If the global token Token is valid, the token refresh module re-requests the latest global token Token;
[0030] If the global token Token is invalid, the token refresh module triggers a request to obtain a new global token Token;
[0031] Then continue to take out the first task in the queue and repeat the above logic until all tasks in the queue are executed and the queue is cleared.
[0032] After the token refresh module takes out the first task in the queue, it first determines whether the token Token used in the previous task request is the same as the current latest global token Token:
[0033] If they are different, it means that the global token Token has been refreshed to a valid state. Re-request with the latest global token Token and record it as a re-requested task. Then delete the corresponding task from the synchronization queue Queue.
[0034] If they are the same, it means that the current global token Token is still invalid, triggering a request to obtain a new token Token;
[0035] If the request to obtain a new token fails, the request task directly returns the response data Response of the failure to obtain a new token;
[0036] If the request to obtain a new token Token is successful, the global token Token is updated, and then the new global token Token is used to request again, and it is recorded as a re-requested task, and then the corresponding task is deleted from the synchronization queue Queue.
[0037] The token refresh module records the task of re-requesting the global token Token, and deletes the corresponding re-request record when the task is successfully requested again;
[0038] When the task request fails again, the token refresh module determines whether the task is the previously requested task. If so, the token refresh operation is no longer performed, and the response data Response of the failure to obtain the new token Token is directly returned.
[0039] A device for refreshing tokens in a multi-concurrent request scenario, characterized in that it includes a memory and a processor; the memory is used to store a computer program, and the processor is used to implement the above-mentioned method steps when executing the computer program.
[0040] A readable storage medium, characterized in that: a computer program is stored on the readable storage medium, and the computer program implements the above method steps when executed by a processor.
[0041] The beneficial effects of the present invention are as follows: the method for refreshing tokens in a multi-concurrent request scenario can automatically refresh the token Token and re-request without affecting the independent request of each business, thereby solving the problem of refreshing the token Token in a multi-concurrent request scenario; at the same time, the code is non-invasive, does not affect the code related to a single network request, and does not increase the cost of a single network request. BRIEF DESCRIPTION OF THE DRAWINGS
[0042] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0043] Attached Figure 1 A schematic diagram of a method for refreshing a token in a multi-concurrent request scenario of the present invention. DETAILED DESCRIPTION
[0044] In order to enable those skilled in the art to better understand the technical solutions in the present invention, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the embodiments of the present invention. Obviously, the described embodiments are only a 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 should fall within the scope of protection of the present invention.
[0045] Each network request task will take the global token Token for network request, and the global token Token can be updated at any time.
[0046] The method for refreshing the token in the multi-concurrent request scenario includes the following steps:
[0047] Step S1, uniformly intercept the server-side return result Response of the network request task, and determine whether the token Token returned by the current request task is invalid; if the returned token Token is invalid, refresh the token Token; there may be multiple asynchronous request tasks that need to refresh the token Token at the same time;
[0048] Step S2: Put all intercepted asynchronous request tasks that need to refresh the token into a Flutter synchronization queue, execute the refresh tasks in sequence, and continuously add new tasks to the synchronization queue;
[0049] Step S3: In the synchronization queue Queue, execute the refresh tasks in sequence;
[0050] Step S4: Take out the first task in the queue and determine whether the global token Token is valid:
[0051] If the global token Token is valid, re-request the latest global token Token;
[0052] If the global token Token is invalid, a request is triggered to obtain a new global token Token;
[0053] Step S5: Repeat step S4 until all tasks in the queue are completed and the queue is cleared.
[0054] In step S4, the first task in the queue is taken out to determine whether the token Token used in the previous task request is the same as the current latest global token Token:
[0055] If they are different, it means that the global token Token has been refreshed to a valid state. Re-request with the latest global token Token and record it as a re-requested task. Then delete the corresponding task from the synchronization queue Queue.
[0056] If they are the same, it means that the current global token Token is still invalid, triggering a request to obtain a new token Token.
[0057] In step S4, if the request to obtain the new token Token fails, the request task directly returns the response data Response of the failure to obtain the new token Token;
[0058] If the request to obtain a new token Token is successful, the global token Token is updated, and then the new global token Token is used to request again, and it is recorded as a re-requested task, and then the corresponding task is deleted from the synchronization queue Queue.
[0059] In the step S4, the task of re-requesting the global token Token is recorded, and when the task is successfully requested again, the corresponding re-request record is deleted;
[0060] When the task request fails again, if it is determined that the task is a previously requested task, the refresh token operation will no longer be performed, and the response data Response of the failure to obtain a new token will be directly returned.
[0061] The system for refreshing tokens in a multi-concurrent request scenario includes a task interception module, a synchronization queue management module and a token refreshing module;
[0062] The task interception module is responsible for uniformly intercepting the server-side return result Response of the network request task, and judging whether the token returned by the current request task is invalid; if the token returned is invalid, the token is refreshed;
[0063] The synchronization queue management module is responsible for placing all intercepted asynchronous request tasks that need to refresh the token Token into a Flutter synchronization queue Queue, executing the refresh tasks in sequence, and continuously adding new tasks to the synchronization queue Queue;
[0064] The token refresh module is responsible for executing refresh tasks in the synchronization queue Queue in sequence;
[0065] Take out the first task in the queue and determine whether the global token Token is valid:
[0066] If the global token Token is valid, the token refresh module re-requests the latest global token Token;
[0067] If the global token Token is invalid, the token refresh module triggers a request to obtain a new global token Token;
[0068] Then continue to take out the first task in the queue and repeat the above logic until all tasks in the queue are executed and the queue is cleared.
[0069] After the token refresh module takes out the first task in the queue, it first determines whether the token Token used in the previous task request is the same as the current latest global token Token:
[0070] If they are different, it means that the global token Token has been refreshed to a valid state. Re-request with the latest global token Token and record it as a re-requested task. Then delete the corresponding task from the synchronization queue Queue.
[0071] If they are the same, it means that the current global token Token is still invalid, triggering a request to obtain a new token Token;
[0072] If the request to obtain a new token fails, the request task directly returns the response data Response of the failure to obtain a new token;
[0073] If the request to obtain a new token Token is successful, the global token Token is updated, and then the new global token Token is used to request again, and it is recorded as a re-requested task, and then the corresponding task is deleted from the synchronization queue Queue.
[0074] The token refresh module records the task of re-requesting the global token Token, and deletes the corresponding re-request record when the task is successfully requested again;
[0075] When the task request fails again, the token refresh module determines whether the task is the previously requested task. If so, the token refresh operation is no longer performed, and the response data Response of the failure to obtain the new token Token is directly returned.
[0076] The device for refreshing a token in a multi-concurrent request scenario includes a memory and a processor; the memory is used to store a computer program, and the processor is used to implement the above method steps when executing the computer program.
[0077] The readable storage medium stores a computer program, and when the computer program is executed by a processor, the above method steps are implemented.
[0078] The embodiment described above is only one specific implementation of the present invention. Common changes and substitutions made by those skilled in the art within the scope of the technical solution of the present invention should be included in the protection scope of the present invention.
Claims
1. A method for refreshing a token in a multi-concurrent request scenario, characterized in that: The following steps are involved: Step S1, uniformly intercept the server return result Response of the network request task, and determine whether the token Token returned by the current request task is invalid; if the token Token returned is invalid, refresh the token Token; Step S2: Put all intercepted asynchronous request tasks that need to refresh the token into a Flutter synchronization queue, execute the refresh tasks in sequence, and continuously add new tasks to the synchronization queue; Step S3: In the synchronization queue Queue, execute the refresh tasks in sequence; Step S4: Take out the first task in the queue and determine whether the global token Token is valid: If the global token Token is valid, re-request the latest global token Token; If the global token Token is invalid, a request is triggered to obtain a new global token Token; Step S5: Repeat step S4 until all tasks in the queue are completed and the queue is cleared.
2. The method for refreshing tokens in a multi-concurrent request scenario according to claim 1, characterized in that: In step S4, the first task in the queue is taken out to determine whether the token Token used in the previous task request is the same as the current latest global token Token: If they are different, it means that the global token Token has been refreshed to a valid state. Re-request with the latest global token Token and record it as a re-requested task. Then delete the corresponding task from the synchronization queue Queue. If they are the same, it means that the current global token Token is still invalid, triggering a request to obtain a new token Token.
3. The method for refreshing tokens in a multi-concurrent request scenario according to claim 2, characterized in that: In the step S4, if the request to obtain the new token Token fails, the request task directly returns the response data Response of the failure to obtain the new token Token; If the request to obtain a new token Token is successful, the global token Token is updated, and then the new global token Token is used to request again, and it is recorded as a re-requested task, and then the corresponding task is deleted from the synchronization queue Queue.
4. The method for refreshing tokens in a multi-concurrent request scenario according to claim 3, characterized in that: In the step S4, the task of re-requesting the global token Token is recorded, and when the task is successfully requested again, the corresponding re-request record is deleted; When the task request fails again, if it is determined that the task is a previously requested task, the refresh token operation will no longer be performed, and the response data Response of the failure to obtain a new token will be directly returned.
5. A system for refreshing tokens in a multi-concurrent request scenario, characterized in that: Including task interception module, synchronization queue management module and token refresh module; The task interception module is responsible for uniformly intercepting the server-side return result Response of the network request task, and judging whether the token returned by the current request task is invalid; if the token returned is invalid, the token is refreshed; The synchronization queue management module is responsible for placing all intercepted asynchronous request tasks that need to refresh the token Token into a Flutter synchronization queue Queue, executing the refresh tasks in sequence, and continuously adding new tasks to the synchronization queue Queue; The token refresh module is responsible for executing refresh tasks in the synchronization queue Queue in sequence; Take out the first task in the queue and determine whether the global token Token is valid: If the global token Token is valid, the token refresh module re-requests the latest global token Token; If the global token Token is invalid, the token refresh module triggers a request to obtain a new global token Token; Then continue to take out the first task in the queue and repeat the above logic until all tasks in the queue are executed and the queue is cleared.
6. The system for refreshing tokens in a multi-concurrent request scenario according to claim 5, characterized in that: After the token refresh module takes out the first task in the queue, it first determines whether the token Token used in the previous task request is the same as the current latest global token Token: If they are different, it means that the global token Token has been refreshed to a valid state. Re-request with the latest global token Token and record it as a re-requested task. Then delete the corresponding task from the synchronization queue Queue. If they are the same, it means that the current global token Token is still invalid, triggering a request to obtain a new token Token; If the request to obtain a new token fails, the request task directly returns the response data Response of the failure to obtain a new token; If the request to obtain a new token Token is successful, the global token Token is updated, and then the new global token Token is used to request again, and it is recorded as a re-requested task, and then the corresponding task is deleted from the synchronization queue Queue.
7. The system for refreshing tokens in a multi-concurrent request scenario according to claim 6, characterized in that: The token refresh module records the task of re-requesting the global token Token, and deletes the corresponding re-request record when the task is successfully requested again; When the task request fails again, the token refresh module determines whether the task is the previously requested task. If so, the token refresh operation is no longer performed, and the response data Response of the failure to obtain the new token Token is directly returned.
8. A device for refreshing tokens in a multi-concurrent request scenario, characterized in that: The method comprises a memory and a processor; the memory is used to store a computer program, and the processor is used to implement the method steps as claimed in any one of claims 1 to 4 when executing the computer program.
9. A readable storage medium, characterized in that: The readable storage medium stores a computer program, and when the computer program is executed by a processor, the method steps according to any one of claims 1 to 4 are implemented.
Citation Information
Cited By
Login token refreshing method and device
CN121567297A
