1. 程式人生 > >13 基於閘道器服務的IP白名單限制訪問(Whitelist IP Restriction)

13 基於閘道器服務的IP白名單限制訪問(Whitelist IP Restriction)


用Kong配置一個book服務
在安裝並啟動Kong之後,使用Kong的管理API埠8001新增一個名稱為book的服務
[[email protected] ~]# 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: Sat, 12 May 2018 12:27:47 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": 1526099267, 
    "connect_timeout": 60000, 
    "id": "f4c0d700-ce37-4a97-b7c2-21c4f8620510", 
    "protocol": "http", 
    "name": "book", 
    "read_timeout": 60000, 
    "port": 80, 
    "path": "/v1/books", 
    "updated_at": 1526099267, 
    "retries": 5, 
    "write_timeout": 60000
}
新增一個路由(paths[]的值必須與book服務中的/v1/books一致)
使book服務暴露出來以供使用者訪問,book服務沒必要新增多個路由。
注意啦,注意啦,注意啦,重要引數我只重複3遍
跨源資源共享(CORS)中的服務路由不允許配置--data 'hosts[]=contoso.com'引數值
[[email protected] ~]# curl -i -X POST \
--url http://localhost:8001/services/book/routes \
--data 'paths[]=/v1/books'
HTTP/1.1 201 Created
Date: Sat, 12 May 2018 12:30:05 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": 1526099405, 
    "strip_path": true, 
    "hosts": null, 
    "preserve_host": false, 
    "regex_priority": 0, 
    "updated_at": 1526099405, 
    "paths": [
        "/v1/books"
    ], 
    "service": {
        "id": "f4c0d700-ce37-4a97-b7c2-21c4f8620510"
    }, 
    "methods": null, 
    "protocols": [
        "http", 
        "https"
    ], 
    "id": "42251e97-2921-45ea-bb19-0416019ea67a"   // {route_id} = id 
}

我們可以這樣檢查一下book服務和它的路由配置的是否正確
[[email protected] ~]# curl -i -X GET \
--url http://localhost:8000/v1/books

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 244
Connection: keep-alive
Date: Sat, 12 May 2018 12:33:12 GMT
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.1.13
X-Powered-By: PHP/7.1.13
X-Kong-Upstream-Latency: 27
X-Kong-Proxy-Latency: 61
Via: kong/0.13.1

[
    {
        "id": 1, 
        "title": "Fashion That Changed the World", 
        "author": "Jennifer Croll"
    }, 
    {
        "id": 2, 
        "title": "Brigitte Bardot - My Life in Fashion", 
        "author": "Henry-Jean Servat and Brigitte Bardot"
    }, 
    {
        "id": 3, 
        "title": "The Fashion Image", 
        "author": "Thomas Werner"
    }
]

為book服務啟用跨源資源共享(CORS)外掛引數配置
URL格式:http://localhost:8001/services/{name of servie}/plugins
[[email protected] ~]# 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: Sat, 12 May 2018 12:39:35 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": 1526128775000, 
    "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": "e352e234-e5ab-4ba8-ad00-3796e176a720", 
    "enabled": true, 
    "service_id": "f4c0d700-ce37-4a97-b7c2-21c4f8620510", 
    "name": "cors"
}
為book服務的路由{route_id}啟用跨源資源共享(CORS)外掛引數配置
{route_id} 引數的值是使用不帶引數--data 'hosts[]=contoso.com'建立的路由id值
URL格式:http://localhost:8001/routes/{route_id}/plugins
[root[email protected] ~]# curl -i -X POST \
--url http://localhost:8001/routes/42251e97-2921-45ea-bb19-0416019ea67a/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: Sat, 12 May 2018 12:37:33 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": 1526128653000, 
    "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": "1f6dc33a-8a30-473f-929b-f4d38aadbdc7", 
    "enabled": true, 
    "route_id": "42251e97-2921-45ea-bb19-0416019ea67a", 
    "name": "cors"
}

我們希望用域名地址訪問8000埠或者8443埠

