Allow Only Certain ip addresses to API in Laravel 5.8
In this post we will give you information about Allow Only Certain ip addresses to API in Laravel 5.8. Hear we will give you detail about Allow Only Certain ip addresses to API in Laravel 5.8And how to use it also give you demo for it if it is necessary.
Today, we will learn how to restrict ip address in laravel api or web route. You can allow only particular ip address in laravel 5.8 application. we will create middleware that will check ip address is white listed then you can access api otherwise you will get error message.
we will give access only particular ip address using laravel middleware. laravel middleware provide way to prevent other ip address that want to access over secure api or url.
In this tutorial example, we will create one middleware as “CheckIpMiddleware” and we will use that middleware on every secure api and url. So just see bellow steps how to complete this things:
Create Middleware
In this step, we will create one middleware as “CheckIpMiddleware” using bellow command:
php artisan make:middleware CheckIpMiddleware
Now you can see created new file CheckIpMiddleware.php in Middleware directory. I created whiteIps array variable for white listed ip address. Only can access api or url that listed on that array. you need to update that as bellow:
app/Http/Middleware/CheckIpMiddleware.php
<?php
namespace AppHttpMiddleware;
use Closure;
class CheckIpMiddleware
{
public $whiteIps = ['192.168.1.1', '127.0.0.1'];
/**
* Handle an incoming request.
*
* @param IlluminateHttpRequest $request
* @param Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (!in_array($request->ip(), $this->whiteIps)) {
/*
You can redirect to any error page.
*/
return response()->json(['your ip address is not valid.']);
}
return $next($request);
}
}
Now we need to register this middleware with specific name. so let’s register it by Kernel file.
app/Http/Kernel.php
protected $routeMiddleware = [
'auth' => AppHttpMiddlewareAuthenticate::class,
'auth.basic' => IlluminateAuthMiddlewareAuthenticateWithBasicAuth::class,
'bindings' => IlluminateRoutingMiddlewareSubstituteBindings::class,
'cache.headers' => IlluminateHttpMiddlewareSetCacheHeaders::class,
......
'checkIp' => AppHttpMiddlewareCheckIpMiddleware::class,
];
Use Middleware
In this step, we will use “checkIp” middleware in api and web routes. i will create example for api and web routes how it is using. so let’s see both example:
routes/api.php
Route::middleware(['checkIp'])->group(function () {
Route::post('users', 'UserController@index')
Route::post('posts', 'PostController@index')
});
routes/web.php
Route::get('my-web', ['middleware' => ['checkIp'], function () {
return view('test');
}]);
Now you can run your project and check
I hope it can help you…
Hope this code and post will helped you for implement Allow Only Certain ip addresses to API in Laravel 5.8. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs