All Products
Search
Document Center

Microservices Engine:jwt-logout

Last Updated:Mar 11, 2026

JWTs are stateless -- once issued, they remain valid until they expire. The jwt-logout plug-in uses Redis to add lightweight session tracking, enabling two capabilities that JWTs lack by default:

  • Token revocation: Invalidate a specific JWT before it expires.

  • Single-device login: Restrict an account to one active session at a time, so logging in on a new device automatically invalidates the previous session.

Note

This plug-in requires gateway version 1.2.32 or later.

How it works

The plug-in stores JWT metadata in Redis to track revoked tokens and active sessions.

Token revocation flow

  1. A client sends a request to a path ending with the configured logout suffix (default: /jwt_logout).

  2. The gateway extracts identifying fields from the JWT payload (default: jti) and writes a key to Redis.

  3. For each subsequent request, the gateway checks whether the JWT's identifying fields match a key in Redis. If a match exists, the request is rejected with a 401 response.

  4. The Redis key expires automatically based on the JWT's exp claim, so no manual cleanup is needed.

Single-device login flow

  1. A client sends a request with a valid JWT. The gateway extracts the configured identity fields from the payload (default: iss, aud, sub) and checks Redis.

  2. If no matching key exists, the gateway writes the full JWT to Redis and allows the request. This JWT becomes the "active session."

  3. If a matching key exists but the stored JWT differs from the current one, the gateway rejects the request with a 403 response -- the account is already active on another device.

  4. To switch the active session to a new device, send a request to the configured login path suffix (default: /jwt_login). The gateway overwrites the stored JWT, invalidating the previous session.

Plug-in type

Authentication

Configuration reference

The plug-in configuration has three main blocks:

redis:       # Required. Redis connection settings.
logout:      # Optional. Token revocation settings.
login:       # Optional. Single-device login settings.

Top-level fields

NameTypeRequiredDefaultDescription
jwksstringNo-JSON Web Key Set (JWKS) for JWT signature verification. Not required when used together with the jwt-auth plug-in. See RFC 7517.
clock_skewnumberNo60Allowed clock offset when validating exp and iat claims. Unit: seconds.
token_headerstringNoAuthorizationRequest header that contains the JWT.
token_prefixstringNoBearerPrefix to strip from the header value before parsing the JWT.
redisobjectYes-Redis connection settings. See Redis fields below.
logoutobjectNo-Token revocation settings. If omitted, token revocation is disabled. See Logout fields below.
loginobjectNo-Single-device login settings. If omitted, single-device login is disabled. See Login fields below.

Redis fields

NameTypeRequiredDefaultDescription
servicestringYes-Redis service name. Format depends on the service source: my-redis.static (fixed address), my-redis.dns (DNS domain name), or my-redis.default.svc.cluster.local (ACK).
portnumberYes-Redis service port.
usernamestringNo-Username for the Redis AUTH command.
passwordstringNo-Password for the Redis AUTH command.
timeoutnumberNo1000Redis command timeout. Unit: milliseconds.

Logout fields

NameTypeRequiredDefaultDescription
key_prefixstringNohigress_jwt_logout_Prefix for the Redis key.
keyarray of stringNo["jti"]JWT payload fields that identify a token. Tokens with identical values for these fields are treated as the same token. If the JWT payload is missing any of these fields, the gateway returns 401 invalid token.
pathstringNo/jwt_logoutURL path suffix that triggers a logout. When a request path ends with this value, the JWT in the request is revoked.
error_statusnumberNo401HTTP status code returned when a revoked token is used.
error_bodystringNo{"message":"invalid token"}Response body returned when a revoked token is used.
ttlnumberNo-Time-to-live for the Redis key. Unit: seconds. Determines how long the token stays revoked. Default: exp claim value minus the current time. If the JWT has no exp claim, defaults to 86,400 seconds (24 hours).

Login fields

NameTypeRequiredDefaultDescription
key_prefixstringNohigress_jwt_logout_Prefix for the Redis key.
keyarray of stringNo["iss","aud","sub"]JWT payload fields that identify an account session. Tokens with identical values for these fields are considered the same account. If the JWT payload is missing any of these fields, the gateway returns 401 invalid token.
pathstringNo/jwt_loginURL path suffix that triggers a forced login. Sending a request to this path overwrites the active session in Redis with the current JWT.
error_statusnumberNo403HTTP status code returned when a duplicate login is detected.
error_bodystringNo{"message":"already login on other device"}Response body returned when a duplicate login is detected.
ttlnumberNo-Time-to-live for the Redis key. Unit: seconds. Determines how long a login session stays active. Default: exp claim value minus the current time. If the JWT has no exp claim, defaults to 86,400 seconds (24 hours).

