白名单域认证 Laravel

Str*_*ses 5 authentication dns middleware whitelist laravel

我正在寻找仅允许某些域访问我的 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.');
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来做这件事,还是我在正确的轨道上?

任何帮助,将不胜感激。

谢谢。

Rod*_*rik 1

你走在正确的轨道上,实施似乎很好。

但是,不要相信 HTTP_REFERER 作为身份验证/识别的手段,因为它很容易被修改。