2015-09-14 64 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作为身份验证/身份验证的手段,因为它可以轻松修改。