All Products
Search
Document Center

Microservices Engine:jwt-auth

Last Updated:Mar 11, 2026

The jwt-auth plug-in authenticates and authorizes HTTP requests based on JSON Web Tokens (JWTs). It extracts a JWT from request headers, URL parameters, or cookies, validates the token signature against a JSON Web Key Set (JWKS), and either forwards the request to backend services or rejects it with a 401 or 403 status code.

Each consumer is configured with its own JWT credentials, which enables fine-grained access control: define who can be verified (authentication) and where each consumer is allowed to go (authorization).

Note

For an alternative approach, see Configure JWT authentication for manual JWT authentication without the plug-in.

How it works

The jwt-auth plug-in uses a two-layer configuration model:

  • Authentication (instance level): Define consumers and their JWT credentials (JWKS). This determines _who_ can be verified.

  • Authorization (route or domain level): Specify which consumers can access specific routes or domains. This determines _where_ each consumer is allowed to go.

The request flow is:

  1. Define one or more consumers at the instance level, each with a name and a JWKS for signature verification.

  2. (Optional) Restrict access to specific routes or domain names by adding an allow list that references consumer names.

  3. When a request arrives, the plug-in extracts the JWT from the configured location (header, URL parameter, or cookie) and validates the signature against the consumer's JWKS.

  4. If validation succeeds, the request is forwarded to the backend service with the X-Mse-Consumer header set to the matched consumer name. If validation fails, the plug-in returns 401. If the consumer is not authorized for the matched route or domain, the plug-in returns 403.

Note

If a JWT matches multiple JWKS entries, the plug-in uses the first matched consumer based on configuration order.

Configuration reference

Authentication fields

Configure authentication at the instance level to define consumers and their JWT credentials.

FieldTypeRequiredDefaultDescription
consumersarray of objectYes-List of consumers. See Consumer fields
global_autharray of stringNo (required for instance-level configurations)-Controls the scope of authentication. true: applies to all routes globally. false: applies only to configured routes and domains. Not set: applies globally only when no routes or domains are configured

Consumer fields

Each entry in the consumers array supports the following fields:

FieldTypeRequiredDefaultDescription
namestringYes-Unique consumer name
jwksstringNo-A JSON Web Key Set (JWKS) as a JSON string. Contains the public keys or symmetric keys used to verify JWT signatures. Set either jwks or remote_jwks
remote_jwksobjectNo{"uri":"http://127.0.0.1/keys","service":"test.static","port":"80","ttl":30000,"timeout":3000}Fetch a JWKS from a remote URI on a schedule. If both jwks and remote_jwks are set, remote_jwks takes precedence. See remote_jwks fields
issuerstringNo-Expected JWT issuer. The plug-in verifies that this value matches the iss claim in the token payload
claimsobjectNo-Key-value pairs that must match the corresponding claims in the JWT payload. For example, "aud": "mobile-site" requires the aud claim to equal mobile-site
claims_to_headersarray of objectNo-Forward specific JWT claims as request headers to the backend service. See claims_to_headers fields
from_headersarray of objectNo[{"name":"Authorization","value_prefix":"Bearer"}]Request headers from which to extract the JWT. See from_headers fields
from_paramsarray of stringNoaccess_tokenURL parameters from which to extract the JWT
from_cookiesarray of stringNo-Cookies from which to extract the JWT
clock_skew_secondsnumberNo60Allowed clock skew (in seconds) when validating the exp and iat claims
keep_tokenboolNotrueWhether to retain the JWT in the request when forwarding to the backend service
Note

The default values for from_headers and from_params apply only when none of from_headers, from_params, or from_cookies are explicitly configured. If any one of the three is set, the defaults for the other two do not apply.

from_headers fields

FieldTypeRequiredDefaultDescription
namestringYes-Name of the request header containing the JWT
value_prefixstringYes-Prefix to strip from the header value before parsing the JWT. For example, set this to Bearer when extracting from the Authorization header

