Jet Academy Loading
Məzmunə keç / Skip to content

What is JWT?

JWT stands for JSON Web Token. JWT is a token format used to transfer certain information — claims — between parties in a compact and URL-safe form. In simple terms, after a user logs into a system, the server may create a token to identify who the user is and what permissions they have. The Client can then present that token to the Server when sending requests to protected API endpoints. For example, a user logs into an e-commerce application. The Server verifies the login information and, if it is correct, creates a JWT. The Client sends that token to the Server with the next request. The Server verifies the token and decides whether the user is allowed to perform the requested operation.

A simplified process looks like this:

Login → Server → JWT → Client → API Request + JWT → Server

JWT is widely used especially in REST API, Backend, and Full-Stack applications for building authentication and authorization systems.

What Is JWT Used For?

One of the most common uses of JWT is transferring information about a user's identity and related claims between systems.

For example, a JWT may contain information about:

  • the user's ID;
  • who issued the token;
  • which application the token is intended for;
  • when the token expires.

JWT can be used in scenarios such as:

  • API authentication;
  • authorization;
  • access tokens;
  • transferring identity information between different services;
  • OAuth and OpenID Connect-based systems;
  • certain security scenarios in microservices.

However, JWT itself is not a complete authentication system. JWT is only a token format. The login process, password verification, authorization rules, token storage, and security mechanisms must be implemented separately.

How Does JWT Work?

In a typical web application, the process begins when the user logs in.

The user sends an email and password to the Server:

POST /api/login

Content-Type: application/json


{

"email": "aysel@example.com",

"password": "example-password"

}

The Server verifies the user's information. If authentication is successful, the Server may generate a JWT. After receiving the token, the Client includes it when sending a request to a protected endpoint:

GET /api/profile

Authorization: Bearer eyJ...

The Server verifies the token's signature and required claims. If the token is valid and the user has permission to access the requested resource, the request is processed.

What Parts Does a JWT Consist Of?

The most common signed JWT format consists of three parts:

Header.Payload.Signature

For example:

xxxxx.yyyyy.zzzzz

These three parts are separated by dots.

They are:

Header

Payload

Signature

An important point is that this three-part structure is mainly seen in JWTs signed using JWS. JWT can also be used in encrypted form as JWE, and the structure of such tokens is different.

What Is a JWT Header?

The Header stores technical information about the token.

For example:

{

"alg": "HS256",

"typ": "JWT"

}

Here:

alg — may indicate the cryptographic algorithm being used;

typ — may indicate the type of the token.

The Header is then encoded using Base64URL.

When verifying a token, the Server should not blindly trust the alg value written inside the token itself. The application should define in advance which algorithms it accepts and allow only those algorithms.

What Is a JWT Payload?

The Payload is the part of the JWT where claims are stored.

For example:

{

"sub": "12345",

"role": "admin",

"iat": 1795520000,

"exp": 1795523600

}

Claims such as the user's identifier, role, and token timing information may be stored here. The Payload is a JSON object. For this reason, understanding JSON makes JWT much easier to understand.

What Is a Claim?

A Claim is a name-value pair representing specific information inside a JWT.

For example:

{

"role": "admin"

}

Here, role is a claim.

The JWT standard defines several registered claims.

Common examples include:

iss — Issuer

sub — Subject

aud — Audience

exp — Expiration Time

nbf — Not Before

iat — Issued At

jti — JWT ID

Application-specific claims can also be used.

What Is exp?

exp — Expiration Time — specifies the time after which the token should no longer be accepted.

For example:

{

"exp": 1795523600

}

When validating a token, the Server should check whether the token has expired. Using access tokens without an expiration time is not considered a good security practice. If a token is stolen, limiting the amount of time it can be used helps reduce the risk.

What Is iat?

iat — Issued At — indicates when the JWT was created.

For example:

{

"iat": 1795520000

}

This information can be used to calculate the age of the token and apply certain security rules.

What Is nbf?

nbf — Not Before — indicates the time from which the token may be accepted.

For example:

{

"nbf": 1795520000

}

If the current time is earlier than nbf, the Server should not accept the token.

What Is iss?

iss — Issuer — indicates which system created the token.

For example:

{

"iss": "https://auth.example.com"

}

The application receiving the token should only accept tokens created by trusted issuers.

What Is aud?

aud — Audience — indicates which application or service the token is intended for.

For example:

{

"aud": "orders-api"

}

If a token was created for another API, validating the Audience helps prevent it from being used by the wrong service.

What Is sub?

sub — Subject — identifies the main subject the token belongs to. In authentication systems, this is often the user's identifier.

For example:

{

"sub": "user-8452"

}

