我正在寻找只允许某些域访问我的 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 作为身份验证/识别的手段,因为它可以轻松修改。
关于laravel - Laravel 白名单域认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32567340/