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).
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:
Define one or more consumers at the instance level, each with a name and a JWKS for signature verification.
(Optional) Restrict access to specific routes or domain names by adding an allow list that references consumer names.
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.
If validation succeeds, the request is forwarded to the backend service with the
X-Mse-Consumerheader set to the matched consumer name. If validation fails, the plug-in returns401. If the consumer is not authorized for the matched route or domain, the plug-in returns403.
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.
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
consumers | array of object | Yes | - | List of consumers. See Consumer fields |
global_auth | array of string | No (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:
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
name | string | Yes | - | Unique consumer name |
jwks | string | No | - | 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_jwks | object | No | {"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 |
issuer | string | No | - | Expected JWT issuer. The plug-in verifies that this value matches the iss claim in the token payload |
claims | object | No | - | 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_headers | array of object | No | - | Forward specific JWT claims as request headers to the backend service. See claims_to_headers fields |
from_headers | array of object | No | [{"name":"Authorization","value_prefix":"Bearer"}] | Request headers from which to extract the JWT. See from_headers fields |
from_params | array of string | No | access_token | URL parameters from which to extract the JWT |
from_cookies | array of string | No | - | Cookies from which to extract the JWT |
clock_skew_seconds | number | No | 60 | Allowed clock skew (in seconds) when validating the exp and iat claims |
keep_token | bool | No | true | Whether to retain the JWT in the request when forwarding to the backend service |
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
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
name | string | Yes | - | Name of the request header containing the JWT |
value_prefix | string | Yes | - | 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
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
claim | string | Yes | - | The claim name in the JWT payload. The value must be a string or an unsigned integer |
header | string | Yes | - | The request header name to set with the extracted claim value |
override | bool | No | true | true: 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.
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
uri | string | Yes | - | URI to fetch the JWKS from |
service | string | Yes | - | 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) |
port | bool | Yes | - | Service port |
timeout | bool | No | 3000 | Request timeout in milliseconds |
ttl | bool | No | 30000 | Cache 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.
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
allow | array of string | No (required for route- or domain-level configurations) | - | Consumer names that are permitted to access the route or domain |
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_: trueWhen _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:
- consumer1Apply to the *.example.com and test.com domains:
allow:
- consumer2Result:
Requests to
route-aorroute-bare allowed only forconsumer1.Requests matching
*.example.comortest.comare allowed only forconsumer2.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/testConsumer 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_: trueResult:
route-bis accessible without a JWT.All other routes require a valid JWT from either
consumer1orconsumer2.
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_: trueStep 3: Configure domain-level authorization
Apply to *.example.com:
allow:
- consumer1
- consumer2Result:
route-bis accessible without a JWT, regardless of the domain.Requests matching
*.example.com(exceptroute-b) are allowed only forconsumer1andconsumer2.
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.
Keep the total size of JWT tokens well within the header size limit to prevent request failures.
HTTP status codes
| HTTP status code | Error message | Cause |
|---|---|---|
401 | Jwt missing | No JWT in the request |
401 | Jwt expired | The JWT has expired |
401 | Jwt verification fails | Payload validation failed, for example, the iss claim does not match |
403 | Access Denied | The consumer is not authorized to access the matched route or domain |