A token generation and verification method and device
By generating and managing the correspondence between client identification and key in the server, combined with the encryption and compression processing in BSON format, the problems of token key uniformity and information security in the prior art are solved, and the token multiples are realized Key issuance and dynamic updates improve information security and system performance.
Patent Information
- Application Number
- CN202111616912.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-27
- Publication Date
- 2024-11-15
- Estimated Expiration
- 2041-12-27
AI Technical Summary
In the prior art, the unified key of the token causes all the original tokens to be invalid when the key is updated, and the token information is not encrypted and compressed, which is easily stolen by the middleman, and the information security is insufficient.
By receiving client data, generating tokens based on preset compression and encryption rules, and storing the correspondence between client identification and key in the server cache, multi-key issuance and dynamic key update are realized, and encryption is performed using BSON format Compression processing.
Improves the security of token information passing, avoids leakage, minimizes back-end storage, reduces resource usage, and provides better security performance and flexibility.
Smart Images

Figure CN114401094B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of communication security technology, and in particular to a token generation method, a verification method and a device. Background Art
[0002] In Internet applications, the server generally needs to perform user verification and authentication on the request sent by the client to ensure service and data security. Then, how to perform user verification is a technical problem that needs to be solved urgently by those skilled in the art. Summary of the invention
[0003] The embodiment of the present invention provides a token generation method, a verification method and a device, which are used to implement user verification and improve the security of interaction.
[0004] In a first aspect, an embodiment of the present invention provides a method for generating a token, comprising:
[0005] Receive first data sent by a client; wherein the first data includes: an identifier of the client and user information of the client;
[0006] Determine data information according to the first data, and determine header information according to the first data, a preset compression rule and a preset encryption rule;
[0007] According to the compression rule, compress the data information to obtain compressed data;
[0008] According to the determined key corresponding to the identifier of the client, the compressed data is encrypted according to the encryption rule to obtain encrypted data;
[0009] The encrypted data and the header information are respectively encoded to generate the token.
[0010] Optionally, in the embodiment of the present invention, the data format of the header information and the data information is BSON.
[0011] Optionally, in an embodiment of the present invention, when a correspondence between an identifier and a key is stored in a cache of the server, a method for determining a key corresponding to the identifier of the client specifically includes:
[0012] Determining whether the identifier of the client is found in the cache;
[0013] If so, extracting the key corresponding to the identifier of the client from the cache;
[0014] If not, a key corresponding to the identifier of the client is generated, and the identifier of the client and the corresponding key are stored in the cache.
[0015] Optionally, in the embodiment of the present invention, the method further includes:
[0016] According to a preset key update rule, the key corresponding to the client identifier that has been determined is updated, and the token is updated according to the updated key;
[0017] The key update rule includes:
[0018] When the header information includes a preset key validity period, the key validity period expires;
[0019] And / or, determining that the generated token is invalid.
[0020] In a second aspect, an embodiment of the present invention provides a token verification method, comprising:
[0021] Obtain a token, where the token is generated using the above method provided in the embodiment of the present invention;
[0022] Decoding the token to obtain header information and encrypted data; and determining the identity of the client based on the header information;
[0023] When the key corresponding to the identifier of the client is found in the cache, the encrypted data is parsed according to the header information and the found key;
[0024] Based on the result of the parsing process, it is determined whether the token is valid.
[0025] Optionally, in the embodiment of the present invention, when the header information includes a preset key period validity period, determining whether the token is valid according to the result of the parsing process specifically includes:
[0026] Determine whether the result of the parsing process is a successful parsing;
[0027] If yes, determining the validity period of the key from the header information, and determining that the token is valid when it is determined that the validity period of the key has not expired;
[0028] If not, the token is determined to be invalid.
[0029] Optionally, in the embodiment of the present invention, the method further includes:
[0030] When it is determined that the validity period of the key has expired, the token is determined to be invalid.
[0031] Optionally, in the embodiment of the present invention, the method further includes:
[0032] When the key corresponding to the identifier of the client is not found in the cache, it is determined that the token is invalid.
[0033] In a third aspect, an embodiment of the present invention provides an electronic device, including:
[0034] A memory for storing program instructions;
[0035] The processor is used to call the program instructions stored in the memory and execute the above method provided by the embodiment of the present invention according to the obtained program.
[0036] In a fourth aspect, an embodiment of the present invention provides a readable storage medium, wherein the readable storage medium stores electronic device executable instructions, wherein the electronic device executable instructions are used to enable the electronic device to execute the above method provided by the embodiment of the present invention.
[0037] In a fifth aspect, an embodiment of the present invention provides a computer device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the steps of the above method provided in the embodiment of the present invention are implemented.
[0038] The beneficial effects of the present invention are as follows:
[0039] The embodiments of the present invention provide a token generation method, verification method and device. When generating a token, the security of the information transmitted in the token can be improved by compressing and encrypting the data information to avoid leakage. At the same time, the content stored in the back-end can be minimized to reduce resource usage. Moreover, since the client's identifier and the key are set correspondingly, instead of using a unified key as in the prior art, verification of multi-key issuance can be achieved, providing better security performance. In addition, when verifying the token, since the token has been compressed and encrypted, the security of the information can be improved while realizing user verification. BRIEF DESCRIPTION OF THE DRAWINGS
[0040] Figure 1 A flowchart of a method for generating a token provided in an embodiment of the present invention;
[0041] Figure 2 A flowchart of a specific embodiment provided in an embodiment of the present invention;
[0042] Figure 3 A flowchart of another specific embodiment provided in an embodiment of the present invention;
[0043] Figure 4 A flowchart of a token verification method provided in an embodiment of the present invention;
[0044] Figure 5 A flowchart of another specific embodiment provided in the embodiments of the present invention;
[0045] Figure 6 The present invention is a schematic diagram of the structure of an electronic device provided in an embodiment of the present invention. DETAILED DESCRIPTION
[0046] The following will be combined with the accompanying drawings to describe in detail the specific implementation of a token generation method, verification method and device provided by an embodiment of the present invention. It should be noted that the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention.
[0047] The inventors found in their research that the earliest authentication was based on Session (i.e., the instant domain), but this authentication method has poor scalability and is easily intercepted and forged. In addition, as the number of authenticated users increases, the server overhead will increase significantly.
[0048] JWT (JSON Web Token) solves the above problems and has its own characteristics of high security, good performance and easy scalability, becoming one of the most popular cross-domain identity verification solutions. JWT itself is a string. The user's basic information and the signature generated by the algorithm for tamper-proofing can be Base64-encoded to obtain JWT. In the process of interaction between the client and the server, the client will first obtain the JWT token generated by the server during the login operation. After that, each request from the client will carry the JWT token. The server can determine the client's identity by parsing the JWT token in the request. In this way, the server can determine the identity of the requester without accessing the database, and the requester is not restricted to a certain host, which is more convenient for distributed systems.
[0049] Therefore, JWT has the following advantages over traditional authentication methods:
[0050] 1. JWT authentication is relatively safe because it introduces digital signatures, which allows the server to verify whether the JWT information has been modified;
[0051] 2. JWT is stateless, and the server does not need to store Session information, which reduces the pressure on the server;
[0052] 3. It does not need to be bound to a specific identity verification system and can be generated anywhere.
[0053] However, JWT currently has the following two problems:
[0054] 1) Tokens issued by the same platform / certification center use a unified key. Once the key needs to be updated, all previously issued tokens will become invalid;
[0055] 2) The information transmitted in the token is not encrypted and compressed, the information can be seen by the middleman, and the length of the token may be too long.
[0056] Based on this, an embodiment of the present invention provides a token generation method, a verification method and a device, which are used to support the verification of multi-key issuance, and can minimize the content stored in the backend, improve the security of the information transmitted in the token, and avoid leakage.
[0057] Specifically, an embodiment of the present invention provides a method for generating a token, such as Figure 1 As shown, applied to the server side, it may include:
[0058] S101, receiving first data sent by a client; wherein the first data includes: an identifier of the client and user information using the client;
[0059] The user information may be information of a user using the client, and the specific information content is not limited here.
[0060] S102, determining data information according to the first data, and determining header information according to the first data, a preset compression rule, and a preset encryption rule;
[0061] S103, compressing the data information according to the compression rule to obtain compressed data;
[0062] The specific compression rules may be set according to actual needs and are not limited here.
[0063] S104, encrypting the compressed data according to the determined key corresponding to the identifier of the client and the encryption rule to obtain encrypted data;
[0064] The specific encryption rules may be set according to actual needs and are not limited here.
[0065] S105. Encode the encrypted data and the header information respectively to generate the token.
[0066] Specifically, when performing the encoding process, the Base64 encoding method may be used for encoding, but it is certainly not limited thereto, and other encoding methods may also be used, which are not limited here.
[0067] In this way, when generating a token, by compressing and encrypting the data information, the security of the information transmitted in the token can be improved to avoid leakage. At the same time, the content stored in the backend can be minimized to reduce resource usage. Moreover, since the client's identification and key are set in correspondence, instead of using a unified key as in the prior art, it is possible to implement verification of multi-key issuance, providing better security performance.
[0068] Optionally, in the embodiment of the present invention, the data format of the header information and the data information is BSON.
[0069] Thus, compared with the existing jwt token, the present invention uses the binary BSON data format to store information. BSON has better performance than the traditional JSON and increases the efficiency of encryption compression for the Payload (i.e. the data information mentioned in the previous content) domain.
[0070] Of course, in actual situations, the data format of the header information and the data information is not limited to BSON, and may be other data formats, which are not limited here.
[0071] Optionally, in an embodiment of the present invention, when a correspondence between an identifier and a key is stored in a cache of the server, a method for determining a key corresponding to the identifier of the client specifically includes:
[0072] Determining whether the identifier of the client is found in the cache;
[0073] If so, extracting the key corresponding to the identifier of the client from the cache;
[0074] If not, a key corresponding to the identifier of the client is generated, and the identifier of the client and the corresponding key are stored in the cache.
[0075] Since no token information is stored in the backend application in the prior art, it is impossible to invalidate an already issued token. In the embodiment of the present invention, since a key corresponding to the client's identifier is stored in the server's cache, it is convenient to subsequently update the token when the already issued token expires.
[0076] Furthermore, when the client identifier does not exist in the cache, a key can be generated according to a preset key generation method, a corresponding relationship between the key and the client identifier is established, and the key is stored in the cache to facilitate subsequent token verification.
[0077] Specifically, in the embodiment of the present invention, the preset key generation method may be any method for generating a key known to those skilled in the art, and is not limited here.
[0078] Optionally, in the embodiment of the present invention, the method further includes:
[0079] According to a preset key update rule, the key corresponding to the client identifier that has been determined is updated, and the token is updated according to the updated key;
[0080] The key update rule includes:
[0081] When the header information includes a preset key validity period, the key validity period expires;
[0082] And / or, determining that the generated token is invalid.
[0083] In this way, dynamic key updates can be achieved, while supporting forced token expiration and reducing the risks caused by token leakage.
[0084] Optionally, in the embodiment of the present invention, the method further includes:
[0085] The server can send the generated token to the client.
[0086] This makes it easy for the client to carry the token in the request sent to the server, thereby implementing the verification and authentication process.
[0087] The above method provided by the embodiment of the present invention is described below in conjunction with specific embodiments.
[0088] Example: Combination Figure 2 As shown, for the token generation process, the execution subject of the following steps is the server.
[0089] S201. When a user successfully logs into a client, receiving first data sent by the client; wherein the first data includes: an identifier of the client and user information using the client;
[0090] S202: Determine data information in a BSON format according to the first data, and determine header information in a BSON format according to the first data, a preset compression rule, and a preset encryption rule;
[0091] For example, if the client ID is 1376360834669744129, the compression rule is GZIP, and the encryption rule is AES256, the generated header information is as follows:
[0092] R\x00\x00\x00\x02alg\x00\x07\x00\x00\x00AES256\x00\x02typ\x00\x04\x00\x00\x00JWT\x00\x02k ey\x00\x14\x00\x00\x001376360834669744129\x00\x02compress\x00\x05\x00\x00\x00GZIP\x00\x00.
[0093] S203, compressing the data information according to the compression rule to obtain compressed data;
[0094] S204, determine whether there is a client identifier in the cache; if not, execute S205; if so, execute S206;
[0095] S205, generating a key corresponding to the identifier of the client, and storing the identifier of the client and the corresponding key in the cache; executing S206;
[0096] S206, encrypting the compressed data according to the key corresponding to the identifier of the client and the encryption rule to obtain encrypted data;
[0097] S207. Perform Base64 encoding on the encrypted data and the header information respectively to generate the token.
[0098] Example: Combination Figure 3 As shown, for the key update process, the execution subject of the following steps is the server.
[0099] S301, obtaining the client's identifier;
[0100] S302, determine whether there is a key corresponding to the client's identifier in the cache; if so, execute S303; if not, end the process;
[0101] S303: When it is determined according to the key update rule that the key needs to be updated, the key is updated.
[0102] To illustrate, when the key is updated, the key corresponding to the client's identifier stored in the cache is deleted, the updated key is stored in the cache, and a corresponding relationship between the updated key and the client's identifier is formed.
[0103] Based on the same inventive concept, an embodiment of the present invention provides a token verification method, such as Figure 4 As shown, applied to the server side, it may include:
[0104] S401, obtaining a token, wherein the token is generated by the method provided in the embodiment of the present invention;
[0105] The token may be obtained from, but is not limited to, a cookie.
[0106] S402, decoding the token to obtain header information and encrypted data; and determining the identifier of the client based on the header information;
[0107] S403, when a key corresponding to the identifier of the client is found in the cache, the encrypted data is parsed according to the header information and the found key;
[0108] Specifically, the parsing process may include: decompression processing and decryption processing.
[0109] Since the header information includes encryption rules and compression rules, the encrypted data can also be parsed using these rules.
[0110] S404: Determine whether the token is valid according to the result of the parsing process.
[0111] In this way, since the client's identification is set corresponding to the key, instead of using a unified key as in the prior art, it is possible to implement verification of multi-key issuance and provide better security performance; in addition, when verifying the token, since the token has been compressed and encrypted, while achieving user verification, it can also improve information security and avoid leakage, and at the same time minimize the content of back-end storage and reduce resource usage.
[0112] Optionally, in the embodiment of the present invention, when the header information includes a preset key period validity period, determining whether the token is valid according to the result of the parsing process specifically includes:
[0113] Determine whether the result of the parsing process is a successful parsing;
[0114] If yes, determining the validity period of the key from the header information, and determining that the token is valid when it is determined that the validity period of the key has not expired;
[0115] If not, the token is determined to be invalid.
[0116] The validity period of the key can be set according to actual needs and is not limited here.
[0117] In this way, it is possible to determine whether the token is valid based on the results of the parsing process, thereby realizing the verification process; when the token is valid and the user information is extracted from the token, a subsequent user authentication process can be performed, thereby improving the security of the interaction between the server and the client.
[0118] It should be noted that if the parsing fails, it means that the token obtained is not the token that has been generated and issued by the server before, and the key of the token is different from the key of the previously issued token, which leads to the unsuccessful parsing. This situation indicates that the token may have been leaked. In this way, the security of the interaction process can be effectively improved.
[0119] Optionally, in the embodiment of the present invention, the method further includes:
[0120] When it is determined that the validity period of the key has expired, the token is determined to be invalid.
[0121] Optionally, in the embodiment of the present invention, the method further includes:
[0122] When the key corresponding to the identifier of the client is not found in the cache, it is determined that the token is invalid.
[0123] This situation indicates that the key may have expired and therefore cannot be found in the cache.
[0124] In this way, the validity of the token can be judged from multiple angles, thereby improving the accuracy of the verification results and improving security performance.
[0125] It should be noted that, optionally, in the embodiment of the present invention, the token verification process can be applied to the interaction process between the server and the client, for example:
[0126] The client sends a request message carrying a token to the server;
[0127] When the server receives the request information, it extracts the token and verifies the token;
[0128] When the verification result is that the token is valid, the identity information of the client (such as but not limited to information including the client's identification) can be extracted from the token;
[0129] Authenticate the extracted identity information;
[0130] When the authentication result is legal, reply to the request information sent by the client.
[0131] Of course, when the authentication result is illegal, and / or the verification result is that the token is invalid, the server will not respond to the request sent by the client.
[0132] Furthermore, when the verification result is that the token is invalid, subsequent authentication will not be performed. Authentication will only be performed when the verification result is that the token is valid.
[0133] The above method provided by the embodiment of the present invention is described below in conjunction with specific embodiments.
[0134] Example: Combination Figure 5 As shown, for the token verification process, the execution subject of the following steps is the server.
[0135] S501, obtaining a token;
[0136] S502, decoding the token to obtain header information and encrypted data; extracting the client identifier from the header information;
[0137] S503, determining whether the key corresponding to the extracted client identifier is stored in the cache; if so, executing S504; if not, executing S508;
[0138] S504, decrypting and decompressing the encrypted data according to the encryption rule and compression rule extracted from the header information and the key found from the cache;
[0139] S505, determine whether the decryption process and the decompression process are successfully completed; if so, execute S506; if not, execute S508;
[0140] S506, determine whether the key validity period in the header information has expired; if not, execute S507; if yes, execute S508;
[0141] S507, determine that the token is valid; end the process;
[0142] S508: Determine whether the token is invalid.
[0143] Based on the same inventive concept, an embodiment of the present invention provides an electronic device, the implementation principle of the electronic device is similar to the implementation principle of the aforementioned method. The specific implementation of the electronic device can refer to the specific embodiment of the aforementioned method, and the repeated parts will not be repeated.
[0144] Specifically, an embodiment of the present invention provides an electronic device, such as Figure 6 As shown, including:
[0145] Memory 601, used for storing program instructions;
[0146] The processor 602 is used to call the program instructions stored in the memory 601 and execute the above method provided in the embodiment of the present invention according to the obtained program.
[0147] Optionally, in the embodiment of the present invention, the electronic device may be, but is not limited to, a server.
[0148] Based on the same inventive concept, an embodiment of the present invention provides a readable storage medium, which stores electronic device executable instructions, and the electronic device executable instructions are used to enable the electronic device to execute the above method provided by the embodiment of the present invention.
[0149] Based on the same inventive concept, an embodiment of the present invention provides a computer device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the steps of the above method provided in the embodiment of the present invention are implemented.
[0150] It should be emphasized that the above solution provided by the embodiment of the present invention has the following advantages:
[0151] 1) Compared with the existing completely stateless jwt token, this solution can realize the functions of multi-key issuance verification, dynamic update of token key, and forced token expiration by introducing a part of the state (that is, the state where the client's identifier corresponds to the key one by one).
[0152] 2) Compared with the existing jwt token, this solution uses binary BSON format to store information. BSON has better performance than traditional JSON and adds encryption compression for the Payload field. Since the Payload field is generally used to pass parameters and occupies more content, compression can effectively reduce the length of the generated token. Encryption allows Payload to pass more content, thereby providing more efficient system performance and better security performance.
[0153] 3) In the case of cloud computing, this solution can maximize the use of the computing power of distributed clusters, while providing better security performance with the support of multi-key issuance and verification capabilities.
[0154] Obviously, those skilled in the art can make various changes and modifications to the present invention without departing from the spirit and scope of the present invention. Thus, if these modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalents, the present invention is also intended to include these modifications and variations.
Claims
1. A method for generating a token, characterized in that: include: Receive first data sent by a client; wherein the first data includes: an identifier of the client and user information of the client; Determine data information according to the first data, and determine header information according to the first data, a preset compression rule and a preset encryption rule; According to the compression rule, compress the data information to obtain compressed data; According to the determined key corresponding to the identifier of the client, the compressed data is encrypted according to the encryption rule to obtain encrypted data; Encoding the encrypted data and the header information respectively to generate the token; The data format of the header information and the data information is BSON; The generating method further comprises: According to a preset key update rule, the key corresponding to the client identifier that has been determined is updated, and the token is updated according to the updated key; The key update rule includes: When the header information includes a preset key validity period, the key validity period expires; And / or, determining that the generated token is invalid.
2. The generation method according to claim 1, characterized in that: When the correspondence between the identifier and the key is stored in the cache of the server, the method for determining the key corresponding to the identifier of the client specifically includes: Determining whether the identifier of the client is found in the cache; If so, extracting the key corresponding to the identifier of the client from the cache; If not, a key corresponding to the identifier of the client is generated, and the identifier of the client and the corresponding key are stored in the cache.
3. A token verification method, characterized in that: include: Obtaining a token, wherein the token is generated by the method according to claim 1 or 2; Decoding the token to obtain header information and encrypted data; and determining the identity of the client based on the header information; When the key corresponding to the identifier of the client is found in the cache, the encrypted data is parsed according to the header information and the found key; Based on the result of the parsing process, it is determined whether the token is valid.
4. The verification method according to claim 3, characterized in that: When the header information includes a preset key period validity period, determining whether the token is valid according to the result of the parsing process specifically includes: Determine whether the result of the parsing process is a successful parsing; If yes, determining the validity period of the key from the header information, and determining that the token is valid when it is determined that the validity period of the key has not expired; If not, the token is determined to be invalid.
5. The verification method according to claim 4, characterized in that: Also includes: When it is determined that the validity period of the key has expired, the token is determined to be invalid.
6. The verification method according to claim 5, characterized in that: Also includes: When the key corresponding to the identifier of the client is not found in the cache, it is determined that the token is invalid.
7. An electronic device, characterized in that: include: A memory for storing program instructions; A processor is used to call the program instructions stored in the memory and execute the method according to any one of claims 1 to 6 according to the obtained program.
8. A readable storage medium, characterized in that: The readable storage medium stores electronic device executable instructions, and the electronic device executable instructions are used to enable the electronic device to execute the method according to any one of claims 1-6.
9. A computer device comprising a memory, a processor and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the program, the steps of the method according to any one of claims 1 to 6 are implemented.