Redis key format

The plug-in constructs Redis keys with the following pattern:

<key_prefix><PayloadKey1>#<PayloadKey2>##<PayloadValue1>#<PayloadValue2>

Examples:

  • Logout key: higress_jwt_logout_jti##xxxx

  • Login key: higress_jwt_login_iss#aud#sub##abcd#www.example.com#test

Connect to an ApsaraDB for Redis instance

To set up a Redis connection for this plug-in:

  1. Create an ApsaraDB for Redis instance. For details, see Overview.

  2. Get the VPC endpoint of the instance (for example, r-xxxxxxx.redis.rds.aliyuncs.com).

  3. Add a service in the MSE console. Select DNS Domain Name from the Service Source drop-down list, enter the Redis port (6379 in most cases) in the Service Port field, enter the VPC endpoint in the Domain Names field, and set TLS Mode to Disabled. For details, see Add a service.

  4. Add the Redis connection block to the plug-in configuration: If the Redis instance requires authentication, include the password:

       redis:
         service: redis.dns
         port: 6379
       redis:
         service: redis.dns
         port: 6379
         password: <your-redis-password>   # Replace with your actual password.

Capacity planning: Each request triggers one Redis read for logout checks and two reads for login checks. Logout and first-time login requests generate two additional writes. To estimate Redis capacity, multiply your gateway request throughput by 2.

Example: Revoke a JWT on logout

Invalidate a specific JWT so it can no longer be used for authentication.

Plug-in configuration:

redis:
  service: redis.dns
  port: 6379
jwks: |
  {
    "keys": [
      {
        "kty": "oct",
        "kid": "123",
        "k": "hM0k3AbXBPpKOGg__Ql2Obcq7s60myWDpbHXzgKUQdYo7YCRp0gUqkCnbGSvZ2rGEl4YFkKqIqW7mTHdj-bcqXpNr-NOznEyMpVPOIlqG_NWVC3dydBgcsIZIdD-MR2AQceEaxriPA_VmiUCwfwL2Bhs6_i7eolXoY11EapLQtutz0BV6ZxQQ4dYUmct--7PLNb4BWJyQeWu0QfbIthnvhYllyl2dgeLTEJT58wzFz5HeNMNz8ohY5K0XaKAe5cepryqoXLhA-V-O1OjSG8lCNdKS09OY6O0fkyweKEtuDfien5tHHSsHXoAxYEHPFcSRL4bFPLZ0orTt1_4zpyfew",
        "alg": "HS256"
      }
    ]
  }
logout:
  path: "/jwt_logout"
  key: ["jti"]
  error_status: 401
  error_body: |
    {"message":"invalid token"}

Step 1: Trigger a logout.

curl http://xxx.hello.com/test/jwt_logout \
  -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJ4eHh4IiwiaXNzIjoiYWJjZCIsInN1YiI6InRlc3QiLCJhdWQiOiJ3d3cudGVzdC5jb20iLCJpYXQiOjE2NjU2NjA1MjcsImV4cCI6MTg2NTY3MzgxOX0.tmKF6qc1mOWNyCCzBOT2XKNoEGeEgr3EbhTKAQfq1io'

# Response:
{"message": "logout success"}

The JWT payload contains the following claims:

{
  "jti": "xxxx",
  "iss": "abcd",
  "sub": "test",
  "aud": "www.test.com",
  "iat": 1665660527,
  "exp": 1865673819
}

With key set to ["jti"], the gateway writes the Redis key higress_jwt_logout_jti##xxxx.

Step 2: Verify that the revoked token is rejected.

curl http://xxx.hello.com/test/abc \
  -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJ4eHh4IiwiaXNzIjoiYWJjZCIsInN1YiI6InRlc3QiLCJhdWQiOiJ3d3cudGVzdC5jb20iLCJpYXQiOjE2NjU2NjA1MjcsImV4cCI6MTg2NTY3MzgxOX0.tmKF6qc1mOWNyCCzBOT2XKNoEGeEgr3EbhTKAQfq1io'

# Response:
{"message":"invalid token"}

The gateway finds the key higress_jwt_logout_jti##xxxx in Redis and rejects the request.

Note

The logout endpoint only revokes the token included in the current request. To revoke a different token, manually create the corresponding Redis key using the key format described above.

Example: Enforce single-device login

Restrict an account to one active session and force-switch the session to a new device.

