gpt4 book ai didi

spring - 无法为数据库添加 SSL 支持

转载 作者:行者123 更新时间:2023-11-29 11:40:46 25 4
gpt4 key购买 nike

我正在使用 Spring3、Hibernate4 和 postgres9.2。

为了启用 SSL 数据库连接,我遵循了以下步骤:

  1. 创建自签名证书:引用:http://www.postgresql.org/docs/9.2/static/ssl-tcp.html#SSL-CERTIFICATE-CREATION
  2. 将生成的 server.crtserver.key 复制到 postgres/9.2/data 文件夹中。
  3. hibernate 连接的 URL :jdbc:postgresql://localhost:5432/DB_NAME?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory

重新启动 postgres 后,我运行我的应用程序,它给出错误:

org.postgresql.util.PSQLException: The server does not support SSL.
at org.postgresql.core.v3.ConnectionFactoryImpl.enableSSL(ConnectionFactoryImpl.java:307)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:105)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:65)
at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:140)
at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:29)
at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:21)
at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:31)
at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:23)
at org.postgresql.Driver.makeConnection(Driver.java:393)
at org.postgresql.Driver.connect(Driver.java:267)

即使我尝试在 pg_hba.conf 文件的末尾添加这一行,但 postgres 没有重新启动:

hostssl     all     all         127.0.0.1/32        trust

编辑

它是为收到此类错误或想要添加数据库 ssl 连接的其他人准备的:

我添加了 ssl = true 并从 postgresql.conf 中删除了对 ssl 相关条目的注释并且它起作用了。 :)

最佳答案

问题的根源似乎是您的服务器不支持 SSL 或未启用 SSL。讯息:

The server does not support SSL

可能仅在服务器拒绝或不理解 SSL 时由 org/postgresql/core/v3/ConnectionFactoryImpl.javaenableSSL(...) 中发出请求。

果然,在您的更新中您说您在 postgresql.conf 中将与 SSL 相关的选项注释掉了。它们被注释掉与服务器根本不存在它们是一样的;它会忽略它们。这将导致服务器说它不支持 SSL 并拒绝 SSL 连接,因为它不知道要发送什么服务器证书。当发生这种情况时,PgJDBC 将报告上述错误。

当您取消注释 postgresql.conf 中的 SSL 选项并重新启动服务器时,它开始工作了。

您可能对以下事实感到困惑:

&ssl
&ssl=true
&ssl=false

都做同样的事情:它们启用 SSL。是的,这有点疯狂。就像我们坚持的历史原因一样,但它是clearly documented in the JDBC driver parameter reference :

ssl

Connect using SSL. The driver must have been compiled with SSL support. This property does not need a value associated with it. The mere presence of it specifies a SSL connection. However, for compatibility with future versions, the value "true" is preferred. For more information see Chapter 4, Using SSL.

如您所见,您仍应编写 ssl=true,因为这可能会在未来发生变化。

阅读server configurationclient configuration本手册的部分内容将帮助您设置证书并将证书安装在本地证书列表中,这样您就不必禁用证书信任检查。

对于遇到此问题的任何其他人:您的 PostgreSQL 错误日志中会有更多详细信息,但我猜您的 PostgreSQL 配置不正确,或者您使用的是手动编译的 PostgreSQL 而您没有使用SSL 支持。

关于spring - 无法为数据库添加 SSL 支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16980551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com