像本範例中這樣的地址格式(假如你申請了一個公網域名contoso.org 固定公網IPv4是 123.125.115.110(一旦公網域名申請下來就把hosts檔案中的contoso.org對應的假公網IP換成申請域名填寫固定公網IPv4地址123.125.115.110(即是替換第一個192.168.10.10),下面第2個192.168.10.10千萬別動它)第2個同樣的IP可不要更改,它永遠不變的作為內網IP地址使用,contoso.com是自定義域名,永遠作為公司內網域名使用,下面截圖中的contoso.org域名是在模擬公網網頁地址,這都是很基礎的東西,本不想囉唆的,就順便解釋一下):

http://contoso.org:8000/v1/books

https://contoso.org:8443/v1/books


上面只是模擬出了公網地址格式的本地訪問,下面是模擬遠端客戶端瀏覽器訪問Kong閘道器暴露出來的book服務


上面即模擬了公網地址格式 又模擬了遠端的客戶端瀏覽器訪問Kong閘道器暴露出來的book服務



[[email protected] ~]# pg_dump -h 127.0.0.1 -p 5432 -U postgres kong > /opt/kong-20180427.bak   # 備份kong資料庫  
Password: 123456  

準備工作終於都準備好了,本篇blog正式進入主題

為book服務的路由{route_id}啟動Basic驗證外掛,我們可以用9種驗證方式來取代basic-auth,
其它8種驗證方式我就不舉例了,真要舉例估計我都能寫一本書,篇幅太多太長了,就不施展了  
URL格式:http://localhost:8001/routes/{route_id}/plugins  
[[email protected] ~]# curl -i -X POST \
--url http://localhost:8001/routes/42251e97-2921-45ea-bb19-0416019ea67a/plugins \
--data "name=basic-auth" \
--data "config.hide_credentials=true"

HTTP/1.1 201 Created
Date: Sat, 12 May 2018 12:47:11 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": 1526129231000, 
    "config": {
        "hide_credentials": true, 
        "anonymous": ""
    }, 
    "id": "7992d4c5-4a8d-445e-8271-06c46c9f5f5d", 
    "enabled": true, 
    "route_id": "42251e97-2921-45ea-bb19-0416019ea67a", 
    "name": "basic-auth"
}
新增第1個username為jack的消費者,{custom_id}引數可省略,此引數是個自定義唯一標識,  
它作用是把消費者jack對映到另外一個數據庫上  
[[email protected] ~]# curl -i -X POST \
--url http://localhost:8001/consumers/ \
--data "username=jack"
HTTP/1.1 201 Created
Date: Sat, 12 May 2018 12:48:23 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": 1526129303000, 
    "username": "jack", 
    "id": "61e2ce89-3ebf-4e1f-8fda-3e3cd145a9bd"
}
為第1個使用者jack啟用Basic驗證外掛  
URL格式:http://localhost:8001/consumers/{username or consumer_id}/basic-auth  
[[email protected] ~]# curl -i -X POST \
--url http://localhost:8001/consumers/jack/basic-auth \
--data "[email protected]" \
--data "password=123456"
HTTP/1.1 201 Created
Date: Sat, 12 May 2018 12:50:05 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": 1526129405000, 
    "id": "ae14ab2f-756e-40be-8c2c-dc45de901760", 
    "username": "[email protected]", 
    "password": "70ee8509541cc3c9062ce62e868f19347d289d72", 
    "consumer_id": "61e2ce89-3ebf-4e1f-8fda-3e3cd145a9bd"
}
線上base64編碼工具http://tool.oschina.net/encrypt?type=3  
鍵-值對{username:password}字串  
[email protected]:123456 左邊的鍵-值對字串BASE64編碼結果為:  
amFja0Bob3RtYWlsLmNvbToxMjM0NTY=  
使用使用者jack的Basic驗證方式訪問書籍資料介面  
[[email protected] ~]# curl -i -X GET \
--url http://localhost:8000/v1/books/3 \
--header "Authorization: Basic amFja0Bob3RtYWlsLmNvbToxMjM0NTY="
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 63
Connection: keep-alive
Date: Sat, 12 May 2018 12:51:28 GMT
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.1.13
X-Powered-By: PHP/7.1.13
Vary: Origin
Access-Control-Allow-Origin: http://contoso.com
Access-Control-Allow-Credentials: true
Access-Control-Expose-Headers: X-Auth-Token
X-Kong-Upstream-Latency: 26
X-Kong-Proxy-Latency: 48
Via: kong/0.13.1

