jwt-auth

描述

jwt-auth 插件用于将 JWT 身份验证添加到 ServiceRoute 中。

通过 Consumer 将其密匙添加到查询字符串参数、请求头或 cookie 中用来验证其请求。

属性

Consumer 端:

名称类型必选项默认值有效值描述
keystringConsumer 的 access_key 必须是唯一的。如果不同 Consumer 使用了相同的 access_key ,将会出现请求匹配异常。
secretstring加密秘钥。如果未指定,后台将会自动生成。该字段支持使用 APISIX Secret 资源,将值保存在 Secret Manager 中。
public_keystringRSA 或 ECDSA 公钥, algorithm 属性选择 RS256ES256 算法时必选。该字段支持使用 APISIX Secret 资源,将值保存在 Secret Manager 中。
private_keystringRSA 或 ECDSA 私钥, algorithm 属性选择 RS256ES256 算法时必选。该字段支持使用 APISIX Secret 资源,将值保存在 Secret Manager 中。
algorithmstring“HS256”[“HS256”, “HS512”, “RS256”, “ES256”]加密算法。
expinteger86400[1,…]token 的超时时间。
base64_secretbooleanfalse当设置为 true 时,密钥为 base64 编码。
lifetime_grace_periodinteger0[0,…]定义生成 JWT 的服务器和验证 JWT 的服务器之间的时钟偏移。该值应该是零(0)或一个正整数。

注意:schema 中还定义了 encrypt_fields = {"secret", "private_key"},这意味着该字段将会被加密存储在 etcd 中。具体参考 加密存储字段

Route 端:

名称类型必选项默认值描述
headerstringauthorization设置我们从哪个 header 获取 token。
querystringjwt设置我们从哪个 query string 获取 token,优先级低于 header。
cookiestringjwt设置我们从哪个 cookie 获取 token,优先级低于 query。
hide_credentialsbooleanfalse该参数设置为 true 时,则不会将含有认证信息的 header\query\cookie 传递给 Upstream。

您可以使用 HashiCorp Vault 实施 jwt-auth,以从其加密的 KV 引擎 使用 APISIX Secret 资源。

接口

该插件会增加 /apisix/plugin/jwt/sign 接口。

jwt-auth - 图1note

你需要通过 public-api 插件来暴露它。

启用插件

如果想要启用插件,就必须使用 JWT token 创建一个 Consumer 对象,并将 Route 配置为使用 JWT 身份验证。

首先,你可以通过 Admin API 创建一个 Consumer:

  1. curl http://127.0.0.1:9180/apisix/admin/consumers \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "username": "jack",
  5. "plugins": {
  6. "jwt-auth": {
  7. "key": "user-key",
  8. "secret": "my-secret-key"
  9. }
  10. }
  11. }'
jwt-auth - 图2note

jwt-auth 默认使用 HS256 算法,如果使用 RS256 算法,需要指定算法,并配置公钥与私钥,示例如下:

  1. curl http://127.0.0.1:9180/apisix/admin/consumers \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "username": "kerouac",
  5. "plugins": {
  6. "jwt-auth": {
  7. "key": "user-key",
  8. "public_key": "-----BEGIN PUBLIC KEY-----\n……\n-----END PUBLIC KEY-----",
  9. "private_key": "-----BEGIN RSA PRIVATE KEY-----\n……\n-----END RSA PRIVATE KEY-----",
  10. "algorithm": "RS256"
  11. }
  12. }
  13. }'

创建 Consumer 对象后,你可以创建 Route 进行验证:

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "methods": ["GET"],
  5. "uri": "/index.html",
  6. "plugins": {
  7. "jwt-auth": {}
  8. },
  9. "upstream": {
  10. "type": "roundrobin",
  11. "nodes": {
  12. "127.0.0.1:1980": 1
  13. }
  14. }
  15. }'

测试插件

首先,你需要为签发 token 的 API 配置一个 Route,该路由将使用 public-api 插件。

  1. curl http://127.0.0.1:9180/apisix/admin/routes/jas \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "uri": "/apisix/plugin/jwt/sign",
  5. "plugins": {
  6. "public-api": {}
  7. }
  8. }'

