2015-09-14 108 views
4

我在尋找最好的方法,只允許某些域訪問我的laravel應用程序。我目前使用的是Laravel 5.1,如果引用域不在白名單域中,我正在使用中間件來重定向。白名單域身份驗證Laravel

class Whitelist { 

    /** 
    * Handle an incoming request. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @param \Closure $next 
    * @return mixed 
    */ 

    public function handle($request, Closure $next) 
    { 
     //requesting URL 
     $referer = Request::server('HTTP_REFERER'); 

     //parse url to match base in table 
     $host = parse_url($referer, PHP_URL_HOST); 
     $host = str_replace("www.", "", $host); 

     //Cached query to whitelisted domains - 1400 = 24 hours 
     $whiteList = Cache::remember('whitelist_domains', 1400, function(){ 
      $query = WhiteListDomains::lists('domain')->all(); 
      return $query; 
     }); 

     //Check that referring domain is whitelisted or itself? 
     if(in_array($host, $whiteList)){ 
      return $next($request); 
     }else{ 
      header('HTTP/1.0 403 Forbidden'); 
      die('You are not allowed to access this file.'); 
     } 
    } 
} 

有沒有更好的方式去做這件事,或者我在正確的軌道上?

任何幫助,將不勝感激。

謝謝。

+0

當你說「我正在尋找最好的方式,只允許某些域訪問我的laravel應用程序。」你究竟是什麼意思?用戶必須位於其中一個域上,或者用戶必須由其中一個域引用? – delatbabel

+1

我認爲這是一個乾淨而好的解決方案 –

回答

0

你在正確的軌道上,實施似乎沒有問題。

但是,不要將HTTP_REFERER作爲身份驗證/身份驗證的手段,因爲它可以輕鬆修改。