我正在寻找仅允许某些域访问我的laravel应用程序的最佳方法。我当前正在使用Laravel 5.1,并且如果引荐域不在白名单域中,则正在使用中间件进行重定向。
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.');
}
}
}
有没有更好的方法可以做到这一点,还是我走在正确的轨道上?
任何帮助,将不胜感激。
谢谢。
最佳答案
您走在正确的轨道上,实现似乎很好。
但是,请勿信任HTTP_REFERER作为身份验证/标识的方式,因为它很容易修改。