2017-01-09 54 views
18

我与Laravel 5.3(在Windows服务器上)有同样的问题。我做了所有可能的尝试:检查.env文件,配置,工匠新密钥生成,缓存:清除,配置:清除,作曲家更新,但问题随机存在。Random RuntimeException:唯一支持的密码是AES-128-CBC和AES-256-CBC,密钥长度正确

请注意,具有完全相同版本的apache,mysql,php的相同代码在Mac OS上不会产生此问题。

我发现密钥(加密器约束器的第一个参数)到达“有时”空,当然它失败。大多数情况下,密钥都是正确的,但随机密钥从EncryptionServiceProvider中清空,然后轮流将它请求到应用配置。

因此,唯一对我有用的解决方案是在EncryptionServiceProvider中添加一个if ($key),以便加密构造函数不会被空键调用。

当然不是一个“干净”的解决方案也不说明问题,但至少避免了寻找充满错误日志文件:

RuntimeException: The only supported ciphers are AES-128-CBC and AES-256-CBC and pages are displayed correctly.

如果这是一个错误Laravel我不知道,但当然,如果有人能解释这一点,我会更乐意知道。

下面是我修改后的类:我只是说new Encrypterif ($key)行:

class EncryptionServiceProvider extends ServiceProvider 
{ 
    /** 
    * Register the service provider. 
    * 
    * @return void 
    */ 
    public function register() 
    { 
     $this->app->singleton('encrypter', function ($app) { 
      $config = $app->make('config')->get('app'); 

      // If the key starts with "base64:", we will need to decode the key before handing 
      // it off to the encrypter. Keys may be base-64 encoded for presentation and we 
      // want to make sure to convert them back to the raw bytes before encrypting. 
      if (Str::startsWith($key = $config['key'], 'base64:')) { 
       $key = base64_decode(substr($key, 7)); 
      } 
     if ($key) 
      return new Encrypter($key, $config['cipher']); 
     }); 
    } 
} 

更多细节和回溯日志:

当然,我写我检查, .env文件,配置,工匠新密钥生成,缓存:清除,配置:清除,作曲家更新。 这东西是可以的,因为它工作99%的时间,但随机我得到的错误。

这里回溯追踪:

[2017-01-09 10:25:40] test.ERROR: RuntimeException: The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths. in C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Encryption\Encrypter.php:43

堆栈跟踪:

#0 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Encryption\EncryptionServiceProvider.php(27): Illuminate\Encryption\Encrypter->__construct('', 'AES-256-CBC') 
#1 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Container\Container.php(746): Illuminate\Encryption\EncryptionServiceProvider->Illuminate\Encryption\{closure}(Object(Illuminate\Foundation\Application), Array) 
#2 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Container\Container.php(644): Illuminate\Container\Container->build(Object(Closure), Array) 
#3 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(709): Illuminate\Container\Container->make('encrypter', Array) 
#4 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Container\Container.php(864): Illuminate\Foundation\Application->make('encrypter') 
#5 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Container\Container.php(819): Illuminate\Container\Container->resolveClass(Object(ReflectionParameter)) 
#6 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Container\Container.php(788): Illuminate\Container\Container->getDependencies(Array, Array) 
#7 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Container\Container.php(644): Illuminate\Container\Container->build('App\\Http\\Middle...', Array) 
#8 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(709): Illuminate\Container\Container->make('App\\Http\\Middle...', Array) 
#9 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(127): Illuminate\Foundation\Application->make('App\\Http\\Middle...') 
#10 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) 
#11 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\View\Middleware\ShareErrorsFromSession.php(49): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request)) 
#12 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(137): Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure)) 
#13 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) 
#14 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Session\Middleware\StartSession.php(64): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request)) 
#15 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(137): Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure)) 
#16 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) 
#17 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse.php(37): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request)) 
#18 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(137): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure)) 
#19 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) 
#20 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(104): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request)) 
#21 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Routing\Router.php(655): Illuminate\Pipeline\Pipeline->then(Object(Closure)) 
#22 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Routing\Router.php(629): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request)) 
#23 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Routing\Router.php(607): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request)) 
#24 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(268): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request)) 
#25 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request)) 
#26 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php(46): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request)) 
#27 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(137): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure)) 
#28 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) 
#29 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(104): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request)) 
#30 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(150): Illuminate\Pipeline\Pipeline->then(Object(Closure)) 
#31 C:\Apache24\htdocs\sph\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(117): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request)) 
#32 C:\Apache24\htdocs\sph\public\index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request)) 
#33 {main} 
+0

对此感兴趣。有这个问题几次,“简单”重新安装框架可以解决这个问题。我真的很想知道为什么。 – Loek

+0

你运行过'php工匠密钥:生成'吗? –

+0

另外,你有没有尝试'debug_print_backtrace();'当key *是* null?在每次调用时跟踪调用堆栈和'error_log()'键值可能会很有趣。 –

回答

-3

它是如此简单,APP_KEY.env文件复制到config/app.php -> key以下运行命令:

php artisan key:generate

-2

我安装了Laravel 5.3,但没有找到.env文件。而繁荣......这是问题。我在应用程序的根目录下创建了.env文件并运行

php artisan key:generate

在终端中,它在终端中生成了base 64编码的字符串。将该字符串复制到.env文件中作为

APP_KEY=base64:************************************************

其中****是生成的编码字符串。再次运行应用程序,它应该工作。

9

在多线程Web服务器而不是多进程Web服务器上使用线程安全版本的PHP时,会出现此问题。您可以在Github issue here,Github issue herePHP bug report here上稍微多读一些问题。还有几个链接,但他们只是从我发布的那几个中分出来的。

基本要点是,使用多线程Web服务器,您有一个处理多个线程的单个进程。但是,putenv()/getenv()方法不是线程安全的,并且在进程级别更改环境变量,因此该进程下的所有线程都会受到影响。

所以,你最终的东西是这样的:(如this issue描述):

Request 1: {starts --- loads env --- work --- finishes} 
Request 2:        {starts ----- loads env --- work --- finishes} 

因此,请求1进来,加载环境优良,并开始工作。当请求1正在工作时,请求2进入并在另一个线程上启动。在请求2读取环境变量之前,请求1完成并且PHP清除由putenv()设置的所有变量。现在,请求2次尝试读取环境,但得到null,因为当请求1完成时变量已被清除。

这个问题可以从两个方面来缓解:

  1. 不要在生产中使用.env文件。直接设置环境变量,并禁用phpdotenv。这也被package itself建议:

    phpdotenv is made for development environments, and generally should not be used in production. In production, the actual environment variables should be set so that there is no overhead of loading the .env file on each request.

  2. 切勿使用env()方法配置文件之外,并确保您缓存您的配置文件。通过使用此方法,环境只读一次:创建配置文件缓存时。每个实际的Web请求都会从缓存中读取数据,并且环境变量将永远不会再次被触摸。

+0

@cytsunny只是想知道这个答案是你在找什么。 – patricus

+0

我投了票。只是想等待看看有没有更好的答案。 (例如使用.env解决问题,而不像OP那样破解)如果在赏金结束之前没有其他答案,将奖赏您的赏金。 – cytsunny

相关问题