当前位置: 首页 > wzjs >正文

国务院建设主管部门网站哈尔滨百度公司地址

国务院建设主管部门网站,哈尔滨百度公司地址,河南做网站团队,响应式网站技术在 API 网关中,身份认证 是保障接口安全的重要手段之一。Kong 作为主流 API 网关之一,提供了多种认证方式,其中 API Key Authentication 适用于 轻量级认证 场景。本文将详细介绍如何在 Kong 中启用 API Key 认证 并进行测试。 目录 1.1.1、…

在 API 网关中,身份认证 是保障接口安全的重要手段之一。Kong 作为主流 API 网关之一,提供了多种认证方式,其中 API Key Authentication 适用于 轻量级认证 场景。本文将详细介绍如何在 Kong 中启用 API Key 认证 并进行测试。

目录

      • 1.1.1、环境准备
        • 1.1.1.1、创建一个服务,key-auth-demo
        • 1.1.1.2、创建对应的路由,key-auth-demo
        • 1.1.1.3、测试环境
      • 1.1.2、插件安装
        • 1.1.2.1、在服务范围内安装
        • 1.1.2.2、 在route范围内安装
        • 1.1.2.3、 全局范围内安装
      • 1.1.2、测试
      • 1.1.3、环境清理
        • 1.1.3.1、清除服务范围内的插件
        • 1.1.3.2、清除route范围内的插件
        • 1.1.3.2、清除全局范围内的插件
      • 1.1.4、总结

1.1.1、环境准备

1.1.1.1、创建一个服务,key-auth-demo
curl -i -s -X POST http://localhost:8001/services \--data name=key-auth-demo \--data url='http://localhost:8080'

http://localhost:8080 端口是运行一个Go的服务,请求/hello将返回对应的数据。

将会返回数据:

Hello, kong,I'm runing at 8080!
1.1.1.2、创建对应的路由,key-auth-demo
curl -i -X POST http://localhost:8001/services/key-auth-demo/routes \--data 'paths[]=/key-auth-demo' \--data name=key-auth-demo
1.1.1.3、测试环境
[root@iZbp1ivu3yaedumdy0va2vZ kong]# curl http://localhost:8000/key-auth-demo/hello
Hello, kong,I'm runing at 8080!

看到如下输出,证明已经环境已经搭建OK。

1.1.2、插件安装

插件的相关文档为

https://docs.konghq.com/hub/kong-inc/key-auth/configuration/

1.1.2.1、在服务范围内安装
curl -X POST http://localhost:8001/services/{serviceName|Id}/plugins \--header "accept: application/json" \--header "Content-Type: application/json" \--data '{"name": "key-auth","config": {"key_names": ["apikey"]}
}示例:
curl -X POST http://localhost:8001/services/key-auth-demo/plugins \--header "accept: application/json" \--header "Content-Type: application/json" \--data '{"name": "key-auth","config": {"key_names": ["apikey"]}
}'

{serviceName|Id} : 这里用要对哪个服务开启

name: 代表已启动的插件名

config: 插件对应的配置信息

返回的结果如下:

{"consumer": null,"config": {"hide_credentials": false,"key_in_header": true,"key_in_query": true,"key_in_body": false,"run_on_preflight": true,"key_names": ["apikey"],"realm": null,"anonymous": null},"name": "key-auth","instance_name": null,"protocols": ["grpc","grpcs","http","https"],"created_at": 1739512641,"updated_at": 1739512641,"enabled": true,"service": {"id": "ae557210-8f1f-415b-8549-3c973e495881"},"id": "8b8d48db-3a15-4a94-ba81-93a1f350702d","route": null,"tags": null
}
1.1.2.2、 在route范围内安装
curl -X POST http://localhost:8001/routes/{route_id_or_name}/plugins \--header "accept: application/json" \--header "Content-Type: application/json" \--data '{"name": "key-auth","config": {"key_names": ["apikey"]}
}'
实例
curl -X POST http://localhost:8001/routes/key-auth-demo/plugins \--header "accept: application/json" \--header "Content-Type: application/json" \--data '{"name": "key-auth","config": {"key_names": ["apikey"]}
}'
1.1.2.3、 全局范围内安装
curl -X POST http://localhost:8001/plugins \--header "accept: application/json" \--header "Content-Type: application/json" \--data '{"name":"key-auth","enabled":true,"protocols":["grpc","grpcs","http","https"],"config":{"hide_credentials":false,"key_in_body":false,"key_in_header":true,"key_in_query":true,"key_names":["apikey"],"run_on_preflight":true}}'

1.1.2、测试

开启了kye-auth插件后,如果还是正常访问,将会返回401

[root@iZbp1ivu3yaedumdy0va2vZ ~]# curl -i  http://localhost:8000/key-auth-demo/hello
HTTP/1.1 401 Unauthorized
Date: Fri, 14 Feb 2025 06:04:46 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
WWW-Authenticate: Key
Content-Length: 96
X-Kong-Response-Latency: 1
Server: kong/3.9.0
X-Kong-Request-Id: 3d24b81b896b1b3e6bcc626177e8ea3b{"message":"No API key found in request","request_id":"3d24b81b896b1b3e6bcc626177e8ea3b"
}

那么应该如何访问呢?

  • 创建一个用户

    curl -X POST http://localhost:8001/consumers -d username=key-auth-user
    
  • 给这个用户分配一个apiKey

    curl -X POST http://localhost:8001/consumers/key-auth-user/key-auth \-d key=api_key_78954455
    