claims_to_headers fields

FieldTypeRequiredDefaultDescription
claimstringYes-The claim name in the JWT payload. The value must be a string or an unsigned integer
headerstringYes-The request header name to set with the extracted claim value
overrideboolNotruetrue: overwrite an existing header with the same name. false: append to the existing header

remote_jwks fields

Use remote_jwks to fetch a JWKS from a remote service at regular intervals.

FieldTypeRequiredDefaultDescription
uristringYes-URI to fetch the JWKS from
servicestringYes-Service name in one of the following formats: Kubernetes service (foo.default.svc.cluster.local), Nacos service (foo.DEFAULT-GROUP.public.nacos), DNS service (test.dns), or static IP service (test.static)
portboolYes-Service port
timeoutboolNo3000Request timeout in milliseconds
ttlboolNo30000Cache duration (time to live) in milliseconds. The plug-in refreshes the JWKS after this interval

Authorization fields

Configure authorization at the route or domain level to restrict access to specific consumers.

FieldTypeRequiredDefaultDescription
allowarray of stringNo (required for route- or domain-level configurations)-Consumer names that are permitted to access the route or domain
Important

Authentication and authorization configurations cannot coexist in the same rule. Define consumers at the instance level, and define allow at the route or domain level.

After a request passes both authentication and authorization, the plug-in adds the X-Mse-Consumer header to the forwarded request. The header value is the matched consumer name.

Disable the plug-in for a route

To exempt a specific route from JWT authentication, apply the following configuration to that route:

_disable_: true

When _disable_ is set to true and the request matches the route, the plug-in is bypassed. The route is accessible to all users without a JWT.

Configuration examples

Global authentication with route-level authorization

Enable JWT authentication globally and restrict specific routes or domains to specific consumers.

Step 1: Define consumers at the instance level

consumers:
- name: consumer1
  issuer: abcd
  jwks: |
    {
      "keys": [
        {
          "kty": "oct",
          "kid": "123",
          "k": "hM0k3AbXBPpKOGg__Ql2Obcq7s60myWDpbHXzgKUQdYo7YCRp0gUqkCnbGSvZ2rGEl4YFkKqIqW7mTHdj-bcqXpNr-NOznEyMpVPOIlqG_NWVC3dydBgcsIZIdD-MR2AQceEaxriPA_VmiUCwfwL2Bhs6_i7eolXoY11EapLQtutz0BV6ZxQQ4dYUmct--7PLNb4BWJyQeWu0QfbIthnvhYllyl2dgeLTEJT58wzFz5HeNMNz8ohY5K0XaKAe5cepryqoXLhA-V-O1OjSG8lCNdKS09OY6O0fkyweKEtuDfien5tHHSsHXoAxYEHPFcSRL4bFPLZ0orTt1_4zpyfew",
          "alg": "HS256"
        }
      ]
    }
- name: consumer2
  issuer: abc
  jwks: |
    {
      "keys": [
        {
          "kty": "RSA",
          "e": "AQAB",
          "use": "sig",
          "kid": "123",
          "alg": "RS256",
          "n": "i0B67f1jggT9QJlZ_8QL9QQ56LfurrqDhpuu8BxtVcfxrYmaXaCtqTn7OfCuca7cGHdrJIjq99rz890NmYFZuvhaZ-LMt2iyiSb9LZJAeJmHf7ecguXS_-4x3hvbsrgUDi9tlg7xxbqGYcrco3anmalAFxsbswtu2PAXLtTnUo6aYwZsWA6ksq4FL3-anPNL5oZUgIp3HGyhhLTLdlQcC83jzxbguOim-0OEz-N4fniTYRivK7MlibHKrJfO3xa_6whBS07HW4Ydc37ZN3Rx9Ov3ZyV0idFblU519nUdqp_inXj1eEpynlxH60Ys_aTU2POGZh_25KXGdF_ZC_MSRw"
        }
      ]
    }