The Backend can use this identifier to determine which user sent the request.

What Is jti?

jti — JWT ID — can be used as a unique identifier for a token.

For example:

{

"jti": "0b98f840-..."

}

This claim can be used in some systems for purposes such as token tracking and managing replay risks.

What Is a JWT Signature?

The Signature makes it possible to verify that the token was created using a specific key and that its contents have not been modified after signing.

For a signed JWT, the simplified logic looks like this:

Header


Payload


Key

↓

Signature

For example, if the Client tries to change:

{

"role": "user"

}

to:

{

"role": "admin"

}

the modified Payload will no longer match the original Signature. During Signature verification, the Server can detect that the token has been modified.

Is JWT Encrypted?

One of the most common misconceptions about JWT is that it is automatically encrypted. This is incorrect. In the most common signed JWTs, the Header and Payload are encoded using Base64URL.

Base64URL is not encryption. Anyone who obtains the token may be able to decode the Payload and view the information inside it.

For this reason, a regular signed JWT should not contain:

  • passwords;
  • bank card information;
  • secret API keys;
  • other confidential information.

If the information needs to remain confidential, an additional encryption mechanism and an appropriate token format are required.

What Is the Difference Between Encoding, Signing, and Encryption?

It is important not to confuse these three concepts. Encoding converts information into another representation.

For example, Base64URL.

Encoding does not make information secret. Signing allows a system to verify that information has not been modified and was signed using the appropriate key. Encryption makes information unreadable to parties that do not have the appropriate key. Depending on the use case, a JWT may be signed or encrypted.

What Is JWS?

JWS — JSON Web Signature — is a standard used to protect information with a digital signature or MAC.

The JWTs most commonly seen on the internet with the structure:

Header.Payload.Signature

typically use JWS Compact Serialization. In these tokens, the Payload is not confidential, but the Signature allows modifications to the information to be detected.

What Is JWE?

JWE — JSON Web Encryption — is a standard used to encrypt JSON-based information. If the claims inside a JWT must not be readable by third parties, JWE can be used.

Therefore, you can think of them as:

JWS → integrity / signature

JWE → confidentiality / encryption

What Is JWT Authentication?

Authentication is the process of determining who the user is. For example, the user enters an email and password. The Backend checks whether the information is correct. If Authentication is successful, the system may create a token for the user. In later requests, the JWT can be used to present the authentication result and certain claims to the Server. JWT itself does not verify the user's password. Password verification is a separate part of the Authentication system.

What Is the Difference Between Authentication and Authorization?

Authentication answers the question:

“Who are you?”

Authorization answers the question:

“Are you allowed to do this?”

For example, a user successfully logs in.

That is Authentication.

However, only an administrator may be allowed to access:

DELETE /api/users/15

The Backend decides whether to allow the operation based on the user's role and permission information.

That is Authorization.

How Is JWT Used in an API?

JWT can be widely used to protect REST APIs.

For example, a public endpoint such as:

GET /api/products

may not require a token.

However:

GET /api/profile

may only be accessible to logged-in users.

When sending a request, the Client can provide the token through the HTTP Authorization Header:

Authorization: Bearer eyJ...

After verifying the token, the Server may allow the request to continue. Understanding the fundamentals of APIs is important for understanding how JWT is used in real applications.

What Is a Bearer Token?

A Bearer Token is a type of credential that can be used by the party presenting it to access a particular resource.

In an HTTP request, it often looks like this:

Authorization: Bearer <token>

JWT can be used as a Bearer Token. However, not every Bearer Token is a JWT. An Access Token may also be created as an opaque token — a random token whose meaning cannot be interpreted by the Client.

What Is an Access Token?

An Access Token is a credential used to access protected APIs and resources. JWT can be used as an Access Token format.

For example:

Login

↓

Access Token

↓

GET /api/profile

↓

200 OK

An Access Token is usually valid for a limited period of time. After it expires, a new Access Token is required.

What Is a Refresh Token?

A Refresh Token is a credential used to obtain a new Access Token.

For example:

Access Token

↓

Expired

↓

Refresh Token

↓

New Access Token

A Refresh Token usually has a longer lifetime than an Access Token and should be protected more carefully. A Refresh Token does not have to be a JWT. In many systems, it may also be used as an opaque token.

What Is the Difference Between an Access Token and a Refresh Token?

An Access Token is used to directly access API resources. A Refresh Token is mainly used to obtain a new Access Token.

A simple comparison:

Access Token → access to the API

Refresh Token → obtain a new Access Token

A Refresh Token should not be sent to endpoints with every normal request. Its use should be limited to the specific token renewal process.

What Is the Difference Between JWT and Session?

