【问题标题】:How to bypass some IP Addresses for Laravel 5 Maintenance Mode如何绕过 Laravel 5 维护模式的一些 IP 地址
【发布时间】:2015-04-02 08:05:32
【问题描述】:

在 Laravel 4 中,您可以通过以下方式绕过 Laravel 维护模式 (php artisan down) 的一些 IP 地址:

App::down(function()
{
    if ( !in_array(Request::getClientIp(), ['192.168.0.1']))
    {
        return Response::view('maintenance', [], 503);
    }
});

您还可以提供一个配置文件 maintenance.php,其中包含所有 IP 地址的列表,以允许在维护模式下访问您的应用程序:

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Allowed IP Addresses
    |--------------------------------------------------------------------------
    | Include an array of IP addresses or ranges that are allowed access to the app when
    | it is in maintenance mode.
    |
    | Supported formats:

    |
    */

    'allowed_ips' => [
        '10.0.2.2',
        '10.2.*.*',
        '10.0.2.3 - 10.0.2.45',
        '10.0.3.0-10.3.3.3'
    ],

]; 

我的问题是,我如何在 Laravel 5 中实现这一点?

【问题讨论】:

    标签: php config laravel-5 maintenance-mode


    【解决方案1】:

    创建新的中间件

    <?php
    
    namespace App\Http\Middleware;
    
    use Closure;
    
    use Illuminate\Contracts\Foundation\Application;
    
    use Illuminate\Http\Request;
    
    use Symfony\Component\HttpKernel\Exception\HttpException;
    
    
    
    class CheckForMaintenanceMode
    
    {
    
        protected $request;
    
        protected $app;
    
    
    
        public function __construct(Application $app, Request $request)
    
        {
    
            $this->app = $app;
    
            $this->request = $request;
    
        }
    
    
    
        /**
    
         * Handle an incoming request.
    
         *
    
         * @param  \Illuminate\Http\Request  $request
    
         * @param  \Closure  $next
    
         * @return mixed
    
         */
    
    
    
        public function handle($request, Closure $next)
    
        {
    
            if ($this->app->isDownForMaintenance() &&
    
                !in_array($this->request->getClientIp(), ['::1','another_IP']))
    
            {
    
                throw new HttpException(503);
    
            }
    
    
    
            return $next($request);
    
        }
    
    }
    

    '::1' 是您自己的 IP,假设您在本地主机中,如果不是,请指定您的 IP。您可以在阵列中排除多个 IP。检查Excluding your IP Address in Maintenance Mode (php artisan down) in Laravel 5

    【讨论】:

      猜你喜欢
      • 2014-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-21
      • 1970-01-01
      相关资源
      最近更新 更多