Laravel配置强制https [英] Laravel configurate force https

查看:6205
本文介绍了Laravel配置强制https的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的网站托管在负载平衡服务器上。 SSL卸载在防火墙上完成,因此客户端被反向代理到Web服务器群。



当https请求到达我们的Laravel应用程序时,服务器变量HTTPS为空和Laravel似乎没有检测到https模式并生成urls(assets& routes)如下:





有没有办法配置Laravel强制URL生成https链接?
我们喜欢有一个配置解决方案,因为我们有一个不在https下运行的开发和临时环境。



注意:
我们已经试图从fideloper的trustedproxy方法,这导致没有改变。
我假设.htaccess重写不是一个选项,因为htaccess重写是基于相同的https头(我们没有收到)或端口(80,laravel调用端口443)。



感谢您的帮助。

解决方案

Laravel的 UrlGenerator 类有一个名为 forceSchema 的方法,它允许您强制使用模式,并忽略从请求的URL中提取的模式。只需创建一个服务提供程序 SecureRoutingServiceProvider ,它使用Laravel的IOC来覆盖默认生成器并返回一个强制使用安全模式的实例:

 使用Illuminate \Routing\UrlGenerator; 
使用Illuminate\Routing\RoutingServiceProvider;

class SecureRoutingServiceProvider extends RoutingServiceProvider
{
public function boot()
{
App :: bind('url',function(){
$ generator = new UrlGenerator(
App :: make('router') - > getRoutes(),
App :: make('request');
});

$ generator-> forceSchema('https');

return $ generator;
}

parent :: boot ;
}
}

接下来,我们需要注册服务提供商将它添加到 app / config / app.php 中的 providers 数组中:

 'providers'=> array(
...,
'SecureRoutingServiceProvider',

这就是所有的事情,我测试了这个代码,它工作正常(在Laravel 4.2) p>

Our website is hosted on a load balanced server. SSL offloading is done on the firewall, so the client is reverse proxied to the web server farm.

When the https request reaches our Laravel app, the server variable HTTPS is empty and Laravel doesn't seem to detect the https mode and generates urls (assets & routes) as:

Is there a way to configure Laravel to force the url's to generate https links? We prefer to have a configuration solution because we have a development and staging environment that aren't running under https.

Notice: We already tried the "trustedproxy" approach from fideloper, and this resulted in no change. I presume that a .htaccess rewrite is not an option since htaccess rewrites are based on the same https header (we don't receive) or port (80, laravel calls port 443).

Thanks for the help.

解决方案

Laravel's UrlGenerator class has a method called forceSchema, which allows you to force the schema to be used and ignore the one extracted from the request's URL. Just create a service provider SecureRoutingServiceProvider which uses Laravel's IOC to override the default generator and return an instance that forces the secure schema:

use Illuminate\Routing\UrlGenerator;
use Illuminate\Routing\RoutingServiceProvider;

class SecureRoutingServiceProvider extends RoutingServiceProvider
{
    public function boot()
    {
        App::bind('url', function () {
            $generator = new UrlGenerator(
                App::make('router')->getRoutes(),
                App::make('request');
            });

            $generator->forceSchema('https');

            return $generator;
        }

        parent::boot();
    }
}

Next we'll need to register the service provider by adding it to the providers array in app/config/app.php:

'providers' => array(
    ...,
    'SecureRoutingServiceProvider',
)

And that's all there is to it. I've tested this code and it works fine (in Laravel 4.2).

这篇关于Laravel配置强制https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