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:
Header.Payload.Signature
Here is an example given by jwt.io
:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
For this example, the three parts are:
// header
{
"alg": "HS256",
"typ": "JWT"
}
// Payload
{
"sub": "1234567890",
"name": "John Doe",
"iat": 1516239022
}
// Signature
HMACSHA256(
base64UrlEncode(header) + "." +base64UrlEncode(payload),
"your-256-bit-secret"
)
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.
base64UrlEncode({"alg": "HS256","typ": "JWT"}) === "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9";
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 thejwt
.sub
: The subject of thejwt
.aud
: The audience of thejwt
.exp
: The expiration time of thejwt
.nbf
: The "not before" time of thejwt
.iat
: The issuance time of thejwt
.jti
: The unique identifier of thejwt
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.
base64UrlEncode({"sub": "1234567890","name": "John Doe","iat": 1516239022}) === "eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ";
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.
HMACSHA256( base64UrlEncode(header) + "." +base64UrlEncode(payload),"your-256-bit-secret") === "SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
- Stateless and extensible:
JWT
is stored on the client side, making it stateless and easy to expand. - CSRF attack prevention: By using
JWT
placed in theAuthorization: Bearer ${JWT}
field of the request header instead of usingCookie
,CSRF
attacks can be effectively prevented. Payload
can store non-sensitive information necessary for other business logic, thereby reducing the server's load.- Cross-language support: Due to the universal nature of
json
and encryption algorithms,JWT
is supported in most programming languages. - Lighter than
OAuth2
: Although with different use cases,OAuth2
being an authorization framework andJWT
being an authentication protocol for user authentication and protection of backendAPI
in a frontend-backend separation scenario.
- Once issued, cannot be updated: To update the data or renew, a reissue is necessary.
- Cannot be revoked:
JWT
remains valid until its expiration time arrives. Payload
isBASE64URL
encoded and not encrypted, thus sensitive data should not be stored in thejwt
without encryption.- Potential security risks: Information leakage can result in unauthorized access to all token permissions, so the
JWT
validity period should not be too long. - Forgery of
JWT
: Attackers can forge theHeader
inJWT
by settingalg
asnone
to verify identity, and some libraries implement this verification by default, thus requiring disabling requests withalg
asnone
. HMAC
key brute forcing: Users can crack the key from the completeJWT
information, as the encryption algorithm is included in thejwt
. This operation can be done locally without server interaction.
CSRF https://github.com/WindrunnerMax/EveryDay/blob/master/Browser/CSRF%E8%B7%A8%E7%AB%99%E8%AF%B7%E6%B1%82%E4%BC%AA%E9%80%A0.md
XSS https://github.com/WindrunnerMax/EveryDay/blob/master/Browser/XSS%E8%B7%A8%E7%AB%99%E8%84%9A%E6%9C%AC%E6%94%BB%E5%87%BB.md
Cookie and Session https://github.com/WindrunnerMax/EveryDay/blob/master/HTML/Cookie%E4%B8%8ESession.md
https://github.com/WindrunnerMax/EveryDay
https://www.freebuf.com/vuls/219056.html
https://blog.csdn.net/qq_28165595/article/details/80214994