Skip to main content

JWT Policy

Overview​

JWT (JSON Web Token) policy is used to validate the token included in API requests. It ensures that only users with valid tokens, generated using the correct JWT KEY and SECRET of the project, can access the API.

This policy supports token validation based on selected paths using either RequiredPath or NotRequiredPath mode. Additionally, it can be combined with the tokenBlacklistLogoutPath function to invalidate tokens upon logout.

Supported Algorithms:

  • HS256 (HMAC SHA-256)
  • RS256 (RSA SHA-256)

JWT Token Rules​

To successfully authenticate a JWT token in APIM:

  • The JWT Token must be included in the Authorization header:
Authorization: bearer JWT_TOKEN
  • The token’s payload must contain:
    • "apim_key_claim_name" with the project’s JWT KEY.
    • "exp" field to indicate token expiration time.

Example Payload:

"apim_key_claim_name": "64c44d593be841bcec7fe768184e4c7e",
"exp": 1900000000

Example Header:

"alg": "HS256",
"typ": "JWT"

JWT Sample Token:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcGltX2tleV9jbGFpbV9uYW1lIjoiNjRjNDRkNTkzYmU4NDFiY2VjN2ZlNzY4MTg0ZTRjN2UiLCJleHAiOjE5MDAwMDAwMDB9.1kB5k-vM2Cef7ZYTyJUQ-LsDUmG4K7sJUfrJ3FqgYxQ

Configuration Details​

JWT Path Matching​

JWT verification can be applied to selected paths using two modes:

Option Description
RequiredPathApply JWT authentication only to the listed method/path pairs. All other paths will not require JWT.
NotRequiredPathApply JWT authentication to all paths except the ones listed.
note

Only one mode (RequiredPath or NotRequiredPath) can be used at a time. Switching modes will reset previous settings.

Input Format:

Method_Path

Examples:

GET_/v1/test
Applies to GET requests at /v1/test
POST_/v1/example
Applies to POST at /v1/example
ANY_/v1/apis
Applies to all HTTP methods at /v1/apis
GET_/v1/apis*
Applies to all subpaths under /v1/apis (e.g., /v1/apis/a)

tokenBlacklistLogoutPath​

This feature allows APIM to blacklist a JWT token when a logout request is made and successfully returns HTTP 200 from the API Gateway.

Toggle Field:

FieldDescription
useJWTBlacklistRevokingWhen enabled, JWT tokens will be added to the blacklist storage upon logout response. This prevents further use of the same token.
note

Cannot be changed: Toggle to prevent this policy from being removed.

Cannot change the setting value: Toggle to prevent changes to the configuration values after setup.

Redis Configuration (used to store blacklisted tokens)​

Option 1: APIMRedis - Default system Redis; no manual configuration needed.

Option 2: CustomRedis

Configuration FieldDescription
Redis HostAddress of the Redis server (e.g., 127.0.0.1).
Redis PortPort number of the Redis server (e.g., 6379).
Redis PasswordPassword for authentication.
Redis DatabaseSpecifies which database index to use (e.g., 1).
Redis Pool SizeMaximum number of connections in the Redis connection pool (default: 1000).
Redis Pool BacklogMaximum number of requests waiting for an available connection (default: 30).

Option 3: RedisCluster

Configuration FieldDescription
Redis Cluster NodesThe cluster node addresses (e.g., redis-cluster.apim:6379).
Redis Cluster PasswordPassword for authentication.
Redis Cluster NameSpecifies the cluster name (e.g., kong_redis_cluster).
Redis Connect TimeoutTimeout for establishing a Redis connection (default: 1000ms).
Redis Read TimeoutTimeout for reading data from Redis (default: 1000ms).
Redis Send TimeoutTimeout for sending data to Redis (default: 1000ms).
Redis Pool SizeMaximum number of connections in the Redis connection pool (default: 1000).
Redis Pool BacklogMaximum number of pending connections (default: 30).
Redis Keepalive TimeoutMaximum idle time before Redis connection is closed (default: 55000ms).