1、创建服务
curl -i -X POST --url http://localhost:8001/services/ --data ‘name=example-service‘ --data ‘url=http://mockbin.org‘
url--对应services 表 protocol
-host-port(http---mockbin.org
)
$ curl -i -X POST --url http://localhost:8001/services/baidu-service/routes --data ‘hosts[]=t1.com‘ --data ‘hosts[]=t2.com‘
Issue the following cURL request to verify that Kong is properly forwarding requests to your Service. Note that by default Kong handles proxy requests on port :8000
:
$ curl -i -X GET --url http://localhost:8000/ --header ‘Host: example.com‘
4、给服务添加启用插件
请求访问时,会检查:
To configure the key-auth plugin for the Service you configured in Kong, issue the following cURL request:
A、为服务启用插件
$ curl -i -X POST --url http://localhost:8001/services/baidu-service/plugins/ --data ‘name=key-auth‘
当系统初始化时,读表plugins(读取插件配置)---会为每个服务加载插件列表,当请求来临时,调用插件的拦截方法,传递conf---
插件内会去从请求中找出访问key,然后差找访问凭证,进而查询出消费者,把消费者信息,写到后续请求头中
测试访问,返回
curl -i -X GET --url http://t1.com:8000 --header ‘HOST:t1.com‘
HTTP/1.1 401 Unauthorized
Date: Sun, 29 Mar 2020 13:02:57 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
WWW-Authenticate: Key realm="kong"
Content-Length: 41
X-Kong-Response-Latency: 1
Server: kong/2.0.2
{"message":"No API key found in request"}
B、建立访问者身份凭证
//创建消费者----增加应用的用户
curl -i -X POST --url http://localhost:8001/consumers/ --data "username=Jason"
//创建消费者访问凭证----keyauth_credentials--增加记录
curl -i -X POST \
--url http://localhost:8001/consumers/Jason/key-auth/ --data ‘key=123456‘
//验证访问
curl -i -X GET --url http://t1.com:8000 --header ‘HOST:t1.com‘ --header ‘apikey:123456‘
原文:https://www.cnblogs.com/justart/p/12595428.html