Module ngx_http_auth_jwt_module

ngx_http_auth_jwt_module模块(1.11.3)通过使用指定的密钥验证提供的JSON Web 令牌(JWT)来实现客户端授权。 JWT 声明必须以JSON Web 签名(JWS)结构进行编码。该模块可用于OpenID Connect身份验证。

该模块可以通过satisfy指令与其他访问模块(例如ngx_http_access_modulengx_http_auth_basic_modulengx_http_auth_request_module)组合。

该模块支持以下加密algorithms

  • HS256,HS384,HS512

  • RS256,RS384,RS512

  • ES256,ES384,ES512

  • EdDSA(Ed25519 和 Ed448 签名)(1.15.7)

在 1.13.7 版之前,仅支持 HS256,RS256,ES256 算法。

Note

此模块可作为commercial subscription的一部分使用。

Example Configuration

location / {
    auth_jwt          "closed site";
    auth_jwt_key_file conf/keys.json;
}

Directives

Syntax:auth_jwt string [token=$variable] | off;
Default:auth_jwt off;
Context:http , server , location , limit_except

启用 JSON Web 令牌验证。指定的string用作realm。参数值可以包含变量。

可选的token参数指定一个包含 JSON Web 令牌的变量。默认情况下,JWT 以Bearer Token的形式在“ Authorization”Headers 中传递。 JWT 也可以作为 cookie 或查询字符串的一部分传递:

auth_jwt "closed site" token=$cookie_auth_token;

特殊值off取消了从先前配置级别继承的auth_jwt指令的效果。

Syntax:auth_jwt_claim_set $variable name ...;
Default:
Context:http

该指令出现在 1.11.10 版本中。

variable设置为由键名标识的 JWT 声明参数。名称匹配从 JSON 树的顶层开始。对于数组,该变量保留以逗号分隔的数组元素列表。

auth_jwt_claim_set $email info e-mail;
auth_jwt_claim_set $job info "job title";

Note

在 1.13.7 版之前,只能指定一个键名,并且数组的结果不确定。

Syntax:auth_jwt_header_set $variable name ...;
Default:
Context:http

该指令出现在 1.11.10 版本中。

variable设置为由键名标识的 JOSEHeaders 参数。名称匹配从 JSON 树的顶层开始。对于数组,该变量保留以逗号分隔的数组元素列表。

Note

在 1.13.7 版之前,只能指定一个键名,并且数组的结果不确定。

Syntax:auth_jwt_key_file file;
Default:
Context:http , server , location , limit_except

JSON Web 密钥集格式指定file以验证 JWT 签名。参数值可以包含变量。

Syntax:auth_jwt_key_request uri;
Default:
Context:http , server , location , limit_except

该指令出现在 1.15.6 版中。

允许从子请求中检索JSON Web 密钥集文件以验证 JWT 签名,并设置将子请求发送到的 URI。为避免验证开销,建议缓存密钥文件:

proxy_cache_path /data/nginx/cache levels=1 keys_zone=foo:10m;

server {
    ...

    location / {
        auth_jwt             "closed site";
        auth_jwt_key_request /jwks_uri;
    }

    location = /jwks_uri {
        internal;
        proxy_cache foo;
        proxy_pass  http://idp.example.com/keys;
    }
}

Syntax:auth_jwt_leeway time;
Default:auth_jwt_leeway 0s;
Context:http , server , location

该指令出现在版本 1.13.10 中。

设置最大允许余量,以在验证expnbf JWT 声明时补偿时钟偏斜。

Embedded Variables

ngx_http_auth_jwt_module模块支持嵌入式变量:

$jwt_header_ name

$jwt_claim_ name

对于嵌套的声明和包含点(“.”)的声明,无法评估变量的值;应该使用auth_jwt_claim_set指令。