Kong Gateway - 12 基于网关服务的跨源资源共享(CORS)

如果在前面11篇Kong Gateway系列的文章中,你亲自动手实验过用浏览器访问以下地址:
http://localhost:8000/v1/books
你将无法获得书籍接口返回的书籍记录,
本篇blog能让你在浏览器中用8000端口或者8443端口能直接访问书籍的Restful API 接口返回的数据
增删改都可以,本篇为了简单,没配置验证,也没有使用访问控制列表功能,
我们可以用Postman去模拟浏览器 在前面11篇文章中,只要加上以下4条命令中最后两条命令,

浏览器就能正常访问书籍的Restful API 接口

用Kong配置一个book服务
在安装并启动Kong之后,使用Kong的管理API端口8001添加一个名称为book的服务
[root@contoso ~]# curl -i -X POST \
--url http://localhost:8001/services/ \
--data 'name=book' \

--data 'url=http://contoso.com/v1/books'

HTTP/1.1 201 Created
Date: Thu, 10 May 2018 12:46:16 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1

{
    "host": "contoso.com", 
    "created_at": 1525956376, 
    "connect_timeout": 60000, 
    "id": "6401d012-893b-437c-a115-c255ee0ef496", 
    "protocol": "http", 
    "name": "book", 
    "read_timeout": 60000, 
    "port": 80, 
    "path": "/v1/books", 
    "updated_at": 1525956376, 
    "retries": 5, 
    "write_timeout": 60000
}
添加一个路由(paths[]的值必须与book服务中的/v1/books一致)
使book服务暴露出来以供用户访问,book服务没必要添加多个路由。
注意啦,注意啦,注意啦,重要参数我只重复3遍
跨源资源共享(CORS)中的服务路由不允许配置--data 'hosts[]=contoso.com'
[root@contoso ~]# curl -i -X POST \
--url http://localhost:8001/services/book/routes \
--data 'paths[]=/v1/books'
HTTP/1.1 201 Created
Date: Thu, 10 May 2018 12:46:27 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1

{
    "created_at": 1525927587, 
    "strip_path": true, 
    "hosts": null, 
    "preserve_host": false, 
    "regex_priority": 0, 
    "updated_at": 1525927587, 
    "paths": [
        "/v1/books"
    ], 
    "service": {
        "id": "6401d012-893b-437c-a115-c255ee0ef496"
    }, 
    "methods": null, 
    "protocols": [
        "http", 
        "https"
    ], 
    "id": "8f4d4267-2edc-4bd9-bc03-6419f5e93317"
}
为book服务启用跨源资源共享(CORS)插件参数配置
URL格式:http://localhost:8001/services/{name of servie}/plugins
[root@contoso ~]# curl -i -X POST \
--url http://localhost:8001/services/book/plugins \
--data "name=cors"  \
--data "config.origins=http://contoso.com" \
--data "config.methods=GET, POST" \
--data "config.headers=Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Auth-Token" \
--data "config.exposed_headers=X-Auth-Token" \
--data "config.credentials=true" \
--data "config.max_age=3600"
HTTP/1.1 201 Created
Date: Thu, 10 May 2018 12:47:29 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1

{
    "created_at": 1525956449000, 
    "config": {
        "methods": [
            "GET", 
            "POST"
        ], 
        "exposed_headers": [
            "X-Auth-Token"
        ], 
        "max_age": 3600, 
        "headers": [
            "Accept", 
            "Accept-Version", 
            "Content-Length", 
            "Content-MD5", 
            "Content-Type", 
            "Date", 
            "X-Auth-Token"
        ], 
        "credentials": true, 
        "origins": [
            "http://contoso.com"
        ], 
        "preflight_continue": false
    }, 
    "id": "5f0c6512-c50a-4df1-8a8f-fbde047b6324", 
    "enabled": true, 
    "service_id": "6401d012-893b-437c-a115-c255ee0ef496", 
    "name": "cors"
}
为book服务的路由{route_id}启用跨源资源共享(CORS)插件参数配置
URL格式:http://localhost:8001/routes/{route_id}/plugins
[root@contoso ~]# curl -i -X POST \
--url http://localhost:8001/routes/8f4d4267-2edc-4bd9-bc03-6419f5e93317/plugins \
--data "name=cors"  \
--data "config.origins=http://contoso.com" \
--data "config.methods=GET, POST" \
--data "config.headers=Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Auth-Token" \
--data "config.exposed_headers=X-Auth-Token" \
--data "config.credentials=true" \
--data "config.max_age=3600"
HTTP/1.1 201 Created
Date: Thu, 10 May 2018 12:47:43 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1

{
    "created_at": 1525956463000, 
    "config": {
        "methods": [
            "GET", 
            "POST"
        ], 
        "exposed_headers": [
            "X-Auth-Token"
        ], 
        "max_age": 3600, 
        "headers": [
            "Accept", 
            "Accept-Version", 
            "Content-Length", 
            "Content-MD5", 
            "Content-Type", 
            "Date", 
            "X-Auth-Token"
        ], 
        "credentials": true, 
        "origins": [
            "http://contoso.com"
        ], 
        "preflight_continue": false
    }, 
    "id": "e4118c02-9665-4e15-ad08-1c50d14088fc", 
    "enabled": true, 
    "route_id": "8f4d4267-2edc-4bd9-bc03-6419f5e93317", 
    "name": "cors"
}
Kong Gateway - 12 基于网关服务的跨源资源共享(CORS)_第1张图片

Kong Gateway - 12 基于网关服务的跨源资源共享(CORS)_第2张图片

你可能感兴趣的:(Kong,API,Gateway,微服务网关,Kong,网关服务)