Identity authentication method and device
By performing asymmetric key encryption and decryption verification on jwt tokens, the problem of jwt tokens being easily stolen and tampered is solved, improving the security and effectiveness of identity authentication, and reducing storage and network overhead.
Patent Information
- Application Number
- CN202210510615.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-05-11
- Publication Date
- 2024-11-26
- Estimated Expiration
- 2042-05-11
AI Technical Summary
The existing jwt-based identity authentication method is prone to theft and tampering of tokens, resulting in reduced security and effectiveness of identity authentication.
By asymmetric key encryption of the jwt token, using public key encryption and private key decryption, the validity of the token is verified, ensuring the security of the token during transmission and storage.
Improves the security and effectiveness of identity authentication, avoids token theft and tampering, and reduces storage and network overhead.
Smart Images

Figure CN114826757B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of communications, and more specifically, to an identity authentication method and device. Background Art
[0002] At present, most projects and systems involve the need for identity authentication and session management. The implementation mechanisms can be divided into two categories: session-based identity authentication and token-based identity authentication. Session-based identity authentication may have problems such as memory consumption and high network overhead; while token-based identity authentication may have problems such as low security and poor user experience.
[0003] The current existing technology adopts a jwt-based identity authentication method to solve the above problem. However, since this method is to splice a one-time password string behind the original jwt token, and the server verifies the identity of the client based on the one-time password string, this method can easily lead to the jwt token being stolen and tampered with, thereby greatly reducing the security and effectiveness of identity authentication. Summary of the invention
[0004] The embodiments of the present application provide an identity authentication method and device, which can prevent the JWT token from being stolen and tampered with by encrypting the JWT token, thereby improving the security and effectiveness of identity authentication.
[0005] In view of this, an embodiment of the present application provides an identity authentication method, including:
[0006] Receive a resource request sent by a client, wherein the resource request includes a jwt encrypted token, and the jwt encrypted token is pre-encrypted by the server using a public key in an asymmetric key pair;
[0007] Decrypt the jwt encrypted token using the private key in the asymmetric key pair to obtain a decrypted jwt token;
[0008] Verify the decrypted jwt token to obtain a verification result;
[0009] If the verification result is valid, resource information corresponding to the resource request is returned to the client.
[0010] Optionally, before receiving the resource request sent by the client, the method further includes:
[0011] receiving a login request from a client, wherein the login request includes user information;
[0012] Generate a jwt token according to the user information, wherein the jwt token includes a random status bit, IP information and an expiration time;
[0013] Encrypt the jwt token using the public key in the asymmetric key pair to obtain the jwt encrypted token;
[0014] Return the jwt encrypted token to the client.
[0015] Optionally, the verifying the decrypted jwt token to obtain a verification result includes:
[0016] According to the expiration time and system time, determine whether the decrypted jwt token is expired;
[0017] If the decrypted jwt token expires, the verification result is invalid;
[0018] If the decrypted jwt token has not expired, determine whether the IP information in the decrypted jwt token is consistent with the IP information carried in the resource request;
[0019] If the IP information in the decrypted jwt token is inconsistent with the IP information carried in the resource request, the verification result is invalid;
[0020] If the IP information in the decrypted jwt token is consistent with the IP information carried in the resource request, determine whether the random status bit exists in the preset status bit table;
[0021] If the random status bit does not exist in the preset status bit table, the verification result is invalid;
[0022] If the random status bit exists in the preset status bit table, the verification result is valid.
[0023] Optionally, before receiving the resource request sent by the client, the method further includes:
[0024] receiving a login request from a client, wherein the login request includes user information;
[0025] Generate an initial jwt token according to the user information, wherein the initial jwt token includes a random status bit, IP information, and an expiration time;
[0026] Add a refresh time string to the initial jwt token to obtain a jwt token;
[0027] Encrypt the jwt token using the public key to obtain the jwt encrypted token;
[0028] Return the jwt encrypted token to the client.
[0029] Optionally, the method further comprises:
[0030] According to the refresh time string and the system time, determine whether the decrypted jwt token is within the validity period;
[0031] If the resolved jwt token is not within the validity period, return to execute the step of generating an initial jwt token based on the user information.
[0032] The present application also provides an identity authentication device, including:
[0033] A first receiving unit is used to receive a resource request sent by a client, wherein the resource request includes a jwt encrypted token, and the jwt encrypted token is pre-encrypted by the server using a public key in an asymmetric key pair;
[0034] A decryption unit, used to decrypt the jwt encrypted token using the private key in the asymmetric key pair to obtain a decrypted jwt token;
[0035] A verification unit, used to verify the decrypted jwt token and obtain a verification result;
[0036] The first returning unit is configured to return resource information corresponding to the resource request to the client if the verification result is valid.
[0037] Optionally, the device further comprises:
[0038] A second receiving unit, configured to receive a login request from a client, wherein the login request includes user information;
[0039] A generating unit, configured to generate a jwt token according to the user information, wherein the jwt token includes a random status bit, IP information, and an expiration time;
[0040] An encryption unit, used to encrypt the jwt token using the public key in the asymmetric key pair to obtain the jwt encrypted token;
[0041] The second returning unit is used to return the jwt encrypted token to the client.
[0042] Optionally, the verification unit is specifically used to:
[0043] According to the expiration time and system time, determine whether the decrypted jwt token is expired;
[0044] If the decrypted jwt token expires, the verification result is invalid;
[0045] If the decrypted jwt token has not expired, determine whether the IP information in the decrypted jwt token is consistent with the IP information carried in the resource request;
[0046] If the IP information in the decrypted jwt token is inconsistent with the IP information carried in the resource request, the verification result is invalid;
[0047] If the IP information in the decrypted jwt token is consistent with the IP information carried in the resource request, determine whether the random status bit exists in the preset status bit table;
[0048] If the random status bit does not exist in the preset status bit table, the verification result is invalid;
[0049] If the random status bit exists in the preset status bit table, the verification result is valid.
[0050] Optionally, the device further comprises:
[0051] A second receiving unit, configured to receive a login request from a client, wherein the login request includes user information;
[0052] A generating unit, configured to generate an initial jwt token according to the user information, wherein the initial jwt token includes a random status bit, IP information, and an expiration time;
[0053] A splicing unit, used for splicing a refresh time string on the initial jwt token to obtain a jwt token;
[0054] An encryption unit, used to encrypt the jwt token using the public key to obtain the jwt encrypted token;
[0055] The second returning unit is used to return the jwt encrypted token to the client.
[0056] Optionally, the device further comprises:
[0057] A judging unit, used to judge whether the decrypted jwt token is within the validity period according to the refresh time string and the system time;
[0058] The execution unit is used to return to the step of generating an initial jwt token according to the user information if the resolved jwt token is not within the validity period.
[0059] The embodiment of the present application provides an identity authentication method, which includes receiving a resource request sent by a client, the resource request including a jwt encrypted token, the jwt encrypted token being pre-encrypted by a server using a public key in an asymmetric key pair; decrypting the jwt encrypted token using a private key in the asymmetric key pair to obtain a decrypted jwt token; verifying the decrypted jwt token to obtain a verification result; if the verification result is valid, returning resource information corresponding to the resource request to the client. It can be seen that the present application can prevent the jwt token from being stolen and tampered with by encrypting the generated jwt token in advance, thereby improving the security and effectiveness of identity authentication. BRIEF DESCRIPTION OF THE DRAWINGS
[0060] In order to more clearly illustrate the embodiments of the present application 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 only embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying any creative work.
[0061] Figure 1 A flowchart of an identity authentication method provided in an embodiment of the present application;
[0062] Figure 2 A flowchart of another identity authentication method provided in an embodiment of the present application;
[0063] Figure 3 A flowchart of another identity authentication method provided in an embodiment of the present application;
[0064] Figure 4 A schematic diagram of the structure of an identity authentication device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0065] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0066] The terms "first", "second", "third", "fourth", etc. (if any) in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments described herein can be implemented in an order other than that illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0067] First, some of the terms that may be involved in this application are explained:
[0068] 1) JWT: It is an open standard based on JSON (RFC 7519) for transferring declarations between network application environments. The token is designed to be compact and secure. JWT declarations are generally used to transfer authenticated user identity information between identity providers and service providers to facilitate the acquisition of resources from resource servers, and can also add some additional declaration information required by other business logic.
[0069] 2) Session authentication: In computers, especially in network applications, it is called "session control". The Session object stores the properties and configuration information required for a specific user session. In this way, when a user jumps between web pages in an application, the variables stored in the Session object will not be lost, but will continue to exist throughout the user session. When a user requests a web page from an application, if the user does not have a session yet, the web server will automatically create a Session object. When the session expires or is abandoned, the server will terminate the session. One of the most common uses of the Session object is to store user preferences. For example, if a user indicates that he does not like to view graphics, that information can be stored in the Session object. For more information on using the Session object, see "Managing Sessions" in the "ASP Applications" section. Note that session state is only retained in browsers that support cookies.
[0070] 3) Token authentication: Token refers to Oauth Token or similar mechanisms, which provide authentication and authorization. Authentication is for users, and authorization is for apps. Its purpose is to allow an app to have access to a user's information. The token here is unique. It cannot be transferred to other apps or other users. JWT is a type of token.
[0071] 4) Session management: A session is a semi-permanent interactive information exchange between two or more communication devices or between a computer and a user. The session is established at a certain point in time and then torn down at a later point in time. An established communication session can involve more than one message in each direction, which exists only in this session and is isolated from other sessions. Sessions are usually stateful, which means that at least one of the communicating parts needs to save information about the session history in order to be able to communicate.
[0072] Generally, most projects and systems involve the need for identity authentication and session management. How to quickly identify identities at a minimal cost has become a problem that needs to be considered and solved for projects and systems with large traffic and fast response requirements, especially large projects and systems.
[0073] At present, most systems can be roughly divided into two categories. One is a centralized system, which is characterized by front-end and back-end integration and based on session authentication. The other is a system with separated front-end and back-end, which is characterized by using the back-end for centralized processing and based on session authentication. Most systems use a front-end and back-end separation structure, and most back-end services are based on session authentication. In addition, for microservice architecture systems, token-based authentication is usually used. For session authentication processing, the server side often spends a certain amount of memory overhead for each session. Therefore, for the currently popular back-end multi-service and distributed systems, it is necessary to deal with the session consistency problem (there are multiple nodes in the distributed system, and each node must save the session in order to identify the user information when the same user accesses different nodes, so as to access resources), which often consumes greater memory overhead and network overhead to synchronize session information between nodes. For identity authentication using tokens, microservices use a token authentication mechanism. The token obtained is often only one-time. When the token expires, it is necessary to re-login and send verification information to the microservice to obtain a new token. If it is only in the microservice system, this behavior of repeatedly obtaining tokens is barely acceptable. However, in the front-end and back-end user identity authentication systems, there are often problems such as low security and poor user experience (the session will expire every once in a while and you need to log in again).
[0074] At present, the existing technology adopts a jwt-based identity authentication method to solve the above problems. However, since this method is to splice a one-time password string behind the original jwt token, and the server verifies the identity of the client based on the one-time password string, when the thief takes the JWT token generated and returned by the server, the thief can also simulate the request to the server on the client to obtain a one-time password, which is spliced behind the JWT token, similar to the thief holding the stolen person's key to operate, thereby realizing the token theft. And when the lawless elements get the jwt, they tamper with the algorithm signature through specific means, or directly tamper with it without verifying the algorithm signature, etc., which can often bypass the algorithm verification and parse the JWT, and even access the server resources through the JWT token replay. Therefore, this method is prone to the theft and tampering of the jwt token, which greatly reduces the security and effectiveness of identity authentication.
[0075] Therefore, in order to solve the above problems, the embodiment of the present application provides an identity authentication method, which can encrypt the jwt token to avoid the jwt token from being stolen and tampered, thereby improving the security and effectiveness of identity authentication. The method provided by the embodiment of the present application is described in detail below.
[0076] See also Figure 1 , an identity authentication method provided in an embodiment of the present application includes the following steps.
[0077] S101: Receive a resource request sent by a client.
[0078] In this embodiment, the client sends a resource request to the server to obtain a resource. The resource request includes a jwt encrypted token, identification information of the resource to be obtained, IP information of the client, etc. It can be understood that the jwt encrypted token can be obtained by the server pre-encrypting the jwt token using the public key in an asymmetric key pair. The asymmetric key pair includes a public key and a private key, which correspond to each other and are stored in the server. Specifically, after the server generates the jwt token, it can use the public key in the asymmetric key pair stored in itself to encrypt the jwt token to obtain the jwt encrypted token.
[0079] S102. Use the private key in the asymmetric key pair to decrypt the jwt encrypted token to obtain a jwt token.
[0080] In this embodiment, after obtaining the jwt encrypted token carried in the resource request sent by the client, the server can use the private key in the asymmetric key pair stored in itself to decrypt the jwt encrypted token, thereby obtaining the decrypted jwt token. It should be noted that the private key used to decrypt the jwt token corresponds to the public key used to encrypt the jwt token.
[0081] S103, verify the decrypted jwt token to obtain a verification result.
[0082] In this embodiment, after the server decrypts the jwt encrypted token to obtain the decrypted jwt token, it can verify the decrypted jwt token to obtain a verification result. It can be understood that if the decrypted jwt token is verified, the verification result is valid; if the decrypted jwt token fails to be verified, the verification result is invalid. Further, when the verification result is invalid, the server can return information that the token verification is invalid to the client.
[0083] Specifically, the jwt token may include a random state bit state, IP information, and expiration time. Among them, the random state bit state is used to determine whether the jwt token has been revoked; the IP information is used to determine whether the jwt token has been stolen; and the expiration time is used to determine whether the jwt has expired.
[0084] As an implementation manner, the above S103 may specifically be:
[0085] S1031. Determine whether the decrypted jwt token is expired based on the expiration time and the system time; if so, the verification result is invalid.
[0086] In this embodiment, the expiration time carried in the decrypted jwt token and the system time in the server can be obtained, and the two can be compared to determine whether the expiration time in the jwt token is earlier than the system time in the server. If so, it means that the jwt token has expired, the verification fails, and the verification result is invalid. The server can return information that the token verification is invalid to the client, and at the same time extract the random status bit state in the jwt token, and delete the state identifier of the random status bit state corresponding to the jwt token in the random status bit table pre-stored in the server or set it to a negative value to indicate that this token has expired.
[0087] S1032. If not, determine whether the IP information in the decrypted jwt token is consistent with the IP information carried in the resource request; if not, the verification result is invalid.
[0088] In this embodiment, after determining that the decrypted jwt token has not expired, the IP information carried in the decrypted jwt token and the IP information carried in the resource request can be obtained to compare whether the two are the same. If the IP information carried in the decrypted jwt token is different from the IP information carried in the resource request, it means that the request is not from the same client, the jwt token may have been stolen by criminals, and the verification result is invalid. The server can return information that the token verification is invalid to the client.
[0089] S1033. If yes, determine whether the random status bit exists in a preset status bit table; if no, the verification result is invalid; if yes, the verification result is valid.
[0090] In this embodiment, if it is determined that the IP information in the decrypted jwt token is consistent with the IP information carried in the resource request, it can be determined whether the random status bit state carried in the decrypted jwt token can be queried in the status bit table pre-stored in the server. If not, it means that the random status bit state may have been deleted from the pre-stored status bit table by the server, that is, the jwt token has been revoked, the jwt token verification failed, and the verification result is invalid. The server can return information that the token verification is invalid to the client; if it can be queried, the jwt token verification is successful and the verification result is valid.
[0091] S104: If the verification result is valid, return resource information corresponding to the resource request to the client.
[0092] In this embodiment, if the verification result obtained by verifying the decrypted jwt token is valid, the server can return the resource information corresponding to the resource request to the client according to the identification information of the resource to be obtained carried in the resource request.
[0093] In an embodiment of the present application, a resource request sent by a client is received, the resource request includes a jwt encrypted token, and the jwt encrypted token is pre-encrypted by the server using a public key in an asymmetric key pair; the jwt encrypted token is decrypted using a private key in the asymmetric key pair to obtain a decrypted jwt token; the decrypted jwt token is verified to obtain a verification result; if the verification result is valid, the resource information corresponding to the resource request is returned to the client. It can be seen that the present application can prevent the jwt token from being stolen and tampered with by encrypting the generated jwt token in advance, thereby improving the security and effectiveness of identity authentication.
[0094] See also Figure 2 The present application also provides another identity authentication method, which may further include, before the above step S101:
[0095] S201: Receive a login request from a client.
[0096] In this embodiment, the server receives a login request sent by the client, wherein the login request may include user information such as user name, account password, and IP address information of the client. Specifically, the client may first send the user name, account password and other information to the proxy server, and then the proxy server combines the user name, account password and the acquired IP address information of the client into user information and forwards it to the server.
[0097] S202. Generate a jwt token based on the user information.
[0098] In this embodiment, after obtaining the login request sent by the client, the server can extract the user information such as the client's IP address information from the login request and generate a jwt token. Specifically, the server can put the client's IP address information carried in the login request as IP information into the Playload of the jwt token; it can also use a random algorithm to generate a random state bit state, put it into the Playload of the jwt token, and store the generated random state bit state in a preset random state bit table; it can also preset an expiration time and put it into the Playload of the jwt token, so that the generated jwt token includes the random state bit state, IP information and expiration time.
[0099] S203. Use the public key in the asymmetric key pair to encrypt the jwt token to obtain the jwt encrypted token.
[0100] In this embodiment, after the server generates the jwt token, it can use the public key in the asymmetric key pair stored in itself and the asymmetric algorithm to encrypt the jwt token, thereby obtaining the jwt encrypted token. Optionally, the server can also use other encryption methods to encrypt the generated jwt token.
[0101] S204, returning the jwt encrypted token to the client.
[0102] In this embodiment, after obtaining the jwt encrypted token, the server can put the jwt encrypted token into a response message and return it to the client.
[0103] In the embodiment of the present application, a login request from a client is received, the login request includes user information; a jwt token is generated according to the user information, the jwt token includes a random status bit, IP information and expiration time; the jwt token is encrypted using the public key in the asymmetric key pair to obtain the jwt encrypted token; and the jwt encrypted token is returned to the client. It can be seen that the present application can encrypt the jwt token to avoid the theft and tampering of the jwt token, thereby improving the security and effectiveness of identity authentication.
[0104] See also Figure 3 , this application embodiment provides another identity authentication method, including:
[0105] S301. The client sends a login request to the server.
[0106] In this embodiment, the client sends a login request to the server, wherein the login request may include user information such as user name, account password, and IP address information of the client. Specifically, the client may first send the user name, account password and other information to the proxy server, and then the proxy server combines the user name, account password and the acquired IP address information of the client into user information and forwards it to the server.
[0107] S302: The server receives a login request sent by the client.
[0108] The detailed process of step S302 can refer to the relevant introduction of the above step S201, which will not be repeated here.
[0109] S303. The server generates an initial jwt token based on user information.
[0110] In this embodiment, after obtaining the login request sent by the client, the server can extract the user information such as the client's IP address information from the login request and generate an initial jwt token. Specifically, the server can put the client's IP address information carried in the login request as IP information into the Playload of the jwt token; it can also use a random algorithm to generate a random state bit state, put it into the Playload of the jwt token, and store the generated random state bit state in a preset random state bit table; it can also preset an expiration time and put it into the Playload of the jwt token, so that the generated initial jwt token includes the random state bit state, IP information and expiration time.
[0111] S304. The server concatenates the refresh time string on the initial jwt token to obtain a jwt token.
[0112] In this embodiment, after the server generates the initial jwt token, it can splice a refresh time string on the initial jwt token to obtain the jwt token. It is understandable that the server can generate a refresh time according to demand for refreshing the jwt token, and the refresh time is less than the expiration time of the jwt token. After the server generates the refresh time, it can encode it to obtain a refresh time string, and then splice it as the fourth segment of the initial jwt token on the initial jwt token to obtain the jwt token.
[0113] S305. The server uses the public key to encrypt the jwt token to obtain the jwt encrypted token.
[0114] The detailed process of step S305 can refer to the relevant introduction of the above step S203, which will not be repeated here.
[0115] S306. The server returns the jwt encrypted token to the client.
[0116] The detailed process of step S306 can refer to the relevant introduction of the above step S204, which will not be repeated here.
[0117] S307. The client receives the jwt encrypted token.
[0118] In this embodiment, after receiving the response message carrying the jwt encrypted token, the client can extract the jwt encrypted token and cache it.
[0119] S308: The client sends a resource request to the server.
[0120] In this embodiment, the client sends a resource request to the server to obtain a resource. The resource request includes a jwt encrypted token, identification information of the resource to be obtained, IP information of the client, etc. It can be understood that the jwt encrypted token can be obtained by the server pre-encrypting the jwt token using the public key in an asymmetric key pair. The asymmetric key pair includes a public key and a private key, which correspond to each other and are stored in the server. Specifically, after the server generates the jwt token, it can use the public key in the asymmetric key pair stored in itself to encrypt the jwt token to obtain the jwt encrypted token.
[0121] S309: The server receives the resource request sent by the client.
[0122] The detailed process of step S309 can refer to the relevant introduction of the above step S101, which will not be repeated here.
[0123] S310. The server uses the private key in the asymmetric key pair to decrypt the jwt encrypted token to obtain a decrypted jwt token.
[0124] The detailed process of step S310 can refer to the relevant introduction of the above step S102, which will not be repeated here.
[0125] S311. The server determines whether the format of the decrypted jwt token meets the format requirements.
[0126] In this embodiment, the server can first determine whether the format of the jwt token meets the preset format requirements. If not, it returns the information that the token verification failed to the server. If so, it can proceed to the next step. Specifically, the server can determine whether the decrypted jwt token meets the format of the four-segment jwt token.
[0127] S312. If yes, the server verifies the decrypted jwt token and obtains a verification result.
[0128] The detailed process of step S312 can refer to the relevant introduction of the above step S103, which will not be repeated here.
[0129] S313: If the verification result is valid, the server returns resource information corresponding to the resource request to the client.
[0130] The detailed process of step S313 can refer to the relevant introduction of the above step S104, which will not be repeated here.
[0131] S314. The server determines whether the decrypted jwt token is within the validity period based on the refresh time string and the system time.
[0132] In this embodiment, the server can extract the refresh time string from the decrypted jwt token, decode it to obtain the refresh time of the jwt token, and obtain the system time in the server. Determine whether the refresh time of the jwt token is greater than the system time. If so, it means that the jwt token is not within the validity period. If not, it means that the jwt token is within the validity period.
[0133] S315. If not, the server returns to execute the step of generating an initial jwt token based on the user information.
[0134] In this embodiment, if the jwt token is not within the validity period, the server can return to execute the step of generating an initial jwt token based on the user information, and then regenerate the jwt encrypted token and return it to the client.
[0135] S316. The client determines whether a new jwt encrypted token has been received.
[0136] In this embodiment, after receiving the resource information returned by the server, the client can determine whether the server has returned a new jwt encrypted token.
[0137] S317. If yes, the client deletes the original jwt encrypted token and caches the new jwt encrypted token.
[0138] In this embodiment, after determining that the server has returned a new jwt encrypted token, the client deletes the original jwt encrypted token cached by the client and caches the new jwt encrypted token.
[0139] It is understandable that when the client user performs an operation within the validity period, the server can generate a new jwt encrypted token based on the currently requested jwt encrypted token and return it to the client. The client can directly replace the original jwt encrypted token with the new jwt encrypted token, and then make each request with the new jwt encrypted token. For the user experience, they are completely unaware that the jwt encrypted token has been replaced, that is, the jwt encrypted token will automatically refresh, realizing a function similar to the session expiration mechanism, and avoiding the unauthorized security vulnerability of refresh_token, realizing a senseless refresh mechanism.
[0140] In an embodiment of the present application, a resource request sent by a client is received, the resource request includes a jwt encrypted token, and the jwt encrypted token is pre-encrypted by the server using a public key in an asymmetric key pair; the jwt encrypted token is decrypted using a private key in the asymmetric key pair to obtain a decrypted jwt token; the decrypted jwt token is verified to obtain a verification result; if the verification result is valid, the resource information corresponding to the resource request is returned to the client. It can be seen that the present application can prevent the jwt token from being stolen and tampered with by encrypting the generated jwt token in advance, thereby improving the security and effectiveness of identity authentication.
[0141] In addition, the embodiments of the present application also have the following advantages:
[0142] 1) Low storage resource overhead. Because the generated jwt token is encrypted using asymmetric encryption technology, no matter how much data is added to the jwt token, the length of the encrypted string will be compressed, so the size of the information carried in each jwt token will be reduced after processing, and the storage resources occupied will be reduced.
[0143] 2) Low network resource overhead. Since each client requests a different server, there is no need to solve the consistency problem during session authentication (there is no need to worry about storage issues when using jwt token authentication, because each machine uses the agreed algorithm to parse the jwt token to obtain authentication information), and it eliminates repeated and unnecessary server requests and database query requests.
[0144] Next, an identity authentication device provided by the present application is introduced. The identity authentication device introduced below and the identity authentication method introduced above can refer to each other.
[0145] See also Figure 4 , the embodiment of the present application provides an identity authentication device, including:
[0146] The first receiving unit 401 is used to receive a resource request sent by a client, where the resource request includes a jwt encrypted token, and the jwt encrypted token is pre-encrypted by the server using a public key in an asymmetric key pair;
[0147] The decryption unit 402 is used to decrypt the jwt encrypted token using the private key in the asymmetric key pair to obtain a decrypted jwt token;
[0148] The verification unit 403 is used to verify the token after the jwt is decrypted to obtain a verification result;
[0149] The first returning unit 404 is configured to return resource information corresponding to the resource request to the client if the verification result is valid.
[0150] Optionally, the device further comprises:
[0151] A second receiving unit, configured to receive a login request from a client, wherein the login request includes user information;
[0152] A generating unit, configured to generate a jwt token according to the user information, wherein the jwt token includes a random status bit, IP information, and an expiration time;
[0153] An encryption unit, used to encrypt the jwt token using the public key in the asymmetric key pair to obtain the jwt encrypted token;
[0154] The second returning unit is used to return the jwt encrypted token to the client.
[0155] Optionally, the verification unit 403 is specifically configured to:
[0156] According to the expiration time and system time, determine whether the decrypted jwt token is expired;
[0157] If yes, the verification result is invalid;
[0158] If not, determine whether the IP information in the decrypted jwt token is consistent with the IP information carried in the resource request;
[0159] If not, the verification result is invalid;
[0160] If so, determining whether the random state bit exists in a preset state bit table;
[0161] If not, the verification result is invalid;
[0162] If so, the verification result is valid.
[0163] Optionally, the device further comprises:
[0164] A second receiving unit, configured to receive a login request from a client, wherein the login request includes user information;
[0165] A generating unit, configured to generate an initial jwt token according to the user information, wherein the initial jwt token includes a random status bit, IP information, and an expiration time;
[0166] A splicing unit, used for splicing a refresh time string on the initial jwt token to obtain a jwt token;
[0167] An encryption unit, used to encrypt the jwt token using the public key to obtain the jwt encrypted token;
[0168] The second returning unit is used to return the jwt encrypted token to the client.
[0169] Optionally, the device further comprises:
[0170] A judging unit, used to judge whether the decrypted jwt token is within the validity period according to the refresh time string and the system time;
[0171] The execution unit is used to return to the step of generating an initial jwt token according to the user information if the resolved jwt token is not within the validity period.
[0172] In an embodiment of the present application, a resource request sent by a client is received, the resource request includes a jwt encrypted token, and the jwt encrypted token is pre-encrypted by the server using a public key in an asymmetric key pair; the jwt encrypted token is decrypted using a private key in the asymmetric key pair to obtain a decrypted jwt token; the decrypted jwt token is verified to obtain a verification result; if the verification result is valid, the resource information corresponding to the resource request is returned to the client. It can be seen that the present application can prevent the jwt token from being stolen and tampered with by encrypting the generated jwt token in advance, thereby improving the security and effectiveness of identity authentication.
[0173] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the presence of other identical elements in the process, method, article or device including the elements.
[0174] The above description of the disclosed embodiments enables those skilled in the art to implement or use the present invention. Various modifications to these embodiments will be apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present invention. Therefore, the present invention will not be limited to the embodiments shown herein, but rather to the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. An identity authentication method, characterized in that: include: receiving a login request from a client, wherein the login request includes user information; Generate an initial jwt token according to the user information, wherein the initial jwt token includes a random status bit, IP information, and an expiration time; Add a refresh time string to the initial jwt token to obtain a jwt token; Encrypt the jwt token using the public key in the asymmetric key pair to obtain a jwt encrypted token; Return the jwt encrypted token to the client; Receive a resource request sent by a client, wherein the resource request includes a jwt encrypted token, and the jwt encrypted token is pre-encrypted by the server using a public key in an asymmetric key pair to encrypt the jwt token; Decrypt the jwt encrypted token using the private key in the asymmetric key pair to obtain a decrypted jwt token; Verify the decrypted jwt token to obtain a verification result; If the verification result is valid, returning resource information corresponding to the resource request to the client; According to the refresh time string and the system time, determine whether the decrypted jwt token is within the validity period; If the decrypted jwt token is not within the validity period, return to execute the step of generating an initial jwt token according to the user information; The decrypted jwt token is verified to obtain a verification result, including: According to the expiration time and system time, determine whether the decrypted jwt token is expired; If the decrypted jwt token expires, the verification result is invalid; If the decrypted jwt token has not expired, determine whether the IP information in the decrypted jwt token is consistent with the IP information carried in the resource request; If the IP information in the decrypted jwt token is inconsistent with the IP information carried in the resource request, the verification result is invalid; If the IP information in the decrypted jwt token is consistent with the IP information carried in the resource request, determine whether the random status bit exists in the preset status bit table; If the random status bit does not exist in the preset status bit table, the verification result is invalid; If the random status bit exists in the preset status bit table, the verification result is valid.
2. The method according to claim 1, characterized in that Before receiving the resource request sent by the client, the method further includes: receiving a login request from a client, wherein the login request includes user information; Generate a jwt token based on the user information; Encrypt the jwt token using the public key in the asymmetric key pair to obtain the jwt encrypted token; Return the jwt encrypted token to the client.
3. An identity authentication device, characterized in that: include: A second receiving unit, configured to receive a login request from a client, wherein the login request includes user information; A generating unit, configured to generate an initial jwt token according to the user information, wherein the initial jwt token includes a random status bit, IP information, and an expiration time; A splicing unit, used for splicing a refresh time string on the initial jwt token to obtain a jwt token; An encryption unit, used to encrypt the jwt token using the public key in the asymmetric key pair to obtain a jwt encrypted token; A second returning unit, used to return the jwt encrypted token to the client; A first receiving unit is used to receive a resource request sent by a client, wherein the resource request includes a jwt encrypted token, and the jwt encrypted token is pre-encrypted by the server using a public key in an asymmetric key pair; A decryption unit, used to decrypt the jwt encrypted token using the private key in the asymmetric key pair to obtain a decrypted jwt token; A verification unit, used to verify the decrypted jwt token and obtain a verification result; A first returning unit, configured to return resource information corresponding to the resource request to the client if the verification result is valid; A judging unit, used to judge whether the decrypted jwt token is within the validity period according to the refresh time string and the system time; An execution unit, configured to instruct a generation unit to generate an initial jwt token according to the user information if the decrypted jwt token is not within the validity period; Wherein, the verification unit is specifically used for: According to the expiration time and system time, determine whether the decrypted jwt token is expired; If the decrypted jwt token expires, the verification result is invalid; If the decrypted jwt token has not expired, determine whether the IP information in the decrypted jwt token is consistent with the IP information carried in the resource request; If the IP information in the decrypted jwt token is inconsistent with the IP information carried in the resource request, the verification result is invalid; If the IP information in the decrypted jwt token is consistent with the IP information carried in the resource request, determine whether the random status bit exists in the preset status bit table; If the random status bit does not exist in the preset status bit table, the verification result is invalid; If the random status bit exists in the preset status bit table, the verification result is valid.
4. The device according to claim 3, characterized in that The device also includes: A second receiving unit, configured to receive a login request from a client, wherein the login request includes user information; A generating unit, used to generate a jwt token according to the user information; An encryption unit, used to encrypt the jwt token using the public key in the asymmetric key pair to obtain the jwt encrypted token; The second returning unit is used to return the jwt encrypted token to the client.
Citation Information
Patent Citations
JWT token management method
CN110225050A
Distributed system security authentication method based on JWT
CN110912700A
Automatic endorsement renewal authentication method based on JWT
CN112260838A
Resource access method and device, electronic equipment and computer readable storage medium
CN114244530A
Token generation method and device, token verification method and device, equipment and storage medium
CN114282267A
