Dev / IT6 min read

What is JWT? Understanding JSON Web Tokens

A complete guide to JSON Web Tokens — how they work, what the header, payload, and signature mean, and when to use them in your applications.

What is a JSON Web Token?

A JSON Web Token (JWT) is a compact, URL-safe way to represent claims between two parties. It is widely used for authentication and authorization in web applications and APIs. When a user logs in, the server creates a JWT and sends it to the client. The client then includes this token in subsequent requests to prove its identity.

JWT is defined in RFC 7519 and has become the de facto standard for stateless authentication in REST APIs, microservices, and single-page applications.

The Structure of a JWT

A JWT consists of three parts separated by dots (.):

xxxxx.yyyyy.zzzzz Header.Payload.Signature

1. Header

The header typically contains two fields: the token type (typ) and the signing algorithm (alg). It is Base64URL encoded.

{
  "alg": "HS256",
  "typ": "JWT"
}

2. Payload

The payload contains claims — statements about the user and additional metadata. There are three types of claims:

{
  "sub": "1234567890",
  "name": "John Doe",
  "email": "[email protected]",
  "role": "admin",
  "iat": 1516239022,
  "exp": 1516325422
}

3. Signature

The signature verifies that the token has not been tampered with. It is created by combining the encoded header, encoded payload, and a secret key:

HMACSHA256( base64UrlEncode(header) + "." + base64UrlEncode(payload), secret )

💡 Important: The payload is only Base64URL encoded, not encrypted. Anyone can decode and read the payload. Never store sensitive data like passwords in a JWT unless you use JWE (JSON Web Encryption).

Common JWT Claims Explained

ClaimNameDescription
issIssuerWho issued the token (e.g. your auth server)
subSubjectWho the token is about (usually user ID)
audAudienceWho the token is intended for
expExpirationUnix timestamp when token expires
iatIssued AtUnix timestamp when token was issued
nbfNot BeforeToken is invalid before this timestamp
jtiJWT IDUnique identifier to prevent replay attacks

JWT Signing Algorithms

JWTs can be signed using symmetric or asymmetric algorithms:

AlgorithmTypeUse Case
HS256, HS384, HS512Symmetric (HMAC)Single server, shared secret
RS256, RS384, RS512Asymmetric (RSA)Multiple services, public key verification
ES256, ES384, ES512Asymmetric (ECDSA)High performance, smaller keys

How JWT Authentication Works

The typical JWT authentication flow looks like this:

  1. User submits username and password to the login endpoint
  2. Server validates credentials, creates a JWT signed with a secret key
  3. Server returns the JWT to the client
  4. Client stores the JWT (typically in memory or localStorage)
  5. Client includes the JWT in the Authorization header for protected requests: Authorization: Bearer <token>
  6. Server validates the JWT signature and checks expiration before processing the request

JWT Security Best Practices

⚠️ The "alg: none" attack: Always explicitly specify which algorithms are valid on your server. Never accept tokens with alg: none as this bypasses signature verification entirely.

JWT vs Session Tokens

JWTSession Token
StorageClient-sideServer-side
ScalabilityExcellent (stateless)Requires session store
RevocationDifficult (until expiry)Instant
SizeLarger (100-500 bytes)Tiny (32-128 bytes)
Best forAPIs, microservicesTraditional web apps

TRY THE FREE TOOL

JWT Decoder

Decode & verify JSON Web Tokens

Open Tool →
← Back to all articles