我正在尝试使用Heroku部署Laravel应用程序,该应用程序使用Laravel Passport。
每次部署应用程序时,重建slug都会导致Laravel Passport加密密钥被删除。这意味着每次部署应用程序时都会:
目前,我最终得到的解决方案是将OAuth私钥和公钥存储在两个不同的Heroku配置变量(a.k.a。环境变量)中。我扩展了PassportServiceProvider
类,如果环境设置为"生产":
PassportServiceProvider
中创建了一个新的app/Providers
课程:<?php
namespace App\Providers;
use Laravel\Passport\PassportServiceProvider as LaravelPassportServiceProvider;
use League\OAuth2\Server\AuthorizationServer;
use League\OAuth2\Server\CryptKey;
use League\OAuth2\Server\ResourceServer;
class PassportServiceProvider extends LaravelPassportServiceProvider
{
protected function registerResourceServer()
{
$publicKeyPath = config('app.env') === 'production' ? env('OAUTH_PUBLIC_KEY') : 'oauth-public.key';
$this->app->singleton(ResourceServer::class, function () use ($publicKeyPath) {
return new ResourceServer(
$this->app->make(\Laravel\Passport\Bridge\AccessTokenRepository::class),
$this->makeCryptKey($publicKeyPath)
);
});
}
public function makeAuthorizationServer()
{
$privateKeyPath = config('app.env') === 'production' ? env('OAUTH_PRIVATE_KEY') : 'oauth-private.key';
return new AuthorizationServer(
$this->app->make(\Laravel\Passport\Bridge\ClientRepository::class),
$this->app->make(\Laravel\Passport\Bridge\AccessTokenRepository::class),
$this->app->make(\Laravel\Passport\Bridge\ScopeRepository::class),
$this->makeCryptKey($privateKeyPath),
app('encrypter')->getKey()
);
}
protected function makeCryptKey($key)
{
if (config('app.env') === 'production') {
return new CryptKey(str_replace('\n', "\n", $key), null, false);
}
return new CryptKey(file://'.Passport::keyPath($key), null, false);
}
}
providers
中的config/app.php
数组:Api\Providers\PassportServiceProvider::class,
composer.json
:"extra": {
"laravel": {
"dont-discover": [
"laravel/passport"
]
}
}
由于this issue,此解决方案本身并不完全正常。
但我不相信这是正确的做法。我想知道是否有人已经面临这个问题。如果是这样,你是如何解决的?
提前谢谢。