Access token generation and verification method, device, electronic device and storage medium
By storing and compressing and encrypting tokens in Bencoding format, it solves the problems of space waste and information leakage in JWT token storage and transmission, and achieves efficient user information acquisition and security guarantee in high concurrency scenarios.
Patent Information
- Application Number
- CN202111213905.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-10-19
- Publication Date
- 2024-10-11
- Estimated Expiration
- 2041-10-19
AI Technical Summary
JWT tokens have problems such as space waste, information leakage, and inability to obtain user information in AJAX requests during the storage and transmission process.
Bencoding format is used to store user information, and an access token is generated through compression and encryption processing, which is stored in the client cookie. It is suitable for high concurrency scenarios.
It reduces the storage space occupied by access tokens, improves response efficiency and system performance, ensures the security of user information, and can obtain user information in any HTTP request.
Smart Images

Figure CN113922968B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of communication security technology, and in particular to a method, device, electronic device and storage medium for generating and verifying an access token. Background Art
[0002] Authentication of requests sent by clients by the server is an important means of protecting data security in Internet applications. The current mainstream authentication method is based on JSON Web Token (JSON Web Token, JWT). JSON (JavaScript Object Notation, JS Object Notation) is a lightweight data exchange format.
[0003] The process of generating a JWT token is as follows: when the client logs in, the server obtains the user information, stores it in JSON format, and processes the user information in JSON format based on the Base64 encoding algorithm to generate a Base64 string as a JWT token and returns it to the client. After that, the client will carry the JWT token every time it sends a request, specifically by placing the JWT token in the Authorization field of the HTTP request header, and the server authenticates the user request by verifying the JWT token.
[0004] However, there are at least three problems with JWT tokens:
[0005] First, the user information in the JWT token is stored in JSON format. Each key and value must be separated by double quotes and colons, which takes up storage space and causes a lot of extra overhead.
[0006] Second, the user information in the JWT token is not encrypted, which makes it easy to leak user information;
[0007] Third, the JWT token is carried in the HTTP request header based on the AJAX request, and the receiving type of the AJAX request is limited to String string, not stream type. Therefore, the server cannot obtain the current user information through the JWT token when downloading the file.
[0008] It should be noted that the information disclosed in the above background technology section is only used to enhance the understanding of the background of the present invention, and therefore may include information that does not constitute the prior art known to ordinary technicians in the field. Summary of the invention
[0009] In view of this, the present invention provides a method, device, electronic device and storage medium for generating and verifying an access token. By storing user information in Bencoding format, the storage space occupied by the access token can be greatly reduced, and resource consumption can be reduced. It is also suitable for high-concurrency scenarios, and can also avoid user information leakage, thereby ensuring user information security.
[0010] One aspect of the present invention provides a method for generating and verifying an access token, comprising: responding to a login request from a client, obtaining user information according to the login request; Bencoding the user information to generate target data stored in a Bencoding format; generating an access token according to the target data, and returning first response data carrying the access token to the client.
[0011] In some embodiments, when Bencoding the user information, it also includes: B-encoding the token validity period; obtaining the target data including the user information data stored in the Bencoding format and the token validity period data stored in the Bencoding format.
[0012] In some embodiments, the token validity period includes a token issuance time and a token expiration time of date data type; the B encoding of the token validity period includes: adding an identifier t to the beginning of the token issuance time and the token expiration time and an identifier e to the end in an encoding manner of [t date data]e, respectively, to generate token validity period data stored in the Bencoding format of t[token issuance time]e and t[token expiration time]e.
[0013] In some embodiments, the user information includes multiple information contents of string type; the Bencoding of the user information includes: obtaining the string length in bytes of each information content in an encoding method of [string length: string], and generating user information data for each information content stored in a Bencoding format of [number of bytes: information content].
[0014] In some embodiments, generating an access token based on the target data includes: compressing the target data to obtain compressed target data; encrypting the compressed target data to obtain encrypted target data; encoding the encrypted target data to generate the access token.
[0015] In some embodiments, when compressing the target data, a GZIP compression algorithm is used; and / or, when encrypting the compressed target data, an SM4 encryption algorithm is used; and / or, when encoding the encrypted target data, a Base64 encoding algorithm is used.
[0016] In some embodiments, returning the first response data carrying the access token to the client includes: placing the access token in the Set-Cookie field of the response header of the first response data, so that the client automatically saves the access token to the Cookie after receiving the first response data, and the access token is automatically carried in the request sent by the client afterwards.
[0017] In some embodiments, when the user information is B-encoded, the token validity period is also B-encoded; the access token generation and verification method also includes: in response to an access request carrying the access token, obtaining the current user information according to the access request, and obtaining the user information and the token validity period according to the access token; checking in turn whether the token validity period is valid and whether the current user information is updated compared to the user information; if the token validity period expires, returning an error message to the client; if the token validity period is valid and the current user information is not updated, returning the second response data corresponding to the access request to the client; if the token validity period is valid and the current user information has not been updated, generating an updated token based on the current user information, and returning the second response data carrying the updated token to the client.
[0018] In some embodiments, after the updated token is generated based on the current user information, the access token is added to a blacklist pool; after verifying that the token is valid, it is determined whether the access token is in the blacklist pool, and if so, the error message is returned to the client; if not, it is verified whether the current user information is updated.
[0019] In some embodiments, obtaining the user information and the token validity period based on the access token includes: processing the access token using the inverse process of the access token generation process to obtain the target data; and B-decoding the target data to obtain the user information and the token validity period.
[0020] Another aspect of the present invention provides an access token generation and verification device, including: a request response module, configured to respond to a login request from a client and obtain user information according to the login request; a Bencoding module, configured to Bencode the user information and generate target data stored in a Bencoding format; a token generation module, configured to generate an access token according to the target data and return first response data carrying the access token to the client.
[0021] Another aspect of the present invention provides an electronic device, comprising: a processor; a memory, wherein the memory stores executable instructions; wherein when the executable instructions are executed by the processor, the method for generating and verifying an access token as described in any of the above embodiments is implemented.
[0022] Another aspect of the present invention provides a computer-readable storage medium for storing a program, wherein when the program is executed by a processor, the method for generating and verifying an access token as described in any of the above embodiments is implemented.
[0023] The beneficial effects of the present invention compared with the prior art include at least:
[0024] By storing user information in Bencoding format, the storage space occupied by access tokens can be greatly reduced, and resource consumption can be reduced. By improving the B encoding method and adding encoding storage for date data, user information and token validity period can be quickly and synchronously obtained based on the access token, without the need for secondary processing of time data. This is especially suitable for high-concurrency scenarios, improving response efficiency and system performance. By compressing the target data, the space occupied by the access token can be further reduced, and by encrypting the target data, the security of user information can be ensured and user information leakage can be prevented. In addition, by storing the access token in the client's Cookie, regardless of whether the request sent by the client is an AJAX or AXIOS request, the server can obtain the access token in the Cookie for parsing and obtaining user information.
[0025] It is to be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the invention. BRIEF DESCRIPTION OF THE DRAWINGS
[0026] The accompanying drawings herein are incorporated into and constitute a part of the specification, illustrate embodiments consistent with the present invention, and together with the specification are used to explain the principles of the present invention. Obviously, the accompanying drawings described below are only some embodiments of the present invention, and for those of ordinary skill in the art, other accompanying drawings can be obtained based on these accompanying drawings without creative work.
[0027] Figure 1 A schematic diagram showing the steps of a method for generating and verifying an access token in one embodiment of the present invention;
[0028] Figure 2 A schematic diagram showing a scenario of a process of generating an access token in an embodiment of the present invention;
[0029] Figure 3 A schematic diagram showing the steps of a method for generating and verifying an access token in another embodiment of the present invention;
[0030] Figure 4 A schematic diagram showing a scenario of a verification process of an access token in an embodiment of the present invention;
[0031] Figure 5 A schematic diagram of a module of a device for generating and verifying an access token in one embodiment of the present invention is shown;
[0032] Figure 6 A schematic structural diagram of an electronic device in an embodiment of the present invention is shown. DETAILED DESCRIPTION
[0033] Example embodiments will now be described more fully with reference to the accompanying drawings. However, example embodiments can be implemented in a variety of forms and should not be construed as limited to the embodiments set forth herein. Instead, these embodiments are provided to make the present invention comprehensive and complete and to fully convey the concepts of the example embodiments to those skilled in the art.
[0034] The accompanying drawings are only schematic illustrations of the present invention and are not necessarily drawn to scale. The same reference numerals in the figures represent the same or similar parts, and their repeated description will be omitted. Some of the block diagrams shown in the accompanying drawings are functional entities and do not necessarily correspond to physically or logically independent entities. These functional entities can be implemented in software form, or in one or more hardware modules or integrated circuits, or in different networks and / or processor devices and / or microcontroller devices.
[0035] In addition, the processes shown in the accompanying drawings are only exemplary and do not necessarily include all the steps. For example, some steps can be decomposed, some steps can be combined or partially combined, and the actual execution order may change according to the actual situation. The words "first", "second" and similar words used in the specific description do not indicate any order, quantity or importance, but are only used to distinguish different components. It should be noted that the embodiments of the present invention and the features in different embodiments can be combined with each other without conflict.
[0036] Figure 1 The main steps of the method for generating and verifying an access token in one embodiment are shown, referring to Figure 1 As shown, the method for generating and verifying an access token in this embodiment includes: step S110, responding to a login request from a client, and obtaining user information according to the login request; step S120, at least Bencoding the user information to generate target data stored in a Bencoding format; step S130, generating an access token according to the target data, and returning first response data carrying the access token to the client.
[0037] In a specific scenario, the client logs in according to the user name and password and sends a login request; after receiving the login request, the server queries the database for the user information corresponding to the login request.
[0038] In traditional JWT token technology, user information is stored in JSON format. For example, strings and integers are stored in JSON format as follows:
[0039] {
[0040] "name":"test",
[0041] "project":"hello world",
[0042] "length":1234,
[0043] "score":-1234
[0044] }
[0045] In the above JSON format storage, symbols such as curly brackets, double quotes, and commas increase unnecessary space overhead, resulting in larger data stored in the JSON format.
[0046] The present invention is based on the Bencoding technology, performs B encoding on user information, and generates target data stored in the Bencoding format, which can save unnecessary overhead and improve space utilization.
[0047] Bencoding is a formatting method used by BT (BitTorrent) seed files (.torrent) to save data. Currently, Bencoding supports four types of data: Strings, Integers, Lists, and Dictionaries. The encoding rules are as follows:
[0048] Strings are encoded as: [string length]: [string], where the length of the string is in bytes. For example, the string "test" is encoded as: 4:test, and the string "hello world" is encoded as: 11:hello world.
[0049] Integers are encoded as: i[integer]e, with the start marker i and the end marker e. For example, the integer 1234 is generated by Bencoding: i1234e, and the integer -1234 is generated by Bencoding: i-1234e. There is no size limit for integers. i0e represents the integer 0, and i-0e is illegal.
[0050] Lists are encoded as: l[Bencoding encoding type]e, with the start tag as l and the end tag as e. Bencoding encoding types include integers, strings, lists, and dictionaries. For example, two strings ["test", "abcde"] are generated after Bencoding: l4:test5abcdee.
[0051] Dictionaries are encoded as d[Bencoding string][Bencoding encoding type]e, with the start tag as d and the end tag as e. The keyword must be a Bencoding string, and the value can be any Bencoding encoding type. For example, the dictionary structure {"tage" = 20} is generated after Bencoding: d3:agei20ee; the dictionary structure {"path" = "C: / ", "filename" = "test.txt"} is generated after Bencoding: d4:path3:C: / 8:filename8:test.txte.
[0052] When storing user information, take the user name xiaoming and ID number 216732483473122 as an example. The token issuance time is 2021-04-14 12:23:34, and the token expiration time is 2021-04-14 14:23:34. If the data is stored in JSON format, it will take up 141 characters, as follows:
[0053] {
[0054] "subject": "xiaoming",
[0055] "name": "Xiaoming",
[0056] "id":"216732483473122",
[0057] "issuer": "capms",
[0058] "issuedAt":"2021-04-14 12:23:34",
[0059] "expiration":"2021-04-14 14:23:34"
[0060] }
[0061] The present invention stores data in Bencoding format without the need for extra overhead such as double quotes and braces, which can save space consumption of user information data. Only 117 characters are needed to store the above user information, as shown below:
[0062] 7:subject8:xiaoming4:name4:Xiaoming2:id15:2167324834731226:issuer5:capms8:issuedAti1618364994e10:expirationi1618368594e.
[0063] In this embodiment, the user information includes multiple string-type information contents; therefore, the user information is B-encoded, that is, it includes: using the encoding method of [string length]:[string], respectively obtaining the string length of each information content in bytes, and generating user information data stored in the Bencoding format of [byte number: information content] for each information content. For example, 8:xiaoming corresponds to the user name string xiaoming with a byte number of 8, and 15:2167324834731226 corresponds to the ID string 216732483473122 with a byte number of 15.
[0064] In addition, i1618364994e and i1618368594e correspond to the token issuance time and the token expiration time respectively, that is, when the present invention B-encodes the user information, it also B-encodes the token validity period including the token issuance time and the token expiration time.
[0065] That is, when the user information is B-encoded, the token validity period is also B-encoded to obtain target data including the user information data stored in the Bencoding format and the token validity period data stored in the Bencoding format.
[0066] Furthermore, in the current B encoding, since only four data types, namely strings, integers, lists, and dictionaries, are supported, timestamp is used to represent date data types such as token issuance time and token expiration time in the Bencoding format. In this way, when the server verifies the access token, it needs to extract the token validity period data in the access token, process the timestamp into date data for the second time, and then determine whether the access token is within the validity period, which is very time-consuming. Especially in the case of high concurrency, it will seriously affect the efficiency of the server to obtain user information.
[0067] Therefore, the present invention improves the B encoding by adding a date data type, encoded as t[date data]e, that is, adding a start identifier t and an end identifier e at the beginning and end of the date data respectively. By performing B encoding on the token issuance time and token expiration time, token validity period data stored in the Bencoding format of t[token issuance time]e and t[token expiration time]e can be generated.
[0068] Thus, storing the above user information and token validity period in the improved Bencoding format requires 135 characters, as follows:
[0069] 7:subject8:xiaoming4:name4:小明2:id15:2167324834731226:issuer5:capms8:issuedAtt2021-04-14 12:23:34e10:expirationt2021-04-14 14:23:34e. Among them, t2021-04-14 12:23:34e corresponds to the token issuance time of 2021-04-14 12:23:34, and t2021-04-14 14:23:34e corresponds to the token expiration time of 2021-04-14 14:23:34.
[0070] Using the improved Bencoding format to store user information and token validity period can still reduce the storage space occupied by the access token compared with the JSON format, reduce resource consumption, and through the encoding and storage of the added date data, the server can quickly synchronously obtain user information and token validity period according to the access token when verifying the access token, without the need for secondary processing of time data. It is especially suitable for high-concurrency scenarios, improving the response efficiency and enhancing the system performance.
[0071] Generate an access token based on the target data, specifically including: compressing the target data to obtain compressed target data, for example, using the GZIP compression algorithm to compress the target data; encrypting the compressed target data to obtain encrypted target data, for example, using the SM4 encryption algorithm to encrypt the compressed target data; encoding the encrypted target data to generate an access token, for example, using the Base64 encoding algorithm to encode the encrypted target data.
[0072] The GZIP compression algorithm, SM4 encryption algorithm and Base64 encoding algorithm are existing algorithm technologies, and the present invention will not further describe them. In other embodiments, other compression algorithms, encryption algorithms, encoding algorithms, etc. may also be used to achieve compression, encryption and encoding in the token generation process.
[0073] By compressing the target data, the space occupied by the access token can be further reduced; by encrypting the target data, the security of user information can be ensured and leakage of user information can be prevented.
[0074] Returning the first response data carrying the access token to the client, specifically including: putting the access token into the Set-Cookie field of the response header of the first response data, so that after the client receives the first response data, the access token is automatically saved in the Cookie, and the access token is automatically carried in the subsequent requests sent by the client.
[0075] Since the access token is stored in the client's Cookie, according to the HTTP mechanism between the client and the server, the user is unaware of the series of processes in which the access token is stored on the client and the client automatically carries the access token when sending a request. Therefore, regardless of whether the request sent by the client is an AJAX or AXIOS request, the server can obtain the user information by parsing the access token in the Cookie.
[0076] Figure 2 The scene of the generation process of the access token in one embodiment is shown. According to the steps and principles of the generation process of the access token described above, combined with Figure 2 As shown, in a specific scenario, the process of generating an access token includes the following steps.
[0077] S210, the client 200a initiates a login request, which carries at least a user name and a password. For example, the client 200a logs in with the user name xiaoming and the corresponding password.
[0078] S220, after receiving the login request, the server 200b verifies the user's identity, and if the verification is successful, the subsequent process continues, and if the verification is unsuccessful, S220' is executed to return an error message. This step is an existing technology, so it will not be described in detail.
[0079] S230, the server 200b queries the user information from the database, for example, the corresponding user information may be queried according to the ID number 216732483473122 carried in the login request.
[0080] S240, the server 200b stores the queried user information and token validity period in an improved Bencoding format. Taking the user information including the user name xiaoming and the ID number 216732483473122, and the token validity period including the token issuance time 2021-04-1412:23:34 and the token expiration time 2021-04-1414:23:34 as an example, the target data stored in the Bencoding format is: 7:subject8:xiaoming4:name4:Xiaoming2:id15:2167324834731226:issuer5:capms8:issuedAtt2021-04-14 12:23:34e10:expirationt2021-04-14 14:23:34e.
[0081] S250, the server 200b uses the GZIP compression algorithm to compress the generated Bencoding string and the target data to obtain a compressed string, that is, the compressed target data. The compressed string can be obtained through the GZIP function in the Java Hutool tool, and the obtained compressed string is: [B@7eda2dbb.
[0082] S260, the server 200b uses the SM4 encryption algorithm to encrypt the compressed string, and the key key used for encryption is determined by the server 200b. The string generated after SM4 encryption is: ca74247e979b07725a600829915a7c16.
[0083] S270, the server 200b performs Base64 encoding on the encrypted string to obtain the final access token. The token data finally obtained is: Y2E3NDI0N2U5NzliMDc3MjVhNjAwODI5OTE1YTdjMTY=.
[0084] If the current mainstream JWT technology is used, stored in JSON format, and then Base64 encoded, the same user information will be obtained as the following string: ewogICDigJxzdWJqZWN04oCdOuKAnXhpYW9taW5n4oCdLAogICDigJxuYW1l4oCdOuKAneWwj+aYjuKAnSwKICAg4oCcaWTigJ064oCdMjE2NzMyNDgzNDc It can be seen that the present invention can save a lot of storage space by storing data in the improved Bencoding format and then compressing it.
[0085] S280, the server 200b puts the access token into the Set-Cookie of the response header and returns it to the client 200a.
[0086] S290, after receiving the response data from the server 200b, the client 200a recognizes that there is a Set-Cookie field in the response data, and automatically saves the value, i.e., the access token, into the cookie. Thus, each time the client 200a sends a request, it will automatically carry this cookie value, i.e., the access token.
[0087] By adopting the above-mentioned access token generation process, storing user information in Bencoding format, and combining compression methods and symmetric encryption functions, the space occupied by token data can be greatly saved, and user information can be effectively prevented from being stolen or leaked, ensuring the integrity and availability of request information and the security of network requests; and by improving the Bencoding encoding method, the server does not need to perform secondary processing on time data, further improving server performance, which is especially suitable for high-concurrency scenarios.
[0088] Based on the methods for generating and verifying access tokens in the above embodiments, Figure 3 The main steps of the method for generating and verifying an access token in another embodiment are shown. The features and principles of generating and verifying an access token described in any of the above embodiments can be applied to Figure 3 The embodiment shown in FIG. Figure 3 In the description of the illustrated embodiment, the features and principles regarding the generation and verification of the access token that have been explained will not be repeated.
[0089] Reference Figure 3 As shown, the method for generating and verifying an access token in this embodiment includes: step S310, in response to a login request from a client, obtaining user information according to the login request; step S320, performing B encoding on the user information and the token validity period, generating target data including user information data stored in a Bencoding format and token validity period data stored in a Bencoding format; step S330, generating an access token according to the target data, and returning first response data carrying the access token to the client; step S340, in response to an access request carrying the access token, obtaining current user information according to the access request, and obtaining user information and token validity period according to the access token; step S350, sequentially verifying whether the token validity period is valid and whether the current user information is updated compared to the user information; step S360, if the token validity period is invalid, returning an error message to the client; step S370, if the token validity period is valid and the current user information is not updated, returning second response data corresponding to the access request to the client; step S380, if the token validity period is valid and the current user information has been updated, generating an update token based on the current user information, and returning second response data carrying the update token to the client.
[0090] The current user information and access token can be obtained from the cookie. When obtaining the user information and token validity period based on the access token, the access token is processed by the inverse process of the access token generation process to obtain the target data, that is, the access token is sequentially Base64 decoded, SM4 decrypted (using the key in the above step S260) and decompressed to obtain the target data containing the user information data stored in the Bencoding format and the token validity period data stored in the Bencoding format; then the target data is B-decoded, that is, the inverse process of B-coding, to obtain the user information and token validity period.
[0091] When checking whether the token validity period is valid and whether the current user information is updated compared with the user information, first check whether the token validity period is valid, that is, check whether the token validity period has expired. If it has expired, the authentication fails and an error message is returned; if it has not expired, that is, the token validity period is valid, continue to determine whether the access token is in the blacklist pool, if it is, return an error message, if not, check whether the current user information is updated, if not updated, return the response data corresponding to the access request, if updated, based on the current user information, use the token generation process described above to regenerate the access token, and return the response data carrying the updated access token. And, after generating the updated access token, add the old access token to the blacklist pool.
[0092] Figure 4The scene of the access token verification process in one embodiment is shown. According to the steps and principles of the access token verification process described above, combined with Figure 4 As shown, in a specific scenario, the verification process of the access token includes the following process.
[0093] S410, the client 200a initiates an access request, such as a page access request, which carries an access token.
[0094] S420, the server 200b performs Base64 decoding on the access token to obtain an encrypted string.
[0095] S430, the server 200b performs SM4 decryption on the encrypted string (using the key used for SM4 encryption) to obtain a compressed string.
[0096] S440, the server 200b performs GZIP decompression on the compressed string to obtain target data stored in Bencoding format.
[0097] S450, the server 200b performs B decoding on the target data, that is, parses the data in Bencoding format to obtain user information and token validity period.
[0098] S460, the server 200b determines whether the access token is within the validity period according to the token validity period; if not, the authentication fails, and S460' is executed to return an error message; if yes, the subsequent process continues.
[0099] S470, the server 200b determines whether the access token is in the blacklist pool. If so, the authentication fails and S460' is executed to return an error message. Otherwise, the server 200b authenticates successfully and performs the subsequent S480 business logic operation in combination with the user information.
[0100] The S480 business logic operation specifically includes verifying whether the current user information corresponding to the access request is updated compared to the user information in the access token. If not, the corresponding response data is returned. If updated, the access token is regenerated using the token generation process described above, and the response data carrying the updated access token is returned, and the old access token is added to the blacklist pool. No further explanation will be given.
[0101] S490, the client 200a displays data on a page according to the response data.
[0102] In a distributed situation, the access token generation and verification method of the present invention is also applicable, and it is only necessary to unify the keys used by the SM4 encryption algorithm in each distributed machine.
[0103] The access token generation and verification method of the present invention can effectively save the space occupied by token data by storing user information in Bencoding format, so that the access token can be stored in Cookie, so that the server can still obtain user information when non-AJAX or AXIOS requests are made; and the present invention improves the Bencoding encoding method, so that the server does not need to process time data twice when obtaining user information, effectively improving the server performance, and is particularly suitable for high-concurrency scenarios; the present invention also introduces the SM4 encryption algorithm in the token data generation process, effectively preventing user information leakage, ensuring the integrity and availability of request information, and ensuring the security of network requests.
[0104] The embodiment of the present invention also provides an access token generation and verification device, which can be used to implement the access token generation and verification method described in any of the above embodiments. The features and principles of the access token generation and verification method described in any of the above embodiments can be applied to the following access token generation and verification device embodiment. In the following access token generation and verification device embodiment, the features and principles of access token generation and verification that have been explained will not be repeated.
[0105] Figure 5 The main modules of the access token generation and verification device in one embodiment are shown, referring to Figure 5 As shown, the access token generation and verification device 500 is configured on the server side, and specifically includes: a request response module 510, configured to respond to the login request of the client and obtain user information according to the login request; a Bencoding module 520, configured to B-encode the user information and generate target data stored in the Bencoding format; a token generation module 530, configured to generate an access token according to the target data and return the first response data carrying the access token to the client.
[0106] Furthermore, the access token generation and verification device 500 may also include modules for implementing other process steps of the above-mentioned access token generation and verification method embodiments, for example, it may also include modules for implementing Figures 2 to 4 The modules of each process step in the embodiment of the method for generating and verifying an access token shown in the figure, and the specific principles of each module can be referred to the description of the embodiments of the method for generating and verifying an access token mentioned above, and will not be repeated here.
[0107] As described above, the access token generation and verification device of the present invention stores user information in Bencoding format, which can reduce the storage space occupied by the access token and reduce resource consumption; by improving the Bencoding encoding method and adding the encoding storage of date data, the user information and the token validity period can be quickly and synchronously obtained according to the access token, without the need for secondary processing of the time data, which is particularly suitable for high-concurrency scenarios, improves response efficiency, and enhances system performance; by compressing the target data, the space occupied by the access token can be further reduced, and by encrypting the target data, the security of user information can be ensured and user information leakage can be prevented; in addition, by storing the access token in the client's Cookie, it is achieved that regardless of whether the request sent by the client is an AJAX or AXIOS request, the server can obtain the user information by parsing the access token in the Cookie.
[0108] An embodiment of the present invention further provides an electronic device, including a processor and a memory, wherein executable instructions are stored in the memory, and when the executable instructions are executed by the processor, the method for generating and verifying an access token described in any of the above embodiments is implemented.
[0109] The electronic device of the present invention is configured on the server side, and can reduce the storage space occupied by the access token and reduce resource consumption by storing user information in the Bencoding format; by improving the Bencoding encoding method and adding the encoding storage of date data, the user information and the token validity period can be quickly and synchronously obtained according to the access token, without the need for secondary processing of the time data, and is particularly suitable for high-concurrency scenarios, improving response efficiency and enhancing system performance; by compressing the target data, the space occupied by the access token can be further reduced, and by encrypting the target data, the security of the user information can be ensured and the leakage of the user information can be prevented; in addition, by storing the access token in the cookie of the client, it is achieved that no matter whether the request sent by the client is an AJAX or AXIOS request, the server side can obtain the user information by parsing the access token in the cookie.
[0110] Figure 6 is a schematic diagram of the structure of an electronic device in an embodiment of the present invention. It should be understood that: Figure 6 The modules are only schematically shown, and these modules may be virtual software modules or actual hardware modules. The combination and splitting of these modules and the addition of other modules are all within the protection scope of the present invention.
[0111] like Figure 6As shown, the electronic device 600 is in the form of a general computing device. The components of the electronic device 600 include but are not limited to: at least one processing unit 610, at least one storage unit 620, a bus 630 connecting different platform components (including the storage unit 620 and the processing unit 610), a display unit 640, etc.
[0112] The storage unit stores program codes, which can be executed by the processing unit 610, so that the processing unit 610 performs the steps of the access token generation and verification method described in any of the above embodiments. For example, the processing unit 610 can execute the following steps: Figures 1 to 4 Steps shown.
[0113] The storage unit 620 may include a readable medium in the form of a volatile storage unit, such as a random access memory unit (RAM) 6201 and / or a cache memory unit 6202 , and may further include a read-only memory unit (ROM) 6203 .
[0114] The storage unit 620 may also include a program / utility 6204 having one or more program modules 6205, such program modules 6205 including but not limited to: an operating system, one or more application programs, other program modules, and program data, each of which or some combination may include an implementation of a network environment.
[0115] Bus 630 may represent one or more of several types of bus structures, including a memory unit bus or memory unit controller, a peripheral bus, an accelerated graphics port, a processing unit, or a local bus using any of a variety of bus architectures.
[0116] The electronic device 600 can also communicate with one or more external devices 700, which can be one or more of a keyboard, a pointing device, a Bluetooth device, etc. These external devices 700 enable the user to communicate interactively with the electronic device 600. The electronic device 600 can also communicate with one or more other computing devices, and the computer device shown includes a router and a modem. This communication can be performed through an input / output (I / O) interface 650. In addition, the electronic device 600 can also communicate with one or more networks (such as a local area network (LAN), a wide area network (WAN) and / or a public network, such as the Internet) through a network adapter 660. The network adapter 660 can communicate with other modules of the electronic device 600 through a bus 630. It should be understood that, although not shown in the figure, other hardware and / or software modules can be used in conjunction with the electronic device 600, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data backup storage platforms.
[0117] The embodiment of the present invention also provides a computer-readable storage medium for storing a program, which, when executed, implements the method for generating and verifying an access token described in any of the above embodiments. In some possible implementations, various aspects of the present invention may also be implemented in the form of a program product, which includes a program code, and when the program product is run on a terminal device, the program code is used to enable the terminal device to execute the method for generating and verifying an access token described in any of the above embodiments.
[0118] The computer-readable storage medium of the present invention is configured on the server side, and the user information is stored in the Bencoding format, which can reduce the storage space occupied by the access token and reduce resource consumption; by improving the Bencoding encoding method and adding the encoding storage of date data, the user information and the token validity period can be quickly and synchronously obtained according to the access token, without the need for secondary processing of the time data, which is particularly suitable for high-concurrency scenarios, improves response efficiency, and enhances system performance; by compressing the target data, the space occupied by the access token can be further reduced, and by encrypting the target data, the security of the user information can be ensured and the leakage of the user information can be prevented; in addition, by storing the access token in the Cookie of the client, it is achieved that regardless of whether the request sent by the client is an AJAX or AXIOS request, the server can obtain the user information by parsing the access token in the Cookie.
[0119] The program product may be a portable compact disk read-only memory (CD-ROM) and include program code, and may be run on a terminal device, such as a personal computer. However, the program product of the present invention is not limited thereto, and may be any tangible medium containing or storing a program that may be used by or in conjunction with an instruction execution system, apparatus, or device.
[0120] The program product may adopt any combination of one or more readable media. The readable medium may be a readable signal medium or a readable storage medium. The readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of readable storage media include, but are not limited to: an electrical connection with one or more wires, a portable disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above.
[0121] The readable storage medium may include a data signal propagated in a baseband or as part of a carrier wave, wherein the readable program code is carried. The data signal of this propagation may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. The readable storage medium may also be any readable medium other than a readable storage medium, which may send, propagate, or transmit a program for use by or in conjunction with an instruction execution system, device, or device. The program code contained on the readable storage medium may be transmitted using any suitable medium, including but not limited to wireless, wired, optical cable, RF, etc., or any suitable combination of the above.
[0122] Program code for performing the operations of the present invention may be written in any combination of one or more programming languages, including object-oriented programming languages such as Java, C++, etc., and conventional procedural programming languages such as "C" or similar programming languages. The program code may be executed entirely on the user computing device, partially on the user device, as a separate software package, partially on the user computing device and partially on a remote computing device, or entirely on a remote computing device or server. In the case of a remote computing device, the remote computing device may be connected to the user computing device through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computing device, such as through the Internet using an Internet service provider.
[0123] The above contents are further detailed descriptions of the present invention in combination with specific preferred embodiments, and it cannot be determined that the specific implementation of the present invention is limited to these descriptions. For ordinary technicians in the technical field to which the present invention belongs, several simple deductions or substitutions can be made without departing from the concept of the present invention, which should be regarded as falling within the protection scope of the present invention.
Claims
1. A method for generating and verifying an access token, characterized in that: include: In response to a login request from a client, obtaining user information according to the login request; Bencoding the user information and token validity period to generate target data stored in a Bencoding format, wherein the target data includes the user information data and token validity period data stored in a Bencoding format; The token validity period includes the token issuance time and the token expiration time of the date data type; the token validity period is B-encoded, including: adding an identifier t to the beginning of the token issuance time and the token expiration time and adding an identifier e to the end in the encoding mode of t[date data]e, respectively, to generate token validity period data stored in the Bencoding format of t[token issuance time]e and t[token expiration time]e; Generate an access token according to the target data, and return first response data carrying the access token to the client; In response to an access request carrying the access token, obtaining current user information according to the access request, and obtaining the user information and the token validity period according to the access token; Checking in sequence whether the token validity period is valid and whether the current user information is updated compared with the user information; If the token expires, an error message is returned to the client. If the token validity period is valid and the current user information is not updated, returning second response data corresponding to the access request to the client; If the token validity period is valid and the current user information has been updated, an update token is generated based on the current user information, and the second response data carrying the update token is returned to the client.
2. The method for generating and verifying an access token according to claim 1, wherein: The user information includes multiple string-type information contents; The user information is B-encoded, including: The character string length in bytes of each piece of information content is obtained in an encoding mode of [character string length: character string], and user information data is generated and stored in a Bencoding format of [byte number: information content] for each piece of information content.
3. The method for generating and verifying an access token according to claim 1, wherein: The generating an access token according to the target data includes: compressing the target data to obtain compressed target data; Encrypting the compressed target data to obtain encrypted target data; The encrypted target data is encoded to generate the access token.
4. The method for generating and verifying an access token as claimed in claim 3, wherein: When compressing the target data, a GZIP compression algorithm is used; and / or When encrypting the compressed target data, using SM4 encryption algorithm; and / or When encoding the encrypted target data, a Base64 encoding algorithm is used.
5. The method for generating and verifying an access token according to claim 1, wherein: The returning the first response data carrying the access token to the client includes: The access token is placed in the Set-Cookie field of the response header of the first response data, so that the client automatically saves the access token to the Cookie after receiving the first response data, and the access token is automatically carried in subsequent requests sent by the client.
6. The method for generating and verifying an access token according to claim 1, wherein: After the update token is generated based on the current user information, the access token is added to the blacklist pool; After verifying that the token validity period is valid, determine whether the access token is in the blacklist pool, if so, return the error message to the client, if not, verify whether the current user information is updated.
7. The method for generating and verifying an access token according to claim 1, wherein: The obtaining the user information and the token validity period according to the access token includes: Processing the access token using a reverse process of a generation process of the access token to obtain the target data; The target data is B-decoded to obtain the user information and the validity period of the token.
8. A device for generating and verifying an access token, characterized in that: include: A request response module, configured to respond to a login request from a client and obtain user information according to the login request; A Bencoding module configured to Bencode the user information and the token validity period to generate target data stored in a Bencoding format, wherein the target data includes the user information data and the token validity period data stored in a Bencoding format; The token validity period includes the token issuance time and the token expiration time of the date data type; the Bencoding module performs B encoding on the token validity period, including: adding an identifier t to the header of the token issuance time and the token expiration time and adding an identifier e to the tail in the encoding mode of t[date data]e, respectively, to generate token validity period data stored in the Bencoding format of t[token issuance time]e and t[token expiration time]e; A token generation module, configured to generate an access token according to the target data, and return first response data carrying the access token to the client; The verification module is configured to: respond to an access request carrying the access token, obtain current user information according to the access request, and obtain the user information and the token validity period according to the access token; verify in turn whether the token validity period is valid and whether the current user information is updated compared to the user information; if the token validity period expires, return an error message to the client; if the token validity period is valid and the current user information is not updated, return second response data corresponding to the access request to the client; if the token validity period is valid and the current user information has not been updated, generate an updated token based on the current user information, and return the second response data carrying the updated token to the client.
9. An electronic device, characterized in that: include: A processor; a memory storing executable instructions; Wherein, when the executable instructions are executed by the processor, the method for generating and verifying an access token as described in any one of claims 1 to 7 is implemented.
10. A computer-readable storage medium for storing a program, characterized in that: When the program is executed by a processor, the method for generating and verifying an access token as described in any one of claims 1 to 7 is implemented.