Step 2: Configure route-level authorization

Apply to the route-a and route-b routes:

allow:
- consumer1

Apply to the *.example.com and test.com domains:

allow:
- consumer2

Result:

  • Requests to route-a or route-b are allowed only for consumer1.

  • Requests matching *.example.com or test.com are allowed only for consumer2.

  • All other authenticated consumers are denied with 403.

Test the configuration:

Pass the JWT in a URL parameter:

curl 'http://xxx.hello.com/test?access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjEy****.eyJpc3MiOiJhYmNkIiwic3ViIjoidGVzdCIsImlhdCI6MTY2NTY2MDUyNywiZXhwIjoxODY1NjczOD****.-vBSV0bKeDwQcuS6eeSZN9dLTUnSnZVk8eVCXdooCQ4'

Pass the JWT in an HTTP request header:

curl http://xxx.hello.com/test -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjEyMyJ9.eyJpc3MiOiJhYmNkIiwic3ViIjoidGVzdCIsImlhdCI6MTY2NTY2MDUyNywiZXhwIjoxODY1NjczODE5fQ.-vBSV0bKeDwQcuS6eeSZN9dLTUnSnZVk8eVCXdooCQ4'

After successful authentication and authorization, the X-Mse-Consumer header is set to consumer1 in the forwarded request.

Denied requests:

No JWT provided (returns 401):

curl http://xxx.hello.com/test

Consumer not in the allow list (returns 403). In this example, consumer1 is not in the allow list for *.example.com:

# consumer1 is not in the allow list of *.example.com
curl 'http://xxx.example.com/test' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjEyMyJ9.eyJpc3MiOiJhYmNkIiwic3ViIjoidGVzdCIsImlhdCI6MTY2NTY2MDUyNywiZXhwIjoxODY1NjczODE5fQ.-vBSV0bKeDwQcuS6eeSZN9dLTUnSnZVk8eVCXdooCQ4'

Instance-level authentication with route exceptions

Enable JWT authentication for all routes at the instance level and exempt a specific route.

Step 1: Enable global authentication

global_auth: true
consumers:
- name: consumer1
  issuer: abcd
  jwks: |
    {
      "keys": [
        {
          "kty": "oct",
          "kid": "123",
          "k": "hM0k3AbXBPpKOGg__Ql2Obcq7s60myWDpbHXzgKUQdYo7YCRp0gUqkCnbGSvZ2rGEl4YFkKqIqW7mTHdj-bcqXpNr-NOznEyMpVPOIlqG_NWVC3dydBgcsIZIdD-MR2AQceEaxriPA_VmiUCwfwL2Bhs6_i7eolXoY11EapLQtutz0BV6ZxQQ4dYUmct--7PLNb4BWJyQeWu0QfbIthnvhYllyl2dgeLTEJT58wzFz5HeNMNz8ohY5K0XaKAe5cepryqoXLhA-V-O1OjSG8lCNdKS09OY6O0fkyweKEtuDfien5tHHSsHXoAxYEHPFcSRL4bFPLZ0orTt1_4zpyfew",
          "alg": "HS256"
        }
      ]
    }
- name: consumer2
  issuer: abc
  jwks: |
    {
      "keys": [
        {
          "kty": "RSA",
          "e": "AQAB",
          "use": "sig",
          "kid": "123",
          "alg": "RS256",
          "n": "i0B67f1jggT9QJlZ_8QL9QQ56LfurrqDhpuu8BxtVcfxrYmaXaCtqTn7OfCuca7cGHdrJIjq99rz890NmYFZuvhaZ-LMt2iyiSb9LZJAeJmHf7ecguXS_-4x3hvbsrgUDi9tlg7xxbqGYcrco3anmalAFxsbswtu2PAXLtTnUo6aYwZsWA6ksq4FL3-anPNL5oZUgIp3HGyhhLTLdlQcC83jzxbguOim-0OEz-N4fniTYRivK7MlibHKrJfO3xa_6whBS07HW4Ydc37ZN3Rx9Ov3ZyV0idFblU519nUdqp_inXj1eEpynlxH60Ys_aTU2POGZh_25KXGdF_ZC_MSRw"
        }
      ]
    }