在请求头中携带apikey,或者在请求参数中携带apikey,如下所示:

[root@iZbp1ivu3yaedumdy0va2vZ ~]# curl -i -H "apikey:api_key_78954455" http://localhost:8000/key-auth-demo/hello
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Content-Length: 31
Connection: keep-alive
Date: Fri, 14 Feb 2025 06:10:22 GMT
Server: kong/3.9.0
X-Kong-Upstream-Latency: 0
X-Kong-Proxy-Latency: 3
Via: 1.1 kong/3.9.0
X-Kong-Request-Id: e9e4eb0de1646867fcb3b810ef8d9570Hello, kong,I'm runing at 8080!curl -i  http://localhost:8000/key-auth-demo/hello?apikey=api_key_78954455
[root@iZbp1ivu3yaedumdy0va2vZ ~]# curl -i  http://localhost:8000/key-auth-demo/hello?apikey=api_key_78954455
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Content-Length: 31
Connection: keep-alive
Date: Fri, 14 Feb 2025 06:13:40 GMT
Server: kong/3.9.0
X-Kong-Upstream-Latency: 1
X-Kong-Proxy-Latency: 1
Via: 1.1 kong/3.9.0
X-Kong-Request-Id: cbfd604c2e274833e2baa77dfe322938Hello, kong,I'm runing at 8080!

1.1.3、环境清理

1.1.3.1、清除服务范围内的插件
  • 获取服务范围内的插件
curl -X GET http://localhost:8001/services/key-auth-demo/plugins{"data": [{// ..."id": "8b8d48db-3a15-4a94-ba81-93a1f350702d",// ...}],"next": null
}

这里我我记录想要删除id是啥,到下一步请求的时候需要使用。

  • 删除指定的插件

    curl -X DELETE http://localhost:8001/services/key-auth-demo/plugins/8b8d48db-3a15-4a94-ba81-93a1f350702d
    

    身份认证插件清除后,不适用apiKey也能进行访问了。

1.1.3.2、清除route范围内的插件
  • 获取route范围内的插件

    curl -X GET http://localhost:8001/routes/key-auth-demo/plugins{"data": [{// ..."id": "22245c50-f2d1-42c8-b4aa-090414e763d3",// ...}],"next": null
    }
    
  • 删除指定的插件

    curl -X DELETE http://localhost:8001/routes/key-auth-demo/plugins/22245c50-f2d1-42c8-b4aa-090414e763d3
    
1.1.3.2、清除全局范围内的插件
  • 获取全局范围内的插件
curl -X GET http://localhost:8001/plugins
返回结果,省略部分数据
{"data": [{"id": "5ebe8f78-41b0-4ae6-a21c-4e69f97d419c",}],"next": null
}
  • 删除指定的插件
curl -X DELETE http://localhost:8001/plugins/5ebe8f78-41b0-4ae6-a21c-4e69f97d419c

1.1.4、总结

基于 API Key Authentication 的插件非常适合需要快速、轻量认证的场景,尤其在以下情况下表现尤为出色:

  • 提供开放 API 给开发者或第三方系统。
  • 简单的客户端认证,尤其在移动应用和 Web 应用中。
  • 管理和限制不同用户、客户端或服务的访问权限。
  • 配合流量控制插件(如 Rate Limiting)使用,避免过度请求。

虽然 API 密钥认证简单易用,但它的安全性相对较低,尤其是在传输过程中密钥可能会被泄露。

因此,常常配合 HTTPS 使用,以保证通信的安全。

http://www.dtcms.com/wzjs/9449.html

相关文章:

  • 济南网站建设套餐网络营销的目的和意义
  • 无聊网站建设产品软文范例大全
  • 做瞹瞹视频电影邪恶网站seo搜索引擎优化是什么
  • 村级网站建站广州网站快速排名
  • wordpress主题cute网络推广seo
  • wordpress微信关注查看朝阳网站seo
  • 帝国cms网站迁移热门推广平台
  • 邵阳做网站的有哪些域名解析网站
  • 东莞手机网站制作引流客户的最快方法是什么
  • 化工课设代做网站论文收录网站排名
  • 企业所得税税率2022年最新税率表谷歌seo搜索引擎优化
  • 西宁市网站建设价格app推广软件
  • 大数据在营销中的应用优化关键词的方法有哪些
  • 深圳罗湖高端网站建设怎么建网站教程
  • 罗田企业网站建设网站安全
  • 用dw做网站的视频站长工具seo词语排名
  • 我做推广找不到我的网站百度推广客户端下载安装
  • 中日韩精品电影推荐网站微软优化大师
  • 优秀的网页设计网站google搜索引擎免费入口
  • 专门做牛肉的网站阿里巴巴怎么优化关键词排名
  • 建外贸网站推广如何制作微信小程序
  • 深圳做网站推广公司互联网营销模式有哪些
  • 装修平台哪个最好正规seo关键词排名哪家专业
  • web网站建设方案国内最新的新闻
  • 政府网站建设概况杭州百度百科
  • 拱墅网站建设黄页网站推广公司
  • 用php做的网站百度手机怎么刷排名多少钱
  • 酷播wordpressseo推广哪家服务好
  • 网站建设合同范本下载比百度好用的搜索软件手机版
  • 台州市建站公司宁波网站推广优化