[{"id":3,"title":"The Fashion Image","author":"Thomas Werner"}]
為名稱為book的服務啟用IP白名單限制訪問
其中192.168.10.50表示限制macOS系統這一臺計算機不能訪問book服務
其中192.168.43.0/24表示限制IP地址是192.168.43這一整個網段的IP都不能訪問book服務(Windows 10在此網段內)
URL格式:http://contoso.org:8001/services/{service}/plugins
[[email protected] ~]# curl -i -X POST \
--url http://localhost:8001/services/book/plugins \
--data "name=ip-restriction"  \

--data "config.whitelist=192.168.10.50, 192.168.43.0/24"

HTTP/1.1 201 Created
Date: Sat, 12 May 2018 12:58:25 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": 1526129906000, 
    "config": {
        "whitelist": [
            "192.168.10.50", 
            "192.168.43.0/24"
        ]
    }, 
    "id": "d3ef0103-9eca-4e20-a845-10cfc2152ca1", 
    "enabled": true, 
    "service_id": "f4c0d700-ce37-4a97-b7c2-21c4f8620510", 
    "name": "ip-restriction"
}
為名稱為book的服務的路由{route_id啟用IP白名單限制訪問
其中192.168.10.50表示限制macOS系統這一臺計算機不能訪問book服務的路由
其中192.168.43.0/24表示限制IP地址是192.168.43這一整個網段的IP都不能訪問book服務的路由(Windows 10在此網段內)
URL格式:http://localhost:8001/routes/{route_id}/plugins
[[email protected] ~]# curl -i -X POST \
--url http://localhost:8001/routes/42251e97-2921-45ea-bb19-0416019ea67a/plugins \
--data "name=ip-restriction"  \
--data "config.whitelist=192.168.10.50, 192.168.43.0/24"
HTTP/1.1 201 Created
Date: Sat, 12 May 2018 13:01:21 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": 1526130082000, 
    "config": {
        "whitelist": [
            "192.168.10.50", 
            "192.168.43.0/24"
        ]
    }, 
    "id": "bafcf0ad-31dd-4779-aca9-c2dea8384e29", 
    "enabled": true, 
    "route_id": "42251e97-2921-45ea-bb19-0416019ea67a", 
    "name": "ip-restriction"
}

到下面這個命令這兒,在不同作業系統的客戶端各種瀏覽器裡即使用jack的賬號成功登陸也會返回

{"message":"Your IP address is not allowed"} 這條資訊才是與我們預期的結果一致,為什麼這麼說?

因為我還沒有讓登入使用者與IP白名單進行關聯這條命令執行,最後面會演示關聯後的效果(在白名單裡的IP都能訪問book書籍資料介面),只要白名單沒有關聯具體的使用者,那麼現在所有的使用者就都相當於在黑名單當中,大家都不能訪問書籍介面

現在的jack,就相當於在黑名單中,唯一名稱的book服務不允許我們即定義服務的IP白名單又定義IP的黑名單

[[email protected] ~]# curl -i -X GET \
--url http://localhost:8000/v1/books/3 \
--header "Authorization: Basic amFja0Bob3RtYWlsLmNvbToxMjM0NTY="
HTTP/1.1 403 Forbidden
Date: Sat, 12 May 2018 13:02:26 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Server: kong/0.13.1
Vary: Origin
Access-Control-Allow-Origin: http://contoso.com
Access-Control-Allow-Credentials: true
Access-Control-Expose-Headers: X-Auth-Token

{"message":"Your IP address is not allowed"}

以下命令就是上面提到的最後面會演示關聯後的效果(在白名單裡的IP都能訪問book書籍資料介面)
現在可以使用以下命令將白名單whitelist關聯到消費者jack:
{consumer_id} = 61e2ce89-3ebf-4e1f-8fda-3e3cd145a9bd
[[email protected] ~]# curl -i -X POST \
--url http://localhost:8001/plugins \
--data "name=ip-restriction" \
--data "consumer_id=61e2ce89-3ebf-4e1f-8fda-3e3cd145a9bd"  \
--data "config.whitelist=192.168.10.50, 192.168.43.0/24"