Plug-in configuration:

redis:
  service: redis.dns
  port: 6379
jwks: |
  {
    "keys": [
      {
        "kty": "oct",
        "kid": "123",
        "k": "hM0k3AbXBPpKOGg__Ql2Obcq7s60myWDpbHXzgKUQdYo7YCRp0gUqkCnbGSvZ2rGEl4YFkKqIqW7mTHdj-bcqXpNr-NOznEyMpVPOIlqG_NWVC3dydBgcsIZIdD-MR2AQceEaxriPA_VmiUCwfwL2Bhs6_i7eolXoY11EapLQtutz0BV6ZxQQ4dYUmct--7PLNb4BWJyQeWu0QfbIthnvhYllyl2dgeLTEJT58wzFz5HeNMNz8ohY5K0XaKAe5cepryqoXLhA-V-O1OjSG8lCNdKS09OY6O0fkyweKEtuDfien5tHHSsHXoAxYEHPFcSRL4bFPLZ0orTt1_4zpyfew",
        "alg": "HS256"
      }
    ]
  }
login:
  path: "/jwt_login"
  key: ["iss","aud","sub"]
  error_status: 403
  error_body: |
    {"message":"already login on other device"}

Step 1: First login (auto-registered).

curl http://xxx.hello.com/test/abc \
  -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJ6enp6IiwiaXNzIjoiYWJjZCIsImF1ZCI6Ind3dy5leGFtcGxlLmNvbSIsInN1YiI6InRlc3QiLCJpYXQiOjE2NjU2NjA1MjcsImV4cCI6MTg2NTY3MzgxOX0.WljMr5ucxfLF8SmeaaL25c0QG3IX04HoD0als9gglYg'

The JWT payload:

{
  "jti": "zzzz",
  "iss": "abcd",
  "aud": "www.example.com",
  "sub": "test",
  "iat": 1665660527,
  "exp": 1865673819
}

No matching key exists in Redis, so the gateway writes higress_jwt_login_iss#aud#sub##abcd#www.example.com#test and allows the request. This JWT becomes the active session.

Step 2: Attempt login from another device.

A different JWT with the same iss, aud, and sub values but a different jti (yyyyy instead of zzzz):

curl http://xxx.hello.com/test/abc \
  -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjEyMyJ9.eyJqdGkiOiJ5eXl5eSIsImlzcyI6ImFiY2QiLCJhdWQiOiJ3d3cuZXhhbXBsZS5jb20iLCJzdWIiOiJ0ZXN0IiwiaWF0IjoxNjY1NjYwNTI5LCJleHAiOjE4NjU2NzM4MTl9.6vi6eKPWSKHQxfzBPrj3-SWI4Q5zGtWhqp38JIN3FEo'

# Response:
{"message":"already login on other device"}

The identity fields (iss, aud, sub) match an existing Redis key, but the full JWT differs. The gateway rejects the request.

Step 3: Force login from the new device.

Send a request to the login path suffix to overwrite the active session:

curl http://xxx.hello.com/test/jwt_login \
  -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjEyMyJ9.eyJqdGkiOiJ5eXl5eSIsImlzcyI6ImFiY2QiLCJhdWQiOiJ3d3cuZXhhbXBsZS5jb20iLCJzdWIiOiJ0ZXN0IiwiaWF0IjoxNjY1NjYwNTI5LCJleHAiOjE4NjU2NzM4MTl9.6vi6eKPWSKHQxfzBPrj3-SWI4Q5zGtWhqp38JIN3FEo'

# Response:
{"message":"login success"}

The gateway overwrites the Redis value with the new JWT. After this:

  • Requests with the new JWT succeed.

  • Requests with the old JWT (from Step 1) are rejected with {"message":"already login on other device"}.

Note

The first request with a valid JWT is automatically registered as the active session. You do not need to call the forced login endpoint for initial logins. Use it only to switch sessions between devices.

Usage notes

  • The default logout key ["jti"] relies on the jti (JWT ID) claim, which uniquely identifies each token per RFC 7519. If two tokens share the same jti, they are treated as identical.

  • The default login key ["iss","aud","sub"] combines issuer, audience, and subject -- a combination that typically represents a unique user-application pair, suitable for single-device enforcement.

  • Triggering a logout also clears any associated login key for the same JWT. This applies regardless of whether token revocation, single-device login, or both are enabled.

HTTP status codes

Status codeMessageCause
401invalid tokenNo JWT in the request header, invalid JWT format, expired JWT, or the token has been revoked.
500redis server errorRedis connection timed out or failed.