Laravel 5.6 TrustedProxies 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48681417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:21:29  来源:igfitidea点击:

Laravel 5.6 TrustedProxies error

phplaravelproxylaravel-5.6

提问by SkifAlef

I've upgraded from L5.5 to L5.6 today (updating Symfony components to v4 in the process). Also I've updated fideloper/proxypackage to 4.0 as of official Laravel 5.6 upgrade guide.

我今天从 L5.5 升级到 L5.6(在此过程中将 Symfony 组件更新到 v4)。此外,我已将fideloper/proxy软件包更新为 4.0 作为官方 Laravel 5.6 升级指南。

After that I starts to getting this error: Type error: Argument 2 passed to Symfony\Component\HttpFoundation\Request::setTrustedProxies() must be of the type integer, array given, called in /var/www/html/vendor/fideloper/proxy/src/TrustProxies.php on line 54

之后,我开始收到此错误: Type error: Argument 2 passed to Symfony\Component\HttpFoundation\Request::setTrustedProxies() must be of the type integer, array given, called in /var/www/html/vendor/fideloper/proxy/src/TrustProxies.php on line 54

Symfony 4's Symfony\Component\HttpFoundation\Request::setTrustedProxies()indeed expects integer (bitmask) as a 2nd argument:

Symfony 4Symfony\Component\HttpFoundation\Request::setTrustedProxies()确实希望整数(位掩码)作为第二个参数:

/**
* Sets a list of trusted proxies.
*
* You should only list the reverse proxies that you manage directly.
*
* @param array $proxies          A list of trusted proxies
* @param int   $trustedHeaderSet A bit field of Request::HEADER_*, to set which headers to trust from your proxies
*
* @throws \InvalidArgumentException When $trustedHeaderSet is invalid
*/
public static function setTrustedProxies(array $proxies, int $trustedHeaderSet)
{
    self::$trustedProxies = $proxies;
    self::$trustedHeaderSet = $trustedHeaderSet;
}

and fideloper/proxy4.0 is indeed gives an array instead of an integer into this function:

并且fideloper/proxy4.0 确实在此函数中提供了一个数组而不是整数:

public function handle(Request $request, Closure $next)
{
    $request::setTrustedProxies([], $this->getTrustedHeaderNames()); // Reset trusted proxies between requests
    $this->setTrustedProxyIpAddresses($request);
    return $next($request);
}

and

/**
 * Retrieve trusted header name(s), falling back to defaults if config not set.
 *
 * @return array
 */
protected function getTrustedHeaderNames()
{
    return $this->headers ?: $this->config->get('trustedproxy.headers');
}

So I can't understand if this is bug in fideloper/proxyor I'm just missing something?

所以我不明白这是错误fideloper/proxy还是我只是遗漏了什么?

采纳答案by WJDev

As stated in the upgrade guide, you need to set the $headers property in App\Http\Middleware\TrustProxiesto a bit property.

升级指南所述,您需要将App\Http\Middleware\TrustProxies 中的 $headers 属性设置为 bit 属性。

The constants are defined in Symfony\Component\HttpFoundation\Request.

常量在Symfony\Component\HttpFoundation\Request中定义。

const HEADER_FORWARDED = 0b00001; // When using RFC 7239
const HEADER_X_FORWARDED_FOR = 0b00010;
const HEADER_X_FORWARDED_HOST = 0b00100;
const HEADER_X_FORWARDED_PROTO = 0b01000;
const HEADER_X_FORWARDED_PORT = 0b10000;
const HEADER_X_FORWARDED_ALL = 0b11110; // All "X-Forwarded-*" headers
const HEADER_X_FORWARDED_AWS_ELB = 0b11010; // AWS ELB doesn't send X-Forwarded-Host

In the upgrade guide, HEADER_X_FORWARDED_ALL is used but you can use a combination of the bit values.

在升级指南中,使用了 HEADER_X_FORWARDED_ALL,但您可以使用位值的组合。