之后就可以通过调用它来获取 token 了。

  • 没有额外的 payload:
  1. curl http://127.0.0.1:9080/apisix/plugin/jwt/sign?key=user-key -i
  1. HTTP/1.1 200 OK
  2. Date: Wed, 24 Jul 2019 10:33:31 GMT
  3. Content-Type: text/plain
  4. Transfer-Encoding: chunked
  5. Connection: keep-alive
  6. Server: APISIX web server
  7. eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXkiOiJ1c2VyLWtleSIsImV4cCI6MTU2NDA1MDgxMXx.Us8zh_4VjJXF-TmR5f8cif8mBU7SuefPlpxhH0jbPVI
  • 有额外的 payload:
  1. curl -G --data-urlencode 'payload={"uid":10000,"uname":"test"}' http://127.0.0.1:9080/apisix/plugin/jwt/sign?key=user-key -i
  1. HTTP/1.1 200 OK
  2. Date: Wed, 21 Apr 2021 06:43:59 GMT
  3. Content-Type: text/plain; charset=utf-8
  4. Transfer-Encoding: chunked
  5. Connection: keep-alive
  6. Server: APISIX/2.4
  7. eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1bmFtZSI6InRlc3QiLCJ1aWQiOjEwMDAwLCJrZXkiOiJ1c2VyLWtleSIsImV4cCI6MTYxOTA3MzgzOX0.jI9-Rpz1gc3u8Y6lZy8I43RXyCu0nSHANCvfn0YZUCY

现在你可以使用获取到的 token 进行请求尝试

  • token 放到请求头中:
  1. curl http://127.0.0.1:9080/index.html \-H 'Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXkiOiJ1c2VyLWtleSIsImV4cCI6MTU2NDA1MDgxMX0.Us8zh_4VjJXF-TmR5f8cif8mBU7SuefPlpxhH0jbPVI' -i
  1. HTTP/1.1 200 OK
  2. Content-Type: text/html
  3. Content-Length: 13175
  4. ...
  5. Accept-Ranges: bytes
  6. <!DOCTYPE html>
  7. <html lang="cn">
  8. ...
  • 缺少 token
  1. curl http://127.0.0.1:9080/index.html -i
  1. HTTP/1.1 401 Unauthorized
  2. ...
  3. {"message":"Missing JWT token in request"}
  • token 放到请求参数中:
  1. curl http://127.0.0.1:9080/index.html?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXkiOiJ1c2VyLWtleSIsImV4cCI6MTU2NDA1MDgxMX0.Us8zh_4VjJXF-TmR5f8cif8mBU7SuefPlpxhH0jbPVI -i
  1. HTTP/1.1 200 OK
  2. Content-Type: text/html
  3. Content-Length: 13175
  4. ...
  5. Accept-Ranges: bytes
  6. <!DOCTYPE html>
  7. <html lang="cn">
  8. ...
  • token 放到 cookie 中:
  1. curl http://127.0.0.1:9080/index.html --cookie jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXkiOiJ1c2VyLWtleSIsImV4cCI6MTU2NDA1MDgxMX0.Us8zh_4VjJXF-TmR5f8cif8mBU7SuefPlpxhH0jbPVI -i
  1. HTTP/1.1 200 OK
  2. Content-Type: text/html
  3. Content-Length: 13175
  4. ...
  5. Accept-Ranges: bytes
  6. <!DOCTYPE html>
  7. <html lang="cn">
  8. ...

禁用插件

当你需要禁用 jwt-auth 插件时,可以通过以下命令删除相应的 JSON 配置,APISIX 将会自动重新加载相关配置,无需重启服务:

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "methods": ["GET"],
  5. "uri": "/index.html",
  6. "id": 1,
  7. "plugins": {},
  8. "upstream": {
  9. "type": "roundrobin",
  10. "nodes": {
  11. "127.0.0.1:1980": 1
  12. }
  13. }
  14. }'