JWT and server-side sessions can be used in two different approaches to Authentication and Session management. In a traditional Session model, the Server stores the user's Session information on the Server side. The Client usually stores only a random Session identifier.

For example:

Client → Session ID

Server → Session Data

In a JWT-based approach, certain claims may be carried inside the token.

Client → Signed JWT

Server → Token Verification

However, this does not mean that no state is stored on the Server. In real JWT systems, Refresh Tokens, revocation lists, user status, permissions, and other information may still be stored on the Server side. For this reason, the statement “JWT is always stateless” should not be treated as an absolute rule.

Are JWT and Cookie the Same Thing?

No. JWT is a token format. Cookie is a browser mechanism used to store information and send it to a Server through HTTP requests. A JWT can be stored inside a Cookie. For example, a Server may send a JWT as an HttpOnly Cookie. In other systems, the token may be managed in another way.

Therefore:

JWT → information/token format

Cookie → browser storage and transport mechanism

Is It Safe to Store JWT in localStorage?

Where an Authentication Token is stored is an important security architecture decision.

localStorage can be read by JavaScript. If an application has an XSS vulnerability, malicious JavaScript may be able to read tokens stored there. For this reason, automatically storing Authentication Tokens in localStorage should not be considered a secure default choice for browser applications. In many web scenarios, an HttpOnly, Secure, and appropriately configured SameSite Cookie or a Backend-for-Frontend approach may be more suitable. However, the correct solution depends on the application's architecture and security requirements.

What Is HttpOnly?

HttpOnly is a Cookie attribute that helps prevent the Cookie from being read directly through JavaScript in the browser.

For example:

Set-Cookie: access_token=...; HttpOnly; Secure

Although it does not completely eliminate XSS risk, it can reduce the risk of direct token theft through JavaScript.

What Is Secure?

The Secure attribute helps ensure that the Cookie is sent only over secure HTTPS connections. Authentication and Token systems should operate over HTTPS. Transmitting tokens over unencrypted HTTP connections may increase the risk of interception.

What Is SameSite?

The SameSite Cookie attribute helps control when a Cookie is sent with cross-site requests. It is one of the mechanisms used to reduce CSRF risk. The appropriate SameSite value depends on the application's architecture and the Frontend/Backend domain structure.

Is JWT Secure?

JWT can be part of a secure system when implemented correctly. However, simply using JWT does not automatically make a system secure.

Security depends on factors such as:

  • trusted cryptographic algorithms;
  • proper key management;
  • Signature verification;
  • Token expiration;
  • Issuer and Audience validation;
  • secure Token storage;
  • HTTPS;
  • Authorization;
  • XSS and CSRF protection;
  • Refresh Token management;
  • Token revocation strategy.

For this reason, an Authentication system cannot be considered complete simply by generating a JWT and sending it to the Client.

Why Should the JWT Signature Be Verified?

When the Server receives a JWT, it should verify its Signature. Otherwise, an attacker may modify the token Payload and the Server may trust the modified information.

For example, a user may change:

{

"role": "user"

}

to:

{

"role": "admin"

}

Signature verification helps detect this modification. If Token verification fails, the request should be rejected.

Why Is alg Security Important in JWT?

The alg field inside the JWT Header may indicate which cryptographic algorithm was used. The Server should not automatically accept any algorithm presented by the token. The application should define in advance which algorithms are trusted and only accept algorithms included in that allowlist. For example, if the application is designed for RS256, a token unexpectedly using another algorithm should not automatically be accepted.

What Is the Difference Between HS256 and RS256?

HS256 uses HMAC SHA-256. The same shared secret is used to sign the token and verify the Signature.

Shared Secret

↓

Sign

↓

Verify

RS256 uses RSA.

The Private Key signs the token. The Public Key is used for Signature verification.

Private Key → Sign

Public Key → Verify

One advantage of this approach is that services verifying the token may not need access to the Private Key. The appropriate algorithm depends on the application's architecture and security requirements.

What Happens to JWT When You Log Out?

In JWT-based systems, the Logout process depends on the architecture.If the Access Token is stored only on the Client side, the Client can delete the token. However, if the token is not invalidated by the Server in another way and has not yet expired, a stolen copy may technically remain usable until its expiration time.

Different approaches can be used to manage this problem:

  • short-lived Access Tokens;
  • Refresh Token rotation;
  • Server-side Refresh Token storage;
  • Token revocation;
  • denylist;
  • Server-side management of user Sessions.

The Logout strategy should be considered in advance when designing a JWT system.

What Is Token Revocation?

