The acronym JSON WEB TOKEN is abbreviated as JWT and is an open standard based on JSON for securely exchanging information between two communicating parties in a concise, URL-safe declarative format, often used for authentication.
JWT consists of three parts separated by ., which are Header, Payload, and Signature, with the following structure:
Here is an example given by jwt.io:
For this example, the three parts are:
The Header consists of two parts: the type of token and the algorithm name. In this example, the token type is jwt, and the encryption algorithm is HMAC-SHA256. Encoding the Header using the BASE64URL algorithm yields the first part of the jwt. Note that BASE64URL algorithm encoding is slightly different from BASE64 encoding. BASE64URL requires replacing + with a minus sign -, / with _, and it has no standard filling, so the = is removed.
The Payload is the main body of the JWT and can store data information. It contains three parts: registered claims, public claims, and private claims.
iss: The issuer of the jwt.sub: The subject of the jwt.aud: The audience of the jwt.exp: The expiration time of the jwt.nbf: The "not before" time of the jwt.iat: The issuance time of the jwt.jti: The unique identifier of the jwt to prevent replay attacks.Any information can be added to the public claims. Typically, user and business information are added, but sensitive information is not recommended as the public claims can be decrypted on the client side unless the information is encrypted.
Private claims are defined jointly by the server and client. Sensitive information is also not recommended here.
The Signature is a hash signature generated by encoding and hashing the first two parts of the data using the algorithm defined in the Header of the JWT. This is primarily to ensure that the data has not been tampered with.
JWT is stored on the client side, making it stateless and easy to expand.JWT placed in the Authorization: Bearer ${JWT} field of the request header instead of using Cookie, CSRF attacks can be effectively prevented.Payload can store non-sensitive information necessary for other business logic, thereby reducing the server's load.json and encryption algorithms, JWT is supported in most programming languages.OAuth2: Although with different use cases, OAuth2 being an authorization framework and JWT being an authentication protocol for user authentication and protection of backend API in a frontend-backend separation scenario.JWT remains valid until its expiration time arrives.Payload is BASE64URL encoded and not encrypted, thus sensitive data should not be stored in the jwt without encryption.JWT validity period should not be too long.JWT: Attackers can forge the Header in JWT by setting alg as none to verify identity, and some libraries implement this verification by default, thus requiring disabling requests with alg as none.HMAC key brute forcing: Users can crack the key from the complete JWT information, as the encryption algorithm is included in the jwt. This operation can be done locally without server interaction.