SylviaBABY commented on code in PR #7022: URL: https://github.com/apache/apisix/pull/7022#discussion_r870012210
########## docs/zh/latest/getting-started.md: ########## @@ -210,114 +255,34 @@ curl "http://127.0.0.1:9080/apisix/admin/routes/1" -H "X-API-KEY: edd1c9f034335f }' ``` -## 第三步:验证 - -我们已创建了路由与上游服务,并将它们进行了绑定。现在让我们访问 Apache APISIX 来测试这条路由: +我们已经创建了路由与上游服务,现在可以通过以下命令访问上游服务: ```bash curl -i -X GET "http://127.0.0.1:9080/get?foo1=bar1&foo2=bar2" -H "Host: httpbin.org" ``` -它从我们的上游服务(实际是 `httpbin.org`)返回数据,并且结果符合预期。 - -## 进阶操作 - -本节提供了 Apache APISIX 的一些进阶操作技巧,包括:添加身份验证、为路由添加前缀、使用 APISIX Dashboard 以及常见问题排查。 - -### 添加身份验证 - -我们在第二步中创建的路由是公共的,只要知道 Apache APISIX 对外暴露的地址,**任何人** 都可以访问这个上游服务,这种访问方式没有保护措施,存在一定的安全隐患。在实际应用场景中,我们需要为路由添加身份验证。 - -现在我们希望只有特定的用户 `John` 可以访问这个上游服务,需要使用 [消费者(Consumer)](./terminology/consumer.md) 和 [插件(Plugin)](./terminology/plugin.md) 来实现身份验证。 - -首先,让我们用 [key-auth](./plugins/key-auth.md) 插件创建一个 [消费者(Consumer)](./terminology/consumer.md) `John`,我们需要提供一个指定的密钥: - -```bash -curl "http://127.0.0.1:9080/apisix/admin/consumers" -H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" -X PUT -d ' -{ - "username": "john", - "plugins": { - "key-auth": { - "key": "key-of-john" - } - } -}' -``` - -接下来,让我们绑定 `消费者(John)` 到路由上,我们只需要为路由 **启用** [key-auth](./plugins/key-auth.md) 插件即可。 - -```bash -curl "http://127.0.0.1:9080/apisix/admin/routes/1" -H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" -X PUT -d ' -{ - "uri": "/get", - "host": "httpbin.org", - "plugins": { - "key-auth": {} - }, - "upstream_id": "1" -}' -``` - -现在当我们访问第二步创建的路由时,会触发 **Unauthorized Error(未经授权的错误)**。 - -访问那个路由的正确方式是添加一个带有正确密钥的名为 `apikey` 的 `Header`,如下方代码所示。 - -```bash -curl -i -X GET http://127.0.0.1:9080/get -H "Host: httpbin.org" -H "apikey: key-of-john" -``` - -### 为路由添加前缀 - -现在,假设您要向路由添加前缀(例如:samplePrefix),并且不想使用 `host` 头, 则可以使用 `proxy-rewrite` 插件来完成。 - -```bash -curl "http://127.0.0.1:9080/apisix/admin/routes/1" -H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" -X PUT -d ' -{ - "uri": "/samplePrefix/get", - "plugins": { - "proxy-rewrite": { - "regex_uri": ["^/samplePrefix/get(.*)", "/get$1"] - }, - "key-auth": {} - }, - "upstream_id": "1" -}' -``` +该请求将被 APISIX 转发到 `http://httpbin.org:80/anything/foo?arg=10`。 -现在您可以使用以下命令来调用路由: +## APISIX Dashboard Review Comment: ```suggestion ## 使用 APISIX Dashboard ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
