key-auth

    名字

    添加 Key Authentication 到一个 serviceroute。 然后,consumer 将其密钥添加到查询字符串参数或标头中以验证其请求。

    consumer 端配置:

    名称类型必选项默认值有效值描述
    headerstring可选apikey设置我们从哪个 header 获取 key。
    querystring可选apikey设置我们从哪个 querystring 获取 key,优先级低于header

    如何启用

    1. 创建一个 consumer 对象,并设置插件 key-auth 的值。

    你可以使用浏览器打开 dashboard:http://127.0.0.1:9080/apisix/dashboard/,通过 web 界面来完成上面的操作,先增加一个 consumer:

    然后在 consumer 页面中添加 key-auth 插件: key-auth - 图2

    1. 创建 route 或 service 对象,并开启 key-auth 插件。
    1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
    2. "methods": ["GET"],
    3. "uri": "/index.html",
    4. "id": 1,
    5. "plugins": {
    6. },
    7. "upstream": {
    8. "type": "roundrobin",
    9. "nodes": {
    10. "39.97.63.215:80": 1
    11. }
    12. }
    13. }'

    下面是一个正常通过 key-auth 验证的请求:

    1. $ curl http://127.0.0.2:9080/index.html -H 'apikey: auth-one' -i
    2. ...

    如果当前请求没有正确设置 apikey ,将得到一个 401 的应答。

    禁用插件

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

    现在就已经移除了该插件配置,其他插件的开启和移除也是同样的方法。