JWT introduction, jwt Introduction
JWT is a concise and URL-safe declarative specification used to transmit security information between two parties. As an open standard (RFC 7519), JWT defines a concise and self-contained Method for Safe Transmission of information between communication parties in the form of Json objects. Because of the existence of digital signatures, the information is credible. JWT can use the HMAC algorithm or the RSA public/private key pair for signature.
-
Compact): It can be sent through URL, POST parameter, or HTTP header. Because the data volume is small, the transmission speed is fast.
-
Self-contained): The load contains the information required by all users, avoiding multiple queries to the database.
Main application scenarios of JWT
-
Identity Authentication
In this scenario, once the user completes the login, each subsequent request contains JWT, which can be used to verify the user's identity and verify the access permissions of routes, services, and resources. Because of its low overhead, it can be easily transmitted in systems with different domain names, which is widely used in single sign-on (SSO.
-
Information exchange
It is safe to use JWT to encode data between communication parties. because the information is signed, it can ensure that the information sent by the sender is not forged.
JWT Structure
JWT includes the usage.Separated three parts:
Its structure looks like this
xxxxx.yyyyy.zzzzz
Header
The header usually contains two parts: the token type and the encryption algorithm used.
{ "alg": "HS256", "typ": "JWT"}
Next we will use this part of contentBase64UrlEncoding forms the first part of the JWT structure.
Payload
The second part of Token is the load. It contains the claim, which is the State and additional metadata of some entities (usually referred to as users). There are three types of Claim:Reserved,PublicAndPrivate.
-
Reserved claims: These claim are pre-defined by JWT. They are not mandatory in JWT, but are recommended.Iss (issuer),Exp (expiration timestamp),Sub (target user),Aud (receiver),Iat (issue time).
-
Public claims: Define your own fields as needed. Avoid conflicts.
-
Private claims: These are custom fields that can be used to exchange information between the two parties.
Load example:
{ "sub": "1234567890", "name": "John Doe", "admin": true}
The above load needs to go throughBase64UrlEncoded as the second part of the JWT structure.
Signature
To create a signature, you must use the encoded header, payload, and a secret key to sign the signature using the signature algorithm specified in the header. For example, if you want to use the HMAC SHA256 algorithm, the signature should be created using the following method:
HMACSHA256( base64UrlEncode(header) + "." + base64UrlEncode(payload), secret)
The signature is used to verify that the sender and message of a message have not been tampered.
Complete JWT
The output in JWT format is.Separated three-segment Base64 encoding. Compared with XML-based standards such as SAML, JWT is easier to pass in HTTP and HTML environments.
The following JWT shows a complete JWT format. It Concatenates the previous Header, Payload, and key signature:
How to Use JWT?
In the implementation of identity authentication, the traditional method is to store a session on the server and return a cookie to the client. After JWT is used, when the user logs on to the system using its authentication information, a jwt is returned. You only need to save the token locally (usually using local storage or using cookies.
When a user wants to access a protected route or resourceAuthorizationHeader usageBearerMode to add JWT, the content looks like the following:
Authorization: Bearer <token>
Because the user's status is not stored in the server's memory, this isStatelessAuthentication mechanism. The server-side protection route will check the Request HeaderAuthorizationIf the JWT information in is valid, user behavior is allowed. Because JWT is self-contained, it reduces the need to query databases.
These features of JWT allow us to rely entirely on stateless features to provide data API services, or even create a download stream service. Because JWT does not use cookies, you can use any domain name to provide your API service without worrying about cross-origin Resource Sharing (CORS ).
The following sequence diagram shows the process:
Why use JWT?
Compared with the XML format, JSON is simpler and smaller after encoding, which makes JWT simpler than SAML and is more suitable for passing in HTML and HTTP environments.
In terms of security, SWT can only use the HMAC algorithm and the shared symmetric key for signature, while JWT and SAML token can use X.509-authenticated public/private key pairs for signature. Compared with simple JSON, XML and XML Digital Signatures introduce complex security vulnerabilities.
Because JSON can be directly mapped to objects, JSON parser is provided in most programming languages, while XML does not have such a natural document-object ing relationship, this makes it easier to use JWT than SAML.