Step 2: Disable authentication for a specific route

Apply to route-b:

_disable_: true

Result:

  • route-b is accessible without a JWT.

  • All other routes require a valid JWT from either consumer1 or consumer2.

Domain-level authorization with route exceptions

Enable JWT authentication for a specific domain while exempting a specific route under that domain.

Step 1: Define consumers at the instance level

consumers:
- name: consumer1
  issuer: abcd
  jwks: |
    {
      "keys": [
        {
          "kty": "oct",
          "kid": "123",
          "k": "hM0k3AbXBPpKOGg__Ql2Obcq7s60myWDpbHXzgKUQdYo7YCRp0gUqkCnbGSvZ2rGEl4YFkKqIqW7mTHdj-bcqXpNr-NOznEyMpVPOIlqG_NWVC3dydBgcsIZIdD-MR2AQceEaxriPA_VmiUCwfwL2Bhs6_i7eolXoY11EapLQtutz0BV6ZxQQ4dYUmct--7PLNb4BWJyQeWu0QfbIthnvhYllyl2dgeLTEJT58wzFz5HeNMNz8ohY5K0XaKAe5cepryqoXLhA-V-O1OjSG8lCNdKS09OY6O0fkyweKEtuDfien5tHHSsHXoAxYEHPFcSRL4bFPLZ0orTt1_4zpyfew",
          "alg": "HS256"
        }
      ]
    }
- name: consumer2
  issuer: abc
  jwks: |
    {
      "keys": [
        {
          "kty": "RSA",
          "e": "AQAB",
          "use": "sig",
          "kid": "123",
          "alg": "RS256",
          "n": "i0B67f1jggT9QJlZ_8QL9QQ56LfurrqDhpuu8BxtVcfxrYmaXaCtqTn7OfCuca7cGHdrJIjq99rz890NmYFZuvhaZ-LMt2iyiSb9LZJAeJmHf7ecguXS_-4x3hvbsrgUDi9tlg7xxbqGYcrco3anmalAFxsbswtu2PAXLtTnUo6aYwZsWA6ksq4FL3-anPNL5oZUgIp3HGyhhLTLdlQcC83jzxbguOim-0OEz-N4fniTYRivK7MlibHKrJfO3xa_6whBS07HW4Ydc37ZN3Rx9Ov3ZyV0idFblU519nUdqp_inXj1eEpynlxH60Ys_aTU2POGZh_25KXGdF_ZC_MSRw"
        }
      ]
    }

Step 2: Disable authentication for a specific route

Apply to route-b:

_disable_: true

Step 3: Configure domain-level authorization

Apply to *.example.com:

allow:
- consumer1
- consumer2

Result:

  • route-b is accessible without a JWT, regardless of the domain.

  • Requests matching *.example.com (except route-b) are allowed only for consumer1 and consumer2.

Matching priority: Route-level rules take precedence over domain-level rules. When a request matches a route rule, the domain rule is skipped. This allows you to enforce authentication on a domain while exempting specific routes.

Token size limits

The total size of all HTTP request headers is capped at approximately 60 KB in most configurations. The exact limit depends on server and client settings.

Important

Keep the total size of JWT tokens well within the header size limit to prevent request failures.

HTTP status codes

HTTP status codeError messageCause
401Jwt missingNo JWT in the request
401Jwt expiredThe JWT has expired
401Jwt verification failsPayload validation failed, for example, the iss claim does not match
403Access DeniedThe consumer is not authorized to access the matched route or domain