Laravel 与 sslmode 中的 PostgreSQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25272838/
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 09:56:26  来源:igfitidea点击:

Laravel with PostgreSQL in sslmode

postgresqllaravelamazon-rds

提问by thechrisroberts

I'm working on a project using AWS, hosted on an EC2 instance with a RDS PostgreSQL. I'm wanting to make use of SSL encryption between my server and database, something AWS RDS supportsbut I can't figure out how to configure Laravel to make it work. Is there a way to specify sslmodewhen configuring a database connections?

我正在使用 AWS 处理一个项目,该项目托管在带有 RDS PostgreSQL 的 EC2 实例上。我想在我的服务器和数据库之间使用 SSL 加密,这是AWS RDS 支持的,但我不知道如何配置 Laravel 以使其工作。有没有办法sslmode在配置数据库连接时指定?

回答by John P

This seems to be a missing feature in Laravel 4, but I have submitted a pull request to fix this.

这似乎是 Laravel 4 中缺少的功能,但我已经提交了一个 pull request 来解决这个问题。

https://github.com/laravel/framework/pull/5488(was merged on 2014-09-08)

https://github.com/laravel/framework/pull/5488(于 2014-09-08 合并)

To patch the code just add the following snippet to the getDsnfunction in the PostgresConnector.php file.

要修补代码,只需将以下代码段添加到getDsnPostgresConnector.php 文件中的函数即可。

if (isset($config['sslmode']))
{
    $dsn .= ";sslmode={$sslmode}";
}

After that you may specify sslmode = 'require'in the pgsql database config section to test the encryption between the web app and your database. Other possible values for sslmode are 'disable', 'allow', 'prefer', 'require', 'verify-ca', 'verify-full'. The default value is 'prefer'.

之后,您可以sslmode = 'require'在 pgsql 数据库配置部分指定以测试 Web 应用程序和您的数据库之间的加密。sslmode 的其他可能值为'disable', 'allow', 'prefer', 'require', 'verify-ca', 'verify-full'。默认值为'prefer'

For further info check the PostgreSQL docs: http://www.postgresql.org/docs/9.3/static/libpq-ssl.html

有关更多信息,请查看 PostgreSQL 文档:http: //www.postgresql.org/docs/9.3/static/libpq-ssl.html

回答by Igor Sazonov

  1. You have to tune your pg_hba.confon your PostgreSQL server
  2. Generate server and client certificates
  3. Store your client certificates to Laravel server
  4. edit pgsql section at config/database.php, check my gist https://gist.github.com/tigusigalpa/2d1ec6e258ed96221abf9679622d274a
  1. 你必须pg_hba.conf在你的 PostgreSQL 服务器上调整你的
  2. 生成服务器和客户端证书
  3. 将您的客户端证书存储到 Laravel 服务器
  4. 编辑 pgsql 部分config/database.php,检查我的要点https://gist.github.com/tigusigalpa/2d1ec6e258ed96221abf9679622d274a

回答by Aboozar

Change your Laravel configuration regrading to the official Postgresql documentation:

将您的 Laravel 配置更改为官方Postgresql 文档

see this screenshot: Postgres SSL support options

请参阅此屏幕截图:Postgres SSL 支持选项

change your postgres config in config/database.php to

将 config/database.php 中的 postgres 配置更改为

'pgsql' => [
    'driver' => 'pgsql',
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '5432'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'charset' => 'utf8',
    'prefix' => '',
    'schema' => 'public',
    'sslmode' => env('DB_SSLMODE', 'prefer'), <-- CHANGE THIS
],

and add the new option to your .env file:

并将新选项添加​​到您的 .env 文件中:

DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=mytestdb
DB_USERNAME=postgres
DB_PASSWORD=secret
DB_SSLMODE=disable  <-- ADD THIS