HTTP/1.1 201 Created
Date: Sat, 12 May 2018 16:07:56 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": 1526141276000, 
    "config": {
        "whitelist": [
            "192.168.10.50", 
            "192.168.43.0/24"
        ]
    }, 
    "id": "fb92b792-d2f2-44be-a8a2-f8d12eed4cb4", 
    "name": "ip-restriction", 
    "enabled": true, 
    "consumer_id": "61e2ce89-3ebf-4e1f-8fda-3e3cd145a9bd"
}
我期望 macOS 系統能夠訪問http://contoso.org:8000/v1/books 原來限制的情形下都能訪問 現在允許macOS訪問 當然就能訪問了 奇怪的是 明明允許Windows 10系統能夠訪問http://contoso.org:8000/v1/books

,但它依然返回{"message":"Your IP address is not allowed"}  這不應該啊 難道我自己玩錯了其中某個步驟 還是官網釋出的東西有問題  我現在得繼續實驗 看看問題出在哪兒了  歡迎大家與我交流 。。。。。。。。。。。。。。。

相關推薦

13 基於服務IP名單限制訪問Whitelist IP Restriction

用Kong配置一個book服務在安裝並啟動Kong之後,使用Kong的管理API埠8001新增一個名稱為book的服務[[email protected] ~]# curl -i -X POST \--url http://localhost:8001/servic

18 基於服務的請求大小限制Request Size Limiting

Configure a Service in Kong[[email protected] ~]# curl -i -X POST \--url http://localhost:8001/services/ \--data 'name=book' \--data

12 基於服務的跨源資源共享(CORS)

如果在前面11篇Kong Gateway系列的文章中,你親自動手實驗過用瀏覽器訪問以下地址:http://localhost:8000/v1/books你將無法獲得書籍介面返回的書籍記錄,本篇blog能讓你在瀏覽器中用8000埠或者8443埠能直接訪問書籍的Restful A

03 基於服務的OAuth2驗證OAuth2 Authentication Code Grant 授權碼模式

https://getkong.org/plugins/oauth2-authentication 我們演示還是用books 的Restful api資料介面,把Kong Gateway - 01範例中PostgresSQL中的kong資料庫刪掉, 匯入一個已經配置好

基於.NET CORE微服務框架 -Api服務管理

最近也更新了surging新的版本 更新內容: 1. 擴充套件Zookeeper封裝 2. 增加服務元資料 3. 增加API閘道器 開源地址:https://github.com/dotnetcore/surging 2.軟體環境 IDE:Visual Studio 2017 1

Spring Cloud中的API服務Zuul 13

轉自 https://blog.csdn.net/u012702547/article/details/77823434 這個系列我感覺真的太好了,可以一步一步的瞭解spring cloud 的搭建以及更深層次的東西,對想學這門技術的朋友真的入門特別的快,感謝這位大哥的分享,我也會持續

.NET Core微服務基於Ocelot實現API服務

一、啥是API閘道器?   API 閘道器一般放到微服務的最前端,並且要讓API 閘道器變成由應用所發起的每個請求的入口。這樣就可以明顯的簡化客戶端實現和微服務應用程式之間的溝通方式。以前的話,客戶端不得不去請求微服務A(假設為Customers),然後再到微服務B(假設為Orders),然後是微服

.NET Core微服務基於Ocelot實現API服務

一、負載均衡與請求快取 1.1 負載均衡   為了驗證負載均衡,這裡我們配置了兩個Consul Client節點,其中ClientService分別部署於這兩個節點內(192.168.80.70與192.168.80.71)。   為了更好的展示API Repsonse來自哪個節點,我們更改一下

服務之API:Kong:外掛介紹:認證外掛ip-restriction之黑白名單

kong目前提供了37個外掛,其中商業收費7個,30個開源免費的外掛,可以設定到api/服務/路由粒度上。 環境設定 外掛功能 類別 免費/收費 name 外掛名 使用場景 認證 免費 basic-au

