qt - 使用 QSqlDatabase 使用 "cert"身份验证方法连接到 PostgreSQL 服务器

标签 qt postgresql

我需要从我的 Qt 应用程序与 PostgreSQL 数据库通信,但是服务器使用“证书”身份验证方法,所以我需要将我的证书传递给服务器。

我目前看到的唯一解决方案是像这样获取 PGconn*:

QSqlDatabase db;
//.....
PGconn* conn = (PGconn*)db.driver()->handle()->data();

并用它做一些工作。甚至拒绝QSqlDatabase,直接使用libpq

如果不使用我的代码中的 libpq,是否还有其他方法可以做到这一点?例如,像这样:

//hypothetic QSqlDatabase methods:
QSqlDatabase db;
//.....
db.SetSslCert("/path/to/my/cert.crt");
db.SetSslKey("/path/to/my/cert.key");
//.....

最佳答案

I need to pass my certificates to the server.

Qt 中没有相应的函数,因为 libpq 中也没有相应的函数。它会自动发生,如 SSL support 中的 Client Certificates 中所述来自 libpq 文档

摘录:

If the server requests a trusted client certificate, libpq will send the certificate stored in file ~/.postgresql/postgresql.crt in the user's home directory. The certificate must be signed by one of the certificate authorities (CA) trusted by the server. A matching private key file ~/.postgresql/postgresql.key must also be present

(在 Windows 中,~/.postgresql 将是 %APPDATA%\postgresql)

Qt 应用程序也会发生同样的情况,因为 Qt 的 QPSQL 驱动程序构建在 libpq 之上。连接使用 SSL 和证书这一事实在本质上是透明的,即使对于驱动程序本身也是如此。

编辑:如果 ~/.posgresql 不方便,因为当它们是多个证书时,存在替代方案:

The location of the certificate and key files can be overridden by the connection parameters sslcert and sslkey or the environment variables PGSSLCERT and PGSSLKEY

连接参数通过QSqlDatabase::setConnectOptions设置.尽管它的文档只提到了少数 postgresql 特定的参数,但它实际上可以接受任何参数,因此 libpq 支持的任何参数都可以使用。

关于qt - 使用 QSqlDatabase 使用 "cert"身份验证方法连接到 PostgreSQL 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27104660/

相关文章:

postgresql - 在 golang 中使用 postgres IN 子句

c++ - 嵌套的 DirectConnection 信号导致段错误

python - 在 PyQt 中处理双向绑定(bind)的更好方法?

c++ - 内部移动 std::vector 元素和 QVector

c++ - opengl中如何用鼠标点击获取帧缓冲区中绘制图形的像素信息

sql - 如何预热/准备表/索引统计信息?

postgresql - Liquibase:我真的需要为单行插入添加回滚部分吗?

c++ - 使用Qt的QTimer函数在OpenGl中制作动画

sql - 从多对多计数

postgresql - Amazon Redshift 中的监控工具