org.postgresql.util.psqlexception:SSL错误:PKIX路径构建失败|无法与SSL连接到邮政服务器

发布于 2025-02-06 08:13:00 字数 2817 浏览 2 评论 0原文

我正在尝试使用我导入到CACERTS的服务器的公共密钥将JDBC连接到SSL启用后服务器。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

 public class TestPostgreSql {

/**
 * Connect to the PostgreSQL database
 *
 * @return a Connection object
 * @throws SQLException 
 * @throws KeyStoreException 
 */
public Connection connect() throws SQLException, KeyStoreException {
    Connection conn = null;
    
try {
    Properties props = new Properties();
    props.setProperty("user", "postgres");
    props.setProperty("password", "postgres");
    props.setProperty("ssl", "true");
    props.put("sslmode", "require");
    props.setProperty("sslfactory", "org.postgresql.ssl.DefaultJavaSSLFactory");
  props.setProperty("javax.net.ssl.trustStore","/Users/siena/Library/Java/JavaVirtualMachines/openjdk-17.0.2/Contents/Home/lib/security/cacerts");
       props.setProperty("javax.net.ssl.trustStorePassword","changeit");
      conn = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/", props);
       
      System.out.println("Connected to the PostgreSQL server successfully.");
    } catch (SQLException e) {

    e.printStackTrace();
    }

    return conn;
}

public static void main(String[] args) throws SQLException, KeyStoreException {
    
    TestPostgresql db = new TestPostgresql();
    db.connect();

}

}

但是要得到以下例外:

org.postgresql.util.PSQLException: SSL error: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at org.postgresql.ssl.MakeSSL.convert(MakeSSL.java:43)
    at org.postgresql.core.v3.ConnectionFactoryImpl.enableSSL(ConnectionFactoryImpl.java:571)
    at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:168)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:235)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:223)
    at org.postgresql.Driver.makeConnection(Driver.java:402)
    at org.postgresql.Driver.connect(Driver.java:261)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:681)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:190)
    at com.spdr.datasecurity.rdbms.enumeration.launchenum.TestPostgresql.connect(TestPostgresql.java:95)
    at com.spdr.datasecurity.rdbms.enumeration.launchenum.TestPostgresql.main(TestPostgresql.java:112)

我错过了什么吗?

我尝试过,

props.put("sslmode", "require");
props.setProperty("sslfactory", "org.postgresql.ssl.NonValidatingFactory");

但这仅在不设置服务器仅允许SSL连接时起作用。

I'm trying to make jdbc connection to a SSL enable postgre server using the server's public key that I imported to cacerts.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

 public class TestPostgreSql {

/**
 * Connect to the PostgreSQL database
 *
 * @return a Connection object
 * @throws SQLException 
 * @throws KeyStoreException 
 */
public Connection connect() throws SQLException, KeyStoreException {
    Connection conn = null;
    
try {
    Properties props = new Properties();
    props.setProperty("user", "postgres");
    props.setProperty("password", "postgres");
    props.setProperty("ssl", "true");
    props.put("sslmode", "require");
    props.setProperty("sslfactory", "org.postgresql.ssl.DefaultJavaSSLFactory");
  props.setProperty("javax.net.ssl.trustStore","/Users/siena/Library/Java/JavaVirtualMachines/openjdk-17.0.2/Contents/Home/lib/security/cacerts");
       props.setProperty("javax.net.ssl.trustStorePassword","changeit");
      conn = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/", props);
       
      System.out.println("Connected to the PostgreSQL server successfully.");
    } catch (SQLException e) {

    e.printStackTrace();
    }

    return conn;
}

public static void main(String[] args) throws SQLException, KeyStoreException {
    
    TestPostgresql db = new TestPostgresql();
    db.connect();

}

}

But getting the below exception:

org.postgresql.util.PSQLException: SSL error: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at org.postgresql.ssl.MakeSSL.convert(MakeSSL.java:43)
    at org.postgresql.core.v3.ConnectionFactoryImpl.enableSSL(ConnectionFactoryImpl.java:571)
    at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:168)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:235)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:223)
    at org.postgresql.Driver.makeConnection(Driver.java:402)
    at org.postgresql.Driver.connect(Driver.java:261)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:681)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:190)
    at com.spdr.datasecurity.rdbms.enumeration.launchenum.TestPostgresql.connect(TestPostgresql.java:95)
    at com.spdr.datasecurity.rdbms.enumeration.launchenum.TestPostgresql.main(TestPostgresql.java:112)

Am I missing anything?

I tried with,

props.put("sslmode", "require");
props.setProperty("sslfactory", "org.postgresql.ssl.NonValidatingFactory");

But this only works when the server is not set to allow only SSL connections.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文