springcloud之Zuul服務

Zuul是Netflix開源的微服務閘道器,它的核心是一系列的過濾器,這些過濾器可以完成以下功能: 身份認證與安全:識別每個資源的驗證要求,並拒絕那些與要求不符的請求。 審查與監控:在邊緣位置追蹤有意義的資料和統計結果,從而帶來精確的生產檢視。 動態路由:動態的請求路由到不同的後端叢集。

Spring Cloud ZuulAPI服務3

過濾器 在Spring Cloud Zuul中實現的過濾器必須包含4個基本特徵:過濾型別,執行順序,執行條件,具體操作。這就是ZuulFilter介面中定義的4個抽象方法: public abstract String filterType(); public abst

Spring Cloud ZuulAPI服務2

路由詳情 傳統路由配置 傳統路由配置方式就是在不依賴與服務發現機制的情況下,通過在配置檔案中具體指定每個路由表示式與服務例項的對映關係來實現API閘道器對外部請求的路由。 單例項配置:通過zuul.routes.<route>.path與zuul.routes.<r

Spring Cloud ZuulAPI服務1

API閘道器是一個智慧的應用伺服器,它的定義類似於面向物件設計模式中的Facade模式,它的存在就像是整個微服務架構系統的門面一樣,所有的外部客戶端訪問都需要經過他來進行排程和過濾。它除了要實現請求路由,負載均衡,校驗過濾等功能之外,還需要更多能力,比如與服務治理框架的結合,請求轉發時的熔斷機制

SpringCloud之服務(gateway)

前言 閘道器服務在SpringCloud中有很重要的作用。 可以將服務跟外網進行隔離起到一定的保護作用,同時服務間區域網通訊更加快捷。而且在閘道器中可以做限流、許可權校驗,使得服務更加專注自身業務。比如說下訂單需要登入許可權,限流,我們在本篇將介紹如何使用。 搭建閘道器專案 注意:需要新增Eureka

api服務 zuul-路由

路由是微服務架構中必須的一部分,比如,“/” 可能對映到你的WEB程式上,”/api/users “可能對映到你的使用者服務上,“/api/shop”可能對映到你的商品服務商。(註解:我理解這裡的這幾個對映就是說通過Zuul這個閘道器把服務對映到不同的服務商去處理,從而變成了微服務!) 通過Zuu

Spring Cloud系列二十三 API服務Spring Cloud ZuulFinchley.RC2版本

為什麼使用Spring Cloud Zuul? 通過前幾章的介紹,我們對於Spring Cloud Netflix 下的核心元件已經瞭解了大半,利用這些元件我們已經可以構建一個簡單的微服務架構系統,比如通過使用Spring Cloud Eureka實現高可用的服務註冊中

服務Zuul

SpringCloud體系最常用閘道器元件為Zuul,閘道器Zuul通過配置檔案約定的介面規則將請求轉發到對應的微服務子專案去處理,這發揮的是其路由功能。除此之外,Zuul的路由功能可以處理前端的跨越

SpringCloud實戰6-Zuul服務

為什麼需要閘道器呢? 我們知道我們要進入一個服務本身,很明顯我們沒有特別好的辦法,直接輸入IP地址+埠號,我們知道這樣的做法很糟糕的,這樣的做法大有問題,首先暴露了我們實體機器的IP地址,別人一看你的IP地址就知道服務部署在哪裡,讓別人很方便的進行攻擊操作。 第二,我

Spring Cloud入門:API服務Spring Cloud Gateway

文章例項使用的Spring Cloud版本為Finchley.SR1,Spring Boot版本為2.0.4。 1 Spring Cloud Gateway 在微服務架構中,閘道器作為服務的一個統一入口,所有的外部客戶端訪問都需要經過它來排程和過濾,可以實現的功能包括動

服務架構

一、要做一個閘道器服務的架構 閘道器具備哪些功能 隔絕網站應用服務與外部服務商應用的直接訪問 限流 限定訪問,ip、域名、請求等非法資訊攔截 日誌記錄 不會因為業務增加而導致網關係統重啟 二、要怎麼設計  系統職責簡述 業務系統: