业务介绍
在下面的步骤中,我们将配置key-auth插件启动身份验证。 在添加此插件之前,所有请求都将在上游被代理。 添加并配置此插件后,只会代理具有正确密钥的请求,从而保护您的上游服务免遭未经授权的使用。
配置key-auth插件
$ curl -i -X POST \
--url http://localhost:8001/services/example-service/plugins/ \
--data 'name=key-auth'
应答:
HTTP/1.1 201 Created
Date: Mon, 20 Apr 2020 03:51:52 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/2.0.3
Content-Length: 363
X-Kong-Admin-Latency: 196
{
"created_at":1587354712,
"config":{
"key_names":["apikey"],
"run_on_preflight":true,
"anonymous":null,
"hide_credentials":false,
"key_in_body":false
},
"id":"b009a5cf-d102-4799-b9f6-9633c9adae67",
"service":{"id":"7bed6af7-c6d0-4228-a2b0-e0103d17aa0a"},
"enabled":true,
"protocols":["grpc","grpcs","http","https"],
"name":"key-auth",
"consumer":null,
"route":null,
"tags":null
}
确认插件已经生效
$ curl -i -X GET \
--url http://localhost:8000/ \
--header 'Host: example.com'
应答
HTTP/1.1 401 Unauthorized
Date: Mon, 20 Apr 2020 03:53:35 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
WWW-Authenticate: Key realm="kong"
Content-Length: 41
X-Kong-Response-Latency: 7
Server: kong/2.0.3
{"message":"No API key found in request"}