我使用声明性配置而不是数据库来配置Kong。一切正常,直到我尝试在/etc/kong/kong.yml中添加JWT身份验证配置。
声明性配置(/etc/kong/kong.yml):
_format_version: "1.1"
services:
- name: serverinfo
url: http://profile.facebook.service.com:9090
# Entities can store tags as metadata
tags:
- serverinfo
# Entities that have a foreign-key relationship can be nested:
routes:
- name: serverinfo-route
paths:
- /serverinfo
plugins:
- name: http-log
config:
http_endpoint: http://localhost:9900/log
method: POST
timeout: 1000
keepalive: 1000
- name: certs
url: http://localhost:68766
tags:
- certs
routes:
- name: certs-route
paths:
- /kong/answers
plugins:
- name: jwt
config:
consumers:
- username: jessie_pinkman
jwt_secrets:
- key: walter_white
- secret: youwannacook?
routes:
- name: serverinfo-route
# Relationships can also be specified between top-level entities,
# either by name or by id
service: serverinfo
hosts: ["profile.facebook.service.com"]
- name: certs-route
# Relationships can also be specified between top-level entities,
# either by name or by id
service: certs
hosts: ["profile.stackoverflow.com"]
plugins:
- name: http-log
service: serverinfo
config:
http_endpoint: http://localhost:9900/log
method: POST
timeout: 1000
keepalive: 1000
- name: jwt
service: certs
consumer: jessie_pinkman
config:
jwt_secrets:
- key: walter_white
secret: youwannacook?
algorithm: HS256
重装金刚:
http :8001/config config=@kong.yml
访问服务:
curl -X POST \
http://127.0.0.1:8000/kong/answers \
-H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJpc3MiOiJ3YWx0ZXJfd2hpdGUifQ.4oidCCnrHcfYkbwC0lvCMAKnxiht07cUzI1oOxWAk78' \
-H 'Content-Type: application/json' \
-H 'Host: profile.stackoverflow.com' \
-H 'username: jessie_pinkman' \
-d '{"id":1}'
/usr/local/kong/logs/error.log中的错误:
[error] 1097#0: *23738 lua coroutine: runtime error: /usr/local/share/lua/5.1/kong/plugins/jwt/handler.lua:209: attempt to index field 'consumer' (a nil value)
stack traceback:
coroutine 0:
/usr/local/share/lua/5.1/kong/plugins/jwt/handler.lua: in function 'do_authentication'
/usr/local/share/lua/5.1/kong/plugins/jwt/handler.lua:244: in function </usr/local/share/lua/5.1/kong/plugins/jwt/handler.lua:232>
coroutine 1:
[C]: in function 'resume'
coroutine.wrap:21: in function <coroutine.wrap:21>
/usr/local/share/lua/5.1/kong/init.lua:663: in function 'access'
access_by_lua(nginx-kong.conf:102):2: in function <access_by_lua(nginx-kong.conf:102):1>, client: 127.0.0.1, server: kong, request: "POST /kong/answers HTTP/1.1", host: "profile.stackoverflow.com"
2019/08/05 20:21:48 [error] 1097#0: *23738 [kong] init.lua:669 /usr/local/share/lua/5.1/kong/plugins/jwt/handler.lua:209: attempt to index field 'consumer' (a nil value), client: 127.0.0.1, server: kong, request: "POST /kong/answers HTTP/1.1", host: "profile.stackoverflow.com"