JWT-based user login method and system, computer equipment and storage medium
By verifying JWT in multiple verification methods, the problems of poor security and high leakage risk caused by excessive reliance on JWT validity period verification in the existing technology are solved, and the security and efficiency of user login are improved.
Patent Information
- Application Number
- CN202410921664.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-10
- Publication Date
- 2024-10-01
AI Technical Summary
In the existing technology, only the validity period of JWT is verified, which results in poor security and high risk of JWT leakage, affecting user login security.
By obtaining user login information, user ID and device ID, generate a login request and authenticate it, and use multiple verification methods (validity verification, signature verification, device verification) to verify the JWT to ensure the accuracy of user identification. and security.
It improves the security of user login, reduces the risk of JWT leakage, and ensures the security and efficiency of user login.
Smart Images

Figure CN118740469A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer network security technology, and in particular to a JWT-based user login method, system, computer device and storage medium. Background Art
[0002] With the rise of distributed systems and microservice architectures, JSON Web Tokens (JWT) is widely used in API (Application Programming Interface) security protection as a lightweight authentication and authorization mechanism. Since JWT usually contains user verification information, such as identity information, identity permissions, and expiration time, if JWT is leaked, attackers may use user information to log in illegally. Therefore, how to ensure user security login is a problem that needs to be solved.
[0003] Currently, a fixed validity period is usually set for JWT in advance, and when the JWT is verified to be not expired, the user can log in to the interface. However, only validating the JWT and over-relying on the validity period of the JWT to ensure security leads to poor security and a high risk of JWT leakage, which brings security risks and affects user login. Summary of the invention
[0004] In view of this, the present invention provides a user login method, system, computer device and storage medium based on JWT to solve the problem that only the validity period of JWT is verified, resulting in poor security and high risk of JWT leakage, which brings security risks and affects user login.
[0005] In a first aspect, the present invention provides a user login method based on JWT, the method comprising:
[0006] Obtaining login information of multiple users for any user interface, user IDs of multiple users, and device IDs of devices used by multiple users;
[0007] Generate multiple login requests based on login information, user identifiers, and device identifiers corresponding to multiple users;
[0008] For a first user, authenticate the login request of the first user. After the authentication is passed, issue a first JWT corresponding to the first user, where the first user is any one of the multiple users;
[0009] At least one verification method is used to verify the first JWT corresponding to the first user. After the verification passes, the first user is allowed to log in to the user interface using the device corresponding to the device identifier of the first JWT.
[0010] The JWT-based user login method provided in the embodiment of the present invention improves the accuracy of identifying user identity by generating a login request based on login information, user identification, and device identification and performing authentication. In addition, by verifying JWT using at least one verification method, the security of user login is improved and the risk of JWT leakage is reduced compared to only verifying the validity period of JWT.
[0011] In an optional implementation, the verification method is validity period verification, and the first JWT includes an issuance time and a validity period threshold; verifying the first JWT corresponding to the first user includes:
[0012] Get the current time;
[0013] Determine the time difference between the issuance time of the first JWT and the current time;
[0014] When the time difference is greater than the validity threshold of the first JWT, the validity check of the first JWT passes.
[0015] The JWT-based user login method provided in an embodiment of the present invention verifies whether the first JWT is within the validity period through the time difference between the issuance time of the first JWT and the current time, based on the time difference and the validity period threshold of the first JWT. The verification passes only when the time difference is greater than the validity period threshold of the first JWT, that is, the first JWT is within the validity period, thereby reducing the risk of JWT leakage and improving the security of user login.
[0016] In an optional embodiment, the method further includes:
[0017] If the time difference is not greater than the validity period threshold of the first JWT, a JWT acquisition request is sent, and the JWT acquisition request is used to obtain a second JWT. The issuance time of the second JWT is updated based on the issuance time of the first JWT. Other information except the issuance time and validity period threshold of the second JWT is obtained by reusing the first JWT.
[0018] Re-verify the validity period based on the second JWT.
[0019] The JWT-based user login method provided in an embodiment of the present invention improves user login efficiency while ensuring the security of the information carried by the JWT by sending a JWT acquisition request to automatically renew the first JWT when the first JWT has expired, without the need to rely on a database for renewal.
[0020] In an optional implementation, the verification method is signature verification, and the first JWT consists of a header, a payload, and a signature; verifying the first JWT corresponding to the first user includes:
[0021] The header and payload of the first JWT are re-signed. If the re-generated signature is consistent with the signature of the first JWT, the signature verification of the first JWT passes.
[0022] Alternatively, when the regenerated signature is inconsistent with the signature of the first JWT, the signature verification of the first JWT fails.
[0023] The JWT-based user login method provided in an embodiment of the present invention can determine whether the first JWT has been tampered with by regenerating a signature based on the header and payload of the first JWT and comparing it with the signature carried by the first JWT, thereby realizing signature verification of the first JWT, reducing the risk of JWT leakage and improving the security of user login.
[0024] In an optional implementation, the verification method is device verification, and the first JWT includes a device identifier; verifying the first JWT corresponding to the first user includes:
[0025] When the device identifier of the first JWT is consistent with the device used by the first user, the device verification of the first JWT passes;
[0026] Alternatively, when the device identifier of the first JWT is inconsistent with the device used by the first user, the device verification of the first JWT fails.
[0027] The JWT-based user login method provided in an embodiment of the present invention can ensure that the JWT issued by the first user on any device can only be used on that device by comparing whether the device identifier in the first JWT is consistent with the device actually used by the first user, thereby reducing the risk of JWT leakage and improving the security of user login.
[0028] In an optional embodiment, the method further includes:
[0029] Obtain a third JWT from JWTs corresponding to multiple second users, where the multiple second users represent users other than the first user among the multiple users, and login information of the third JWT is consistent with login information of the first JWT;
[0030] If the issuance time of the third JWT is later than the issuance time of the first JWT, the first JWT is invalidated, and the current time is used as the invalidation time of the first JWT;
[0031] Using the device identifier corresponding to the first JWT as the primary key of the key-value pair, using the invalid time as the value of the key-value pair, and storing the key-value pair in the cache;
[0032] The device corresponding to the device identifier in the first JWT of the first user is logged out of the user interface, and the device corresponding to the device identifier in the third JWT of the second user is logged in to the user interface.
[0033] The JWT-based user login method provided in an embodiment of the present invention obtains a third JWT that is consistent with the login information of the first JWT from the JWTs corresponding to multiple second users. That is, at this time, two users want to log in to the same account, so the issuance time of the first JWT and the issuance time of the third JWT are compared. If the issuance time of the third JWT is later than that of the first JWT, it means that the third JWT is logged in later. At this time, the device used by the first user exits the interface, and the device used by the second user logs in to the interface, so as to ensure that only one user can use one account, thereby improving the security of user login.
[0034] In an optional embodiment, the method further includes:
[0035] Obtain a login request initiated again by the first user to the user interface, authenticate the login request initiated again, and after the authentication is passed, issue a fourth JWT. The issuance time of the fourth JWT is updated based on the issuance time of the first JWT. Other information except the issuance time of the fourth JWT is obtained by reusing the first JWT.
[0036] In the cache, if a key-value pair with the device ID of the fourth JWT as the primary key is found, the value of the key-value pair is obtained;
[0037] When the issuance time of the fourth JWT is later than the invalid time corresponding to the value, the device corresponding to the device identifier of the fourth JWT of the first user is logged in to the user interface, and the device corresponding to the device identifier in the third JWT of the second user is exited from the user interface.
[0038] The JWT-based user login method provided by the embodiment of the present invention, when the first user initiates a login request again, a fourth JWT is issued to the first user. At this time, the key-value pair stored when the first user was invalidated during the last login is searched from the cache, and by judging the issuance time of the fourth JWT and the invalidation time corresponding to the value of the key-value pair, it can be judged whether the fourth JWT is issued after being invalidated. If it is issued after being invalidated, the first user is required to log in again and the second user is required to log out, so as to ensure that only one user can use an account, thereby improving the security of user login.
[0039] In a second aspect, the present invention provides a JWT-based user login system, the system comprising:
[0040] A front end, used to obtain login information of multiple users for any user interface, user identifiers of multiple users, and device identifiers of devices used by multiple users, and generate multiple login requests based on the login information, user identifiers, and device identifiers corresponding to the multiple users;
[0041] The authentication center is used to authenticate the login request of the first user, and after the authentication is passed, issue a first JWT corresponding to the first user, where the first user is any one of the multiple users;
[0042] The application server is used to verify the first JWT corresponding to the first user using at least one verification method, and after the verification passes, allow the first user to log in to the user interface using the device corresponding to the device identifier of the first JWT.
[0043] In a third aspect, the present invention provides a computer device, comprising: a memory and a processor, the memory and the processor are communicatively connected to each other, computer instructions are stored in the memory, and the processor executes the JWT-based user login method of the above-mentioned first aspect or any corresponding embodiment thereof by executing the computer instructions.
[0044] In a fourth aspect, the present invention provides a computer-readable storage medium having computer instructions stored thereon, the computer instructions being used to enable a computer to execute the JWT-based user login method of the above-mentioned first aspect or any corresponding embodiment thereof.
[0045] In a fifth aspect, the present invention provides a computer program product, comprising computer instructions, which are used to enable a computer to execute the JWT-based user login method of the above-mentioned first aspect or any corresponding embodiment thereof. BRIEF DESCRIPTION OF THE DRAWINGS
[0046] In order to more clearly illustrate the specific implementation methods of the present invention or the technical solutions in the prior art, the drawings required for use in the specific implementation methods or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some implementation methods of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0047] Figure 1 is a schematic diagram of a JWT-based user login system according to an embodiment of the present invention;
[0048] Figure 2 is a flowchart of a JWT-based user login method according to an embodiment of the present invention;
[0049] Figure 3 is a schematic diagram of a JWT-based user login method using a JWT-based user login system according to an embodiment of the present invention;
[0050] Figure 4 is a schematic diagram of a microservice system with a gateway according to an embodiment of the present invention;
[0051] Figure 5 It is a schematic diagram of the hardware structure of a computer device according to an embodiment of the present invention. DETAILED DESCRIPTION
[0052] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are 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 those skilled in the art without creative work are within the scope of protection of the present invention.
[0053] As a lightweight identity authentication and authorization mechanism, JWT is widely used in API security protection. Since JWT carries a variety of user information, once JWT is leaked, it will bring security risks. Therefore, in the related technology, a fixed validity period is usually pre-set for JWT, and when the JWT is verified to be not expired, the user login interface is enabled. However, only the validity period of JWT is verified, and excessive reliance on the validity period of JWT to ensure security results in poor security and a high risk of JWT leakage, which brings security risks and affects user login. In view of this, an embodiment of the present invention proposes a user login method based on JWT. By adopting at least one verification method to verify JWT, the security of user login is improved and the risk of JWT leakage is reduced compared to only verifying the validity period of JWT.
[0054] In this embodiment, a user login system based on JWT is provided. Figure 1 is a schematic diagram of a JWT-based user login system according to an embodiment of the present invention. Figure 1 As shown, the system includes: a front end, used to obtain login information of multiple users for any user interface, user identifications of multiple users and device identifications of devices used by multiple users, and generate multiple login requests based on the login information, user identifications and device identifications corresponding to the multiple users; an authentication center, used to authenticate the login request of the first user for a first user, and after the authentication is passed, issue a first JWT corresponding to the first user, the first user being any user among the multiple users; an application server, used to verify the first JWT corresponding to the first user using at least one verification method, and after the verification is passed, allow the first user to log in to the user interface using the device corresponding to the device identification of the first JWT.
[0055] Specifically, the front end interacts directly with multiple users, collects various information required for user login and generates a login request. The front end can directly send the login request to the authentication center through the access interface, or forward it to the authentication center through the gateway, and the embodiment of the present invention does not limit this. The authentication center and the application server are both located on the back-end server. The authentication center matches the information contained in the login request with the information stored in the database. If the match is successful, the authentication is successful. If the match is unsuccessful, the authentication fails. After the authentication center passes the authentication, it will issue a JWT. For the first user, the first JWT corresponding to the first user needs to be verified at least once by the application server to reduce the risk of JWT leakage.
[0056] According to an embodiment of the present invention, a JWT-based user login method embodiment is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although the logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.
[0057] In this embodiment, a JWT-based user login method is provided, which can be used in the above-mentioned JWT-based user login system. Figure 2 : is a flowchart of a JWT-based user login method according to an embodiment of the present invention. Figure 2 As shown, the process includes the following steps:
[0058] Step S201, obtain the login information of multiple users for any user interface, the user identifications of multiple users, and the device identifications of the devices used by multiple users. Specifically, the embodiment of the present invention takes logging into any application as an example for explanation, and the user interface is the login interface of the application. The login information includes information such as a user name and a password. The user identification can be an identification assigned to the user by the application, which can uniquely identify the user. Optionally, a user identification can correspond to one or more user names. The embodiment of the present invention takes one user identification corresponding to one user name as an example for explanation. The device identification is used to uniquely identify the device used by the user. The multiple users can log in to the same user interface, that is, log in to the same application, or log in to different user interfaces, that is, log in to different applications. The embodiment of the present invention takes multiple users logging into the same user interface as an example for explanation.
[0059] Step S202, based on the login information, user identification and device identification corresponding to multiple users, multiple login requests are generated. Specifically, the front end generates login requests based on various information, sends the login requests to the authentication center, or forwards them to the authentication center through a gateway, so that the authentication center performs authentication.
[0060] Step S203, for the first user, the login request of the first user is authenticated, and after the authentication is passed, the first JWT corresponding to the first user is issued, and the first user is any one of the multiple users. Specifically, the authentication center authenticates the first user based on the various information contained in the login request of the first user. For example, a pair of usernames and passwords are stored in the database of the authentication center, the username is 123, and the password is ascdefg. In the login request of the first user, the password corresponding to the username 123 is abcdefg, which is inconsistent with the storage in the database, and the login request authentication of the first user fails. It should be noted that it is also possible to authenticate whether the first user has login authority, etc., and the embodiment of the present invention does not limit the specific content of the authentication. If the login request of the first user is authenticated, the authentication center will issue the first JWT corresponding to the first user. Optionally, the front end will store the JWT corresponding to each user. Any JWT consists of three parts: header, payload, and signature. The payload of the first JWT contains all the information in the login request and the issuance time of the first JWT. Optionally, after the payload information of the first JWT is set, a symmetric encryption algorithm can be used to encrypt and protect the information in the payload to reduce the risk of JWT leakage. Since JWT encryption is a conventional technical means, decryption when obtaining the information contained in the JWT is also a conventional technical means. In the embodiment of the present invention, encryption is performed by default when issuing the JWT, and decryption is performed when obtaining the information in the JWT payload, which will not be repeated. Optionally, if the authentication of the first user's login request fails, the front end can display a prompt message to prompt the first user to make corresponding modifications.
[0061] Step S204, at least one verification method is used to verify the first JWT corresponding to the first user, and after the verification is passed, the first user is allowed to log in to the user interface using the device corresponding to the device identifier of the first JWT. Specifically, since the relevant technology only verifies the validity period of the JWT, which is too dependent on the validity period of the JWT, resulting in poor security and a high risk of JWT leakage, the embodiment of the present invention verifies the JWT at least once, and only allows the user to log in if at least one verification is passed, thereby reducing the risk of JWT leakage and improving the security of user login.
[0062] The JWT-based user login method provided in the embodiment of the present invention improves the accuracy of identifying user identity by generating a login request based on login information, user identification, and device identification and performing authentication. In addition, by verifying JWT using at least one verification method, the security of user login is improved and the risk of JWT leakage is reduced compared to only verifying the validity period of JWT.
[0063] In this embodiment, a JWT-based user login method is provided, which can be used in the above-mentioned JWT-based user login system. The method specifically includes the following steps:
[0064] Step S301, obtaining login information of multiple users for any user interface, user IDs of multiple users, and device IDs of devices used by multiple users. Figure 2 Step S201 of the illustrated embodiment will not be described in detail here.
[0065] Step S302: Generate multiple login requests based on the login information, user identifiers and device identifiers corresponding to multiple users. Figure 2 Step S202 of the illustrated embodiment will not be described in detail here.
[0066] Step S303: For the first user, authenticate the login request of the first user. After the authentication is passed, issue a first JWT corresponding to the first user. The first user is any user among the multiple users. For details, please refer to Figure 2 Step S203 of the illustrated embodiment will not be described in detail here.
[0067] Step S304: Use at least one verification method to verify the first JWT corresponding to the first user. After the verification passes, the first user is allowed to log in to the user interface using the device corresponding to the device identifier of the first JWT.
[0068] Specifically, the verification method is validity period verification, and the above step S304 verifies the first JWT corresponding to the first user including:
[0069] Step a1, obtaining the current time. Specifically, the application server obtains the current system time to prepare for the validity period verification.
[0070] Step a2, the first JWT includes the issuance time and the validity period threshold; determine the time difference between the issuance time of the first JWT and the current time. Specifically, the application server uses a symmetric encryption algorithm to decrypt the payload information of the first JWT to obtain the issuance time. Optionally, if the first JWT is encrypted by other methods, the corresponding decryption method can be used for decryption.
[0071] Step a3, when the time difference is greater than the validity threshold of the first JWT, the validity check of the first JWT passes. Specifically, when issuing a JWT, the authentication center usually sets a default validity threshold for the JWT, which is stored in the payload of the JWT. The validity threshold can be 3 days, 5 days, or 7 days, etc., and this is not limited in the embodiment of the present invention. If the time difference is greater than the validity threshold of the first JWT, it means that the first JWT is still valid and the validity check passes.
[0072] Step a4, when the time difference is not greater than the validity threshold of the first JWT, a JWT acquisition request is sent. The JWT acquisition request is used to obtain the second JWT. The issuance time of the second JWT is updated based on the issuance time of the first JWT. Other information except the issuance time and validity threshold of the second JWT is obtained by reusing the first JWT. Specifically, if the time difference is not greater than the validity threshold of the first JWT, it means that the first JWT has expired, and the application server can send a JWT acquisition request to the authentication center to request renewal. The authentication center can reissue the second JWT and send it to the application server, and add a custom field in the header of the HTTP response to carry the second JWT to the front end. After the front end obtains the response header, it checks whether it carries a new JWT. If it carries a new JWT, it determines which user the new JWT belongs to, and then replaces the JWT in the user's local storage, so that the user can carry a new JWT when requesting to log in again. Among them, for the information in the second JWT payload, the validity threshold of the first JWT can be changed according to actual needs to obtain the validity threshold of the second JWT, and the issuance time of the first JWT can be updated to the issuance time of the second JWT. Other information can reuse the first JWT. By verifying the validity period of JWT, the expired JWT can be renewed in time, and the renewal process is completed through the self-verification feature of JWT, without relying on the database, which improves security.
[0073] Step a5: re-verify the validity period based on the second JWT. Specifically, the validity period of the renewed second JWT is verified with reference to the above steps a1 to a3.
[0074] Specifically, the verification method is device verification, and the above step S304 verifies the first JWT corresponding to the first user including:
[0075] Step b1, the first JWT consists of a header, a payload, and a signature; the header and payload of the first JWT are regenerated with a signature, and when the regenerated signature is consistent with the signature of the first JWT, the signature verification of the first JWT passes. Specifically, since the signature of the JWT is obtained based on the hash value of the header and the payload, any change to the header and the payload will cause the signature to change, so that the signature verification can be used to determine whether the JWT has been tampered with or damaged during transmission. When the signature regenerated based on the header and payload of the first JWT is consistent with the signature stored in the first JWT itself, it means that the first JWT has not been tampered with and the signature verification passes.
[0076] Step b2, when the regenerated signature is inconsistent with the signature of the first JWT, the signature verification of the first JWT fails. Specifically, when the signature regenerated based on the header and payload of the first JWT is inconsistent with the signature stored in the first JWT itself, it means that the header and / or payload of the first JWT has been tampered with and cannot pass the signature verification. By regenerating the signature based on the header and payload of the first JWT and comparing it with the signature carried by the first JWT, it is possible to determine whether the first JWT has been tampered with, and implement signature verification of the first JWT. The signature verification process is a self-verification process of the JWT, which reduces the risk of JWT leakage and improves the security and efficiency of user login.
[0077] Specifically, the verification method is device verification, and the above step S304 verifies the first JWT corresponding to the first user including:
[0078] Step c1, the first JWT includes a device identifier; when the device identifier of the first JWT is consistent with the device used by the first user, the device verification of the first JWT is passed. Specifically, in order to further improve the security of user login, it is necessary to ensure that the JWT issued by any user on the device used by the user can only be used on the device. Therefore, the application server can obtain the serial number of the device used by the first user or other information that can clearly indicate the device, and compare the obtained information with the device identifier in the payload of the first JWT. If they are consistent, it means that the device that issued the first JWT is the same as the device actually used, and the device verification is passed.
[0079] In step c2, if the device identifier of the first JWT is inconsistent with the device used by the first user, the device verification of the first JWT fails. Specifically, if the device information obtained by the application server is inconsistent with the device identifier, it means that the device that issued the first JWT is not the same as the device actually used, and the device verification cannot pass. By comparing whether the device identifier in the first JWT is consistent with the device actually used by the first user, it can be ensured that the JWT issued by the first user on any device can only be used on that device, reducing the risk of JWT leakage and improving the security of user login.
[0080] It should be noted that steps a1 to a5 are validity period verification, steps b1 and b2 are signature verification, and steps c1 and c2 are device verification. Only when the above three verification methods are passed, can the first JWT be considered to have passed the verification, so that the device used by the first user can log in to the user interface. Since the second JWT will be renewed after the first JWT expires, the second JWT is re-verified for validity period, and subsequent signature verification and device verification are performed. The above three verification methods are only examples, and the application server can also adopt other verification methods according to actual needs, and the embodiments of the present invention do not limit this.
[0081] Step S305, a third JWT is obtained from the JWTs corresponding to the multiple second users, where the multiple second users represent users other than the first user among the multiple users, and the login information of the third JWT is consistent with the login information of the first JWT. Specifically, the login information of the third JWT is consistent with the login information of the first JWT, that is, there are two users, that is, two devices to log in to the same account. In order to reduce the risk of JWT leakage and ensure the security of user login, the device needs to be kicked out.
[0082] Step S306: If the issuance time of the third JWT is later than the issuance time of the first JWT, the first JWT is invalidated, and the current time is used as the invalidation time of the first JWT. Specifically, the embodiment of the present invention is explained by taking the later logged-in device kicking out the first logged-in device as an example, that is, if the issuance time of the third JWT is later than the issuance time of the first JWT, the first JWT issued first is invalidated, and the time of the kick-out, that is, the current time, is used as the invalidation time of the first JWT.
[0083] Step S307, using the device identifier corresponding to the first JWT as the primary key of the key-value pair, using the invalid time as the value of the key-value pair, and storing the key-value pair in the cache. Specifically, the cache can be Redis (RemoteDictionary Server) on the server side, which is only used as an example and is not limited to this. Redis stores data in the form of key-value pairs, so the device identifier of the first JWT is used as the primary key of the key-value pair, and the invalid time is used as the value of the key-value pair.
[0084] Step S308: The device corresponding to the device identifier in the first JWT of the first user is logged out of the user interface, and the device corresponding to the device identifier in the third JWT of the second user is logged in to the user interface. Specifically, the device used by the first user who logged in first is logged out of the user interface, and the device used by the second user who logged in later is logged in to the user interface, so as to ensure that only one user can log in to one account using one device at the same time.
[0085] Step S309, obtain the login request initiated by the first user to the user interface again, authenticate the login request initiated again, and issue a fourth JWT after the authentication is passed. The issuance time of the fourth JWT is updated based on the issuance time of the first JWT, and other information except the issuance time of the fourth JWT is obtained by reusing the first JWT. Specifically, if the device used by the first user is kicked out, he can choose to log in again. When logging in again, there is no need to obtain various information of the first user again to improve the user login efficiency.
[0086] Step S310, if a key-value pair with the device identifier of the fourth JWT as the primary key is found in the cache, the value of the key-value pair is obtained. Specifically, since the fourth JWT is different from the first JWT only in the issuance time, and the other information is the same, the device identifier of the fourth JWT is used to search in the cache, and a key-value pair with the primary key of the device identifier of the first JWT can be found, thereby obtaining the invalid time when the first user was kicked out when logging in last time.
[0087] Step S311, when the issuance time of the fourth JWT is later than the invalid time corresponding to the value, the device corresponding to the device identifier of the fourth JWT of the first user is logged into the user interface, and the device corresponding to the device identifier in the third JWT of the second user is logged out of the user interface. Specifically, if the issuance time of the fourth JWT is later than the invalid time of the first JWT, it means that the first user is logging in again, that is, the first user is logged in later than the second user. At this time, the device used by the second user is kicked out, and the device used by the first user is logged in to the user interface, which again ensures the security of user login.
[0088] In some optional embodiments, Figure 3 is a schematic diagram of a JWT-based user login method using a JWT-based user login system according to an embodiment of the present invention, such as Figure 3As shown, taking the first user as an example, after the front end obtains the login information, user identification and device identification of the first user for any user interface, a login request is generated and stored. Then, the front end sends the login request to the authentication center, and the authentication center authenticates the login request. After the authentication is passed, the first JWT is issued, and the various information in the login request is set in the payload of the first JWT, and encrypted after the setting is completed. Then, the authentication center returns the encrypted first JWT to the front end, and the front end stores it. Then, the front end sends the first JWT to the application server through the access interface. Then, the application server responds to the front end, and at the same time verifies the decrypted first JWT using at least one verification method. Among them, the verification method includes validity period verification, signature verification and device verification. In the validity period verification, if the first JWT has expired, the application server sends a JWT acquisition request to the authentication center for renewal, and the authentication center renews the first JWT and obtains the second JWT and returns it to the application server. If the verification of the first JWT is passed, the first user is allowed to log in to the user interface using the device.
[0089] In some optional implementations, the JWT-based user login method provided in the embodiments of the present invention is also applicable to a microservice system with a gateway. Microservice is a method for designing complex applications, which develops a single application into a set of small, independent services, each of which runs in its own process and communicates through a lightweight mechanism. Each service is built around a business function and can be independently deployed, expanded, and maintained. Figure 4 is a schematic diagram of a microservice system with a gateway according to an embodiment of the present invention. Figure 4 As shown, the front end initiates a login request, which is submitted to the gateway, which is forwarded by the gateway to the authentication center for authentication. After the authentication is passed, a JWT is issued and sent to the gateway. In this process, the front end no longer needs to interact with the application server. Then, after the gateway receives the request, the verification, decryption, and sending of JWT to obtain the request for renewal that were originally required by the application server are all extracted to the gateway and completed uniformly in the gateway. If the verification passes, the front end login request is forwarded to the specific microservice corresponding to the login request. In each microservice, the JWT is no longer verified, and the verification result of the gateway is used directly. The business calculation is completed according to the business logic inside the microservice. After the calculation in the microservice is completed, it is returned to the gateway and the front end in turn to complete the login request.
[0090] The JWT-based user login method provided in the embodiment of the present invention improves the accuracy of identifying user identity by generating a login request based on login information, user identification, and device identification and performing authentication. In addition, by verifying JWT using at least one verification method, the security of user login is improved and the risk of JWT leakage is reduced compared to only verifying the validity period of JWT.
[0091] As one or more specific application embodiments of the embodiments of the present invention, the optimal implementation scheme or the scheme that the inventor most wants to embody is described in combination with specific application scenarios.
[0092] The further functional description of each of the above modules and units is the same as that of the above corresponding embodiments and will not be repeated here.
[0093] The embodiment of the present invention also provides a computer device, see Figure 5 , Figure 5 is a schematic diagram of the structure of a computer device provided by an optional embodiment of the present invention, such as Figure 5 As shown, the computer device includes: one or more processors 10, a memory 20, and interfaces for connecting various components, including high-speed interfaces and low-speed interfaces. Various components are connected to each other using different buses for communication, and can be installed on a common mainboard or installed in other ways as needed. The processor can process the instructions executed in the computer device, including instructions stored in or on the memory to display the graphical information of the GUI on an external input / output device (such as, a display device coupled to the interface). In some optional embodiments, if necessary, multiple processors and / or multiple buses can be used together with multiple memories and multiple memories. Similarly, multiple computer devices can be connected, and each device provides some necessary operations (for example, as a server array, a group of blade servers, or a multi-processor system). Figure 5 A processor 10 is taken as an example.
[0094] The processor 10 may be a central processing unit, a network processor or a combination thereof. The processor 10 may further include a hardware chip. The hardware chip may be a dedicated integrated circuit, a programmable logic device or a combination thereof. The programmable logic device may be a complex programmable logic device, a field programmable gate array, a general purpose array logic or any combination thereof.
[0095] The memory 20 stores instructions executable by at least one processor 10, so that the at least one processor 10 executes the method shown in the above embodiment.
[0096] The memory 20 may include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function; the data storage area may store data created according to the use of the computer device, etc. In addition, the memory 20 may include a high-speed random access memory, and may also include a non-transient memory, such as at least one disk storage device, a flash memory device, or other non-transient solid-state storage device. In some optional embodiments, the memory 20 may optionally include a memory remotely arranged relative to the processor 10, and these remote memories may be connected to the computer device via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0097] The memory 20 may include a volatile memory, such as a random access memory; the memory may also include a non-volatile memory, such as a flash memory, a hard disk or a solid state drive; the memory 20 may also include a combination of the above types of memory.
[0098] The computer device further comprises a communication interface 30 for the computer device to communicate with other devices or a communication network.
[0099] The embodiment of the present invention also provides a computer-readable storage medium. The method according to the embodiment of the present invention can be implemented in hardware, firmware, or can be implemented as a computer code that can be recorded in a storage medium, or can be implemented as a computer code that is originally stored in a remote storage medium or a non-temporary machine-readable storage medium and will be stored in a local storage medium through a network download, so that the method described herein can be stored in such software processing on a storage medium using a general-purpose computer, a dedicated processor, or programmable or dedicated hardware. Among them, the storage medium can be a magnetic disk, an optical disk, a read-only storage memory, a random access memory, a flash memory, a hard disk or a solid-state hard disk, etc.; further, the storage medium can also include a combination of the above types of memories. It can be understood that a computer, a processor, a microprocessor controller, or programmable hardware includes a storage component that can store or receive software or computer code. When the software or computer code is accessed and executed by a computer, a processor, or hardware, the method shown in the above embodiment is implemented.
[0100] A part of the present invention may be applied as a computer program product, such as a computer program instruction, which, when executed by a computer, can call or provide the method and / or technical solution according to the present invention through the operation of the computer. Those skilled in the art should understand that the existence of the computer program instruction in a computer-readable medium includes, but is not limited to, a source file, an executable file, an installation package file, etc., and accordingly, the way in which the computer program instruction is executed by the computer includes, but is not limited to: the computer directly executes the instruction, or the computer compiles the instruction and then executes the corresponding compiled program, or the computer reads and executes the instruction, or the computer reads and installs the instruction and then executes the corresponding installed program. Here, the computer-readable medium may be any available computer-readable storage medium or communication medium accessible to the computer.
[0101] Although the embodiments of the present invention have been described in conjunction with the accompanying drawings, those skilled in the art may make various modifications and variations without departing from the spirit and scope of the present invention, and such modifications and variations are all within the scope defined by the appended claims.
Claims
1. A user login method based on JWT, characterized in that: The method comprises: Obtaining login information of multiple users for any user interface, user identifiers of the multiple users, and device identifiers of devices used by the multiple users; Generate multiple login requests based on the login information, user identifiers and device identifiers corresponding to the multiple users; For a first user, authenticate the login request of the first user, and after the authentication is passed, issue a first JWT corresponding to the first user, where the first user is any one of the multiple users; At least one verification method is used to verify the first JWT corresponding to the first user. After the verification passes, the first user is allowed to log in to the user interface using the device corresponding to the device identifier of the first JWT.
2. The method according to claim 1, characterized in that: The verification method is validity period verification, and the first JWT includes an issuance time and a validity period threshold; the verification of the first JWT corresponding to the first user includes: Get the current time; Determine the time difference between the issuance time of the first JWT and the current time; When the time difference is greater than the validity threshold of the first JWT, the validity verification of the first JWT passes.
3. The method according to claim 2, characterized in that The method further comprises: If the time difference is not greater than the validity period threshold of the first JWT, a JWT acquisition request is sent, where the JWT acquisition request is used to acquire a second JWT, where the issuance time of the second JWT is updated based on the issuance time of the first JWT, and other information except the issuance time and validity period threshold of the second JWT is obtained by multiplexing the first JWT; Re-verify the validity period based on the second JWT.
4. The method according to claim 1, characterized in that The verification method is signature verification, and the first JWT consists of a header, a payload, and a signature; and the verification of the first JWT corresponding to the first user includes: Regenerate a signature for the header and payload of the first JWT. If the regenerated signature is consistent with the signature of the first JWT, the signature verification of the first JWT passes. Alternatively, when the regenerated signature is inconsistent with the signature of the first JWT, the signature verification of the first JWT fails.
5. The method according to claim 1, characterized in that The verification method is device verification, and the first JWT includes a device identifier; and the verification of the first JWT corresponding to the first user includes: When the device identifier of the first JWT is consistent with the device used by the first user, the device verification of the first JWT passes; Alternatively, when the device identifier of the first JWT is inconsistent with the device used by the first user, the device verification of the first JWT fails.
6. The method according to claim 1, characterized in that The method further comprises: Acquire a third JWT from JWTs corresponding to a plurality of second users, where the plurality of second users represent users other than the first user among the plurality of users, and login information of the third JWT is consistent with login information of the first JWT; If the issuance time of the third JWT is later than the issuance time of the first JWT, invalidate the first JWT and use the current time as the invalidation time of the first JWT; Using the device identifier corresponding to the first JWT as the primary key of a key-value pair, using the invalid time as the value of the key-value pair, and storing the key-value pair in a cache; Make the device corresponding to the device identifier in the first JWT of the first user exit the user interface, and make the device corresponding to the device identifier in the third JWT of the second user log in to the user interface.
7. The method according to claim 6, characterized in that The method further comprises: Obtain a login request initiated again by the first user to the user interface, authenticate the login request initiated again, and after the authentication is passed, issue a fourth JWT, where the issuance time of the fourth JWT is updated based on the issuance time of the first JWT, and other information except the issuance time of the fourth JWT is obtained by reusing the first JWT; In the cache, if a key-value pair with the device identifier of the fourth JWT as the primary key is found, obtain the value of the key-value pair; When the issuance time of the fourth JWT is later than the invalid time corresponding to the value, the device corresponding to the device identifier of the fourth JWT of the first user is logged in to the user interface, and the device corresponding to the device identifier in the third JWT of the second user is logged out of the user interface.
8. A JWT-based user login system, characterized in that: The system comprises: A front end, used to obtain login information of multiple users for any user interface, user identifiers of the multiple users, and device identifiers of devices used by the multiple users, and generate multiple login requests based on the login information, user identifiers, and device identifiers corresponding to the multiple users; The authentication center is configured to authenticate a login request of a first user and issue a first JWT corresponding to the first user after the authentication is passed. The first user is any one of the multiple users. The application server is used to verify the first JWT corresponding to the first user using at least one verification method, and after the verification passes, allow the first user to log in to the user interface using the device corresponding to the device identifier of the first JWT.
9. A computer device, characterized in that: include: A memory and a processor, wherein the memory and the processor are communicatively connected to each other, the memory stores computer instructions, and the processor executes the JWT-based user login method according to any one of claims 1 to 7 by executing the computer instructions.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a computer to execute the JWT-based user login method according to any one of claims 1 to 7.
