Blocking Specific IP from Accessing API
In this article, we will explore how to implement IP restriction for APIs. With this feature, we can block or allow IP addresses to access APIs.
Prerequisites
Configure API IP Whitelist
Create a Plugin Template
Refer to the documentation on Create Plugin Template.
In 9. Enable the required plugins by clicking the corresponding Enable button, select the ip-restriction
plugin.
In 11. Edit the plugin parameters in the Configuration Raw Data, enter the following configuration:
{
"whitelist": ["8.8.8.8"],
"message": "Sorry, your IP address is not allowed."
}
In this example, we only allow the IP address 8.8.8.8
to access the API.
Apply the Plugin Template to the API
Taking CreateProduct
as an example.
Refer to the documentation on Configure API.
In 9. Edit the properties of the API, modify the plugin template to the one created in the previous step that includes the ip-restriction
plugin.
Validate IP Whitelist
Send an API request:
curl -X POST -d '{"name": "iPhone 13 Pro", "price": 999.99}' -H 'HOST: test.com' -v http://${API7_GATEWAY_ADDRESS}/products
This should return an error message because our IP address is not 8.8.8.8.
Configure API IP Blacklist
Configure the Plugin Template
Now move 8.8.8.8
to the blacklist.
Refer to the documentation on Configure Plugin Template. In 11. Edit the plugin's parameters in the "Configure Raw Data" section, modify the configuration:
{
"blacklist": ["8.8.8.8"],
"message": "Sorry, your IP address is not allowed."
}
Validate IP Blacklist
Send an API request:
curl -X POST -d '{"name": "iPhone 13 Pro", "price": 999.99}' -H 'HOST: test.com' -v http://${API7_GATEWAY_ADDRESS}/products
Since our IP is not restricted by the blacklist, we can access the API endpoint normally. At this point, you will see the expected API response.
{
"id":1
}