Token Revocation means invalidating a token before its normal expiration time. For example, if a user's account is blocked or a token is stolen, the token may need to stop being accepted immediately. For this purpose, the Server may maintain a denylist based on jti or use another server-side Session or Token mechanism. This is another example showing that not every JWT-based system is completely stateless.

How Is JWT Used in Spring Boot?

In Java Backend projects, JWT is often used together with Spring Security.

A simplified process may look like this:

Login Request

↓

Authentication

↓

JWT Is Created

↓

Client Receives JWT

↓

Protected API Request

↓

Spring Security Filter

↓

JWT Is Verified

↓

Authorization

The Server verifies the user's login information. Then an appropriate JWT is created and returned to the Client. When a request arrives for a protected endpoint, a Filter verifies the token and may add the user's identity and permission information to the Security Context. JET Academy's Java Backend program separately covers Spring Security, Authentication, Authorization, and the creation, signing, and verification of JWT Tokens.

How Is JWT Used in Node.js and Express.js?

In Node.js Backend applications, JWT can be verified through Middleware.

For example, the request:

GET /api/profile

Authorization: Bearer eyJ...

arrives at the Server.

The Middleware retrieves the token from the Authorization Header. The token is verified. If it is valid, the request is passed to the next Handler. If it is invalid, an appropriate HTTP Error is returned. In Full-Stack projects, this approach can be used together with Express.js, MongoDB, and other Backend technologies.

What Is the Relationship Between JWT and Next.js?

Authentication in Next.js applications can be implemented in different ways. JWT may be used as the format for Session information or Access Tokens in certain Authentication architectures. Ready-made libraries and Authentication Providers can also be used in Next.js projects. The important point is not to use JWT simply for the sake of implementing it yourself, but to choose an Authentication architecture that matches the project's security and Session requirements.

Are JWT and OAuth the Same Thing?

No. JWT is a Token format. OAuth is a framework/protocol family for Authorization. In an OAuth system, an Access Token may be in JWT format, but this is not always the case. An opaque Access Token may also be used.

Therefore:

JWT → Token format

OAuth → Authorization framework

is a more accurate way to think about them.

What Is the Relationship Between JWT and OpenID Connect?

OpenID Connect is an Authentication protocol that adds an Identity Layer on top of OAuth 2.0. In OpenID Connect, the ID Token is provided in JWT format. The ID Token carries certain claims about the user's Authentication event and identity information. Access Token and ID Token do not serve the same purpose. The ID Token informs the Client application about the user's Authentication information. The Access Token is intended for accessing API resources.

What Are the Most Common JWT Mistakes?

There are several common mistakes in JWT implementations:

  • storing confidential information inside the Payload;
  • treating Base64URL as Encryption;
  • not performing Signature verification;
  • not checking the exp claim;
  • accepting any alg value;
  • not validating iss and aud;
  • creating long-lived Access Tokens;
  • ignoring Token storage;
  • not using HTTPS;
  • using the Refresh Token as an Access Token;
  • treating Authentication and Authorization as the same concept.

Preventing these problems is important for the security of a JWT-based system.

How Are JWT Errors Investigated?

Different problems may occur in APIs that use JWT.

For example:

401 Unauthorized

The Token may be missing, expired, or verification may have failed.

403 Forbidden

The user has successfully authenticated but does not have sufficient permission for the operation. A Token parsing error may indicate that the Token format is incorrect. A Signature verification error may indicate that the Token was modified, the wrong Key was used, or the wrong Algorithm was used. An Expired Token error indicates that the exp time has passed. Systematically investigating these problems is part of the Debugging process.

What Should You Know Before Learning JWT?

Before starting with JWT, it is useful to understand several fundamental concepts. First, you should understand how HTTP Requests and Responses work.

Then you can learn:

  • APIs and REST APIs;
  • JSON;
  • HTTP Headers;
  • Status Codes.

After that, it is important to understand the difference between Authentication and Authorization.

A possible learning path is:

HTTP → JSON → API → REST API → Authentication → Authorization → JWT → Access Token → Refresh Token → OAuth / OpenID Connect

Those developing Backend skills can later move on to framework-specific topics such as:

  • Token Security;
  • Spring Security;
  • Django Authentication;
  • Node.js Middleware.

JWT should be learned not simply as a command for generating Tokens, but as part of a broader Authentication and Authorization architecture. If you want to learn JWT, Spring Security, REST API, PostgreSQL, Docker, and other modern Backend technologies through real-world projects, you can explore JET Academy's Java Backend Programming Course. If you want to build complete web applications using React, Next.js, Node.js, Express.js, MongoDB, and JWT, you can also explore JET Academy's Full-Stack Programming Course.

Fill the form to learn more about our IT courses

Start learning IT today