LoginSignup
23
25

More than 5 years have passed since last update.

LaravelのルートフィルターでIP制限を実現する

Posted at

はじめに

Laravelで特定の機能にIP制限をかけてみます。今回は、ウェブサーバ側の設定ではなく、ルートフィルターで実現してみました。

かいてみる

app/config/app.php
// 略

/*
  |--------------------------------------------------------------------------
  | 管理画面情報
  |--------------------------------------------------------------------------
 */
'admin' => [
    'allowipaddresses' => ['127.0.0.1', '100.100.100.100'],
],

// 略
filters.php
// 略

Route::filter('auth.allowipaddresses', function($request) {
    $clientIp = Request::getClientIp();
    $allowIpaddresses =Config::get('app.admin.allowipaddresses');
    if(!in_array($clientIp, $allowIpaddresses)){
        // 許可されていないIPアドレスからの接続はトップにリダイレクト
        return Redirect::to('/');
    }
});

// 略
routes.php
// 略

/*
  |--------------------------------------------------------------------------
  | 管理画面
  |--------------------------------------------------------------------------
 */
Route::group(['before' => ['force.ssl', 'auth.allowipaddresses', 'auth.admin']], function() {
    Route::controller('admin', 'AdminController');
});

// 略

では、また!

23
25
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
25