回答by Inigo

After some investigation (Winmerge comparison with a fresh install of Laravel 5.6) this comes down to a difference in the files app\Http\Middleware\TrustProxies.php:

经过一些调查(Winmerge 与全新安装的 Laravel 5.6 进行比较),这归结为文件中的差异app\Http\Middleware\TrustProxies.php

Laravel 5.5:

Laravel 5.5:

namespace App\Http\Middleware;

use Illuminate\Http\Request;
use Fideloper\Proxy\TrustProxies as Middleware;

class TrustProxies extends Middleware
{
    /**
     * The trusted proxies for this application.
     *
     * @var array
     */
    protected $proxies;

    /**
     * The current proxy header mappings.
     *
     * @var array
     */
    protected $headers = [
        Request::HEADER_FORWARDED => 'FORWARDED',
        Request::HEADER_X_FORWARDED_FOR => 'X_FORWARDED_FOR',
        Request::HEADER_X_FORWARDED_HOST => 'X_FORWARDED_HOST',
        Request::HEADER_X_FORWARDED_PORT => 'X_FORWARDED_PORT',
        Request::HEADER_X_FORWARDED_PROTO => 'X_FORWARDED_PROTO',
    ];
}

Laravel 5.6:

Laravel 5.6:

namespace App\Http\Middleware;

use Illuminate\Http\Request;
use Fideloper\Proxy\TrustProxies as Middleware;

class TrustProxies extends Middleware
{
    /**
     * The trusted proxies for this application.
     *
     * @var array
     */
    protected $proxies;

    /**
     * The headers that should be used to detect proxies.
     *
     * @var string
     */
    protected $headers = Request::HEADER_X_FORWARDED_ALL;
}

Ergo, set protected $headers = Request::HEADER_X_FORWARDED_ALL;as per Laravel 5.6 version

Ergo,protected $headers = Request::HEADER_X_FORWARDED_ALL;按照 Laravel 5.6 版本设置

回答by aphoe

Open app\Http\Middleware\TrustProxies.php.

打开app\Http\Middleware\TrustProxies.php

Change the following

更改以下内容

protected $headers = [
    Request::HEADER_FORWARDED => 'FORWARDED',
    Request::HEADER_X_FORWARDED_FOR => 'X_FORWARDED_FOR',
    Request::HEADER_X_FORWARDED_HOST => 'X_FORWARDED_HOST',
    Request::HEADER_X_FORWARDED_PORT => 'X_FORWARDED_PORT',
    Request::HEADER_X_FORWARDED_PROTO => 'X_FORWARDED_PROTO',
];

to

protected $headers = Request::HEADER_X_FORWARDED_ALL;

回答by RedBonzai Developers

Please keep in mind also that when upgrading from Laravel 5.5 to 5.6 Laravel creates a new file called TrustProxies.php with the same namespace as TrustedProxies i.e "App\Http\Middleware\TrustProxies". In Laravel 5.6, the TrustedProxies.phpfile is correctas mentioned above.
The TrustProxies.phpis not. But if they both exist in the same namespace, then Laravel will use the one that it finds first, and that is the TrustProxies.phpfile.

还请记住,当从 Laravel 5.5 升级到 5.6 时,Laravel 会创建一个名为 TrustProxies.php 的新文件,其命名空间与 TrustedProxies ie 相同"App\Http\Middleware\TrustProxies"。在 Laravel 5.6 中,TrustedProxies.php文件如上所述是正确的
TrustProxies.php是没有的。但是如果它们都存在于同一个命名空间中,那么 Laravel 将使用它首先找到的那个,那就是TrustProxies.php文件。

If you have both, then delete TrustProxies.php, and make sure that TrustedProxies.phphas the correct changes as mentioned above.

如果两者都有,则删除TrustProxies.php,并确保TrustedProxies.php具有如上所述的正确更改。

Have an awesome day. Christian

祝你有愉快的一天。基督教