Bas*_*que 5 postgresql ssl datasource jdbc
在 Java 15 中,使用来自 jdbc.postgresql.org 的 PostgreSQL JDBC 驱动程序(版本 42.2.18,JDBC4.2)DataSource:
PGSimpleDataSource ds = new PGSimpleDataSource();\nds.setServerNames( new String[] { "my-server-address" } );\nds.setPortNumbers( new int[] { 25060 } );\nds.setSsl( true );\nds.setDatabaseName( "mydb" );\nds.setUser( "scott" );\nds.setPassword( "tiger" );\nthis.dataSource = ds;\nRun Code Online (Sandbox Code Playgroud)\n\xe2\x80\xa6代码Connection conn = this.dataSource.getConnection();抛出此异常:
\n\norg.postgresql.util.PSQLException:无法打开 SSL 根证书文件 /Users/basilbourque/.postgresql/root.crt。
\n
我知道我的 Postgres 12 服务器设置为 SSL 连接,因为我的IntelliJ Ultimate具有数据库访问功能 ( DataGrip ),可以成功连接 SSL (TLS) 保护。
\nDataSource那么我的JDBC 配置有什么问题呢?
Bas*_*que 10
感谢pgjdbc 驱动程序的Github 问题页面,我找到了 davecramer 的这篇文章:
从 42.2.5 开始,ssl=true 意味着按照发行说明进行验证。如果您希望获得旧行为,请使用 sslmode=require
果然,替换ds.setSsl( true );为ds.setSslMode( "require" );允许我的 JDBC 驱动程序通过DataSource.
PGSimpleDataSource ds = new PGSimpleDataSource();
ds.setServerNames( new String[] { "my-server-address" } );
ds.setPortNumbers( new int[] { 25060 } );
ds.setSslMode( "require" ); // Replaces: ds.setSsl( true );
ds.setDatabaseName( "mydb" );
ds.setUser( "scott" );
ds.setPassword( "tiger" );
this.dataSource = ds;
Run Code Online (Sandbox Code Playgroud)
我不知道这些 SSL/TLS 相关选项实际上在做什么,但这对我连接到我的 DigitalOcean 管理的 Postgres 数据库服务器很有用。
以下代码片段现在可以成功运行:
try
(
Connection conn = this.dataSource.getConnection() ;
Statement stmt = conn.createStatement() ;
)
{
String sql =
"""
SELECT uuid_generate_v1()
;
""";
try (
ResultSet rs = stmt.executeQuery( sql ) ;
)
{
while ( rs.next() )
{
UUID uuid = rs.getObject( 1 , UUID.class );
System.out.println( "uuid = " + uuid );
}
}
}
catch ( SQLException e )
{
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15689 次 |
| 最近记录: |