org.postgresql.util.psqlexception:SSL错误:PKIX路径构建失败|无法与SSL连接到邮政服务器
我正在尝试使用我导入到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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论