A: Usually stored in the client. jwt, or JSON Web Token, is an authentication protocol that is generally used to verify the identity information and identity permissions of a request. When I was browsing a certain website this morning, I met a classmate who was asking this question. He was very curious about the storage location of jwt. I happened to be studying this topic some time ago, so I'm not shy about answering this question without being invited. Later, I found out through some research that the server does not need to save this token. The client just needs to save it, no matter what method is used, you can even ask the user to write a note and put it in their pocket! So how does this token work? Let’s first talk about operations that require server-side storage, which is the traditional session approach. First, to log in the user, you need to maintain a login table on the server. This login table can be placed in the cache or in the database. For the front-end guys, this process is usually imperceptible. The back-end guys use an http header field called set-cookie to write the data into the browser cookie themselves. Then when making a request, the browser will write the cookie into the request header itself. When the client requests to enter the server, the server obtains the session in the cookie, then searches the login table for user information, verifies user permissions, and then completes normal business interaction. Well, now I don’t want to maintain a login table for various reasons. What should I do? But in this way, the user's information will be exposed. The middlemen like this kind of straightforward request the most. They will just take a stool and sit on your server port for a few days, and all the relatives in your database will be clearly exposed to others. This definitely won’t work, so what should we do? Just add a password and confuse it. This way, when the guys get your token, they will be confused for a while and will most likely walk away carelessly, leaving only a small number of guys who are well prepared (KPI) to struggle to crack it. Once you decrypt it on the server, you get the user information. Similarly, you also write the expiration time into the ciphertext. If it expires, it will jump to the login page with a 401 error. In this way, a solution that does not require back-end storage of login credentials is developed. This is the most basic working principle of jwt: the identity information is handed over to the client for safekeeping. The token generated by jwt consists of three parts: header, payload, and signature, which are separated by a decimal point ".". Header, that is, the header information, describes the basic information of the token and is in JSON format: { "alg":"HS256", "typ":"JWT" } alg represents the encryption algorithm used to generate the signature part, and typ indicates that the token is of jwt type. Payload is your user data, also in json format. However, JWT does not recommend putting sensitive data in it, because in the specification, the payload is the same as the header, and is only base64 encoded once and displayed on the token. Signature is the signature of this token. Usually it is a string generated by encrypting the previous header and payload together with a private key string you define yourself. As mentioned before, jwt only base64 encodes the contents of the payload, so it is very easy for attackers to change your content. However, they don’t know your private key, so they cannot generate the correct signature after the change. They use encryption to verify the header.payload of the request again, and find that it does not match the signature. At this time, you can clearly know that someone is causing trouble, and directly return a 500 to pretend that the server is down. Of course, now that you understand how this works, it's not impossible to come up with your own disgusting specifications, for example, encrypt the payload again and then gzip it, etc. So, what are the benefits of using jwt? The first point is that the server does not need to maintain a login table, which saves space, especially when there are many users. This is the end of this article about where the token generated by node using jwt should be stored. For more information about where the token generated by jwt should be stored, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed tutorial on installing Docker on Windows
>>: How to implement remote access control in Centos 7.4
Table of contents Overview Global hook function R...
Preface Every time I use the terminal to create a...
Preface: This article mainly introduces the query...
In fact, it is very simple to encapsulate axios i...
Table of contents Deploy nginx on server1 Deploy ...
1. Problem The problems encountered when initiali...
1. Background As the project's business conti...
Students who make websites often find that some n...
1. Introduction Responsive Web design allows a we...
introduction Let's start with our content. I ...
Today we will look at why master-slave delay occu...
PS: I've recently been reading the Nginx chap...
Table of contents 1. Commonjs exports and require...
Detailed example of MySQL exchange partition Pref...
After this roll call device starts calling the ro...