问题描述
我被这个问题困扰的时间比我想承认的要长。我正在尝试将 postgres db 连接用于我的小型 spring mvc 项目而不是自由。我的 server.xml 如下所示。
<server description="new server">
<!-- Enable features -->
<featureManager>
<feature>jdbc-4.2</feature>
<feature>jsp-2.3</feature>
<feature>localConnector-1.0</feature>
<feature>servlet-4.0</feature>
<feature>ldapRegistry-3.0</feature>
<feature>appSecurity-3.0</feature>
<feature>transportSecurity-1.0</feature>
</featureManager>
<ssl id="defaultSSLSettings" keyStoreRef="defaultKeyStore" trustDefaultCerts="true" />
<keyStore id="defaultKeyStore"
location="/opt/ibm/wlp/usr/servers/defaultServer/resources/security/key.jks"
password="changeIt"/>
<dataSource id="DefaultDataSource" jndiName="jdbc/postgres"
transactional='true' type='javax.sql.ConnectionPoolDataSource'>
<jdbcDriver libraryRef="PostgresLib" />
<properties databaseName="clouddb"
password=""
serverName="ea957de9-2271-4d6c-999e-f2c250575850.budepemd0im5pmu4u60g.databases..cloud"
user="admin" portNumber="30352" />
</dataSource>
<library id="PostgresLib">
<fileset
dir="C:/Users/AkanchaSingh/Desktop/iit-test-app/test-app"
includes="postgresql-42.2.5.jre6.jar" />
</library>
<httpEndpoint host="*" httpPort="9080" httpsPort="9443"
id="defaultHttpEndpoint">
<tcpOptions soReuseAddr="true" />
<httpOptions maxKeepAliveRequests="-1" />
</httpEndpoint>
<applicationManager autoExpand="true"
startTimeout="600" stopTimeout="600"></applicationManager>
<applicationMonitor updateTrigger="mbean" />
<webApplication autoStart="true" contextRoot="test"
id="test" location="/opt/ibm/wlp/usr/servers/defaultServer/test.war"
name="test">
</webApplication>
</server>
我尝试通过所有方法连接到 postgres,例如:数据源和连接管理器
Properties info = new Properties();
String url = "jdbc:postgresql://ea957de9-2271-4d6c-999e-f2c250575850.budepemd0im5pmu4u60g.databases.appdomain.cloud:30352/clouddb";
info.setProperty("user","");
info.setProperty("password","");
info.setProperty("ssl","true");
info.setProperty("sslfactory","org.postgresql.ssl.SingleCertValidatingFactory");
info.setProperty("sslfactoryarg",loadFile("/opt/ibm/wlp/usr/servers/defaultServer/resources/security/PGSSLROOTCERT.crt"));
即使在从 postgres db 连接页面提供 root.crt 之后,我仍然收到
[err] org.postgresql.util.PsqlException:SSL 错误:收到致命警报:handshake_failure [错误] 在 org.postgresql.ssl.MakeSSL.convert(MakeSSL.java:42) [错误] 在 org.postgresql.core.v3.ConnectionFactoryImpl.enableSSL(ConnectionFactoryImpl.java:435) [错误] 在 org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:94) [错误] 在 org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192) [错误] 在 org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) [错误] 在 org.postgresql.jdbc.PgConnection.(PgConnection.java:195)
我也尝试在我的密钥库和信任库中传递证书..在这种情况下似乎没有任何效果..我可以通过 IDE 和 psql 在本地成功连接到 postgres 数据库,但是一旦我将它dockerize 并运行,它抛出这个异常。
Docker 文件:
FROM websphere-liberty:19.0.0.12-full-java8-ibmjava
ENTRYPOINT ["/opt/ibm/wlp/bin/server","run","defaultServer"]
USER root
EXPOSE 9080
copY --chown=1001:0 server.xml /opt/ibm/wlp/usr/servers/defaultServer/
RUN mkdir -p /root/.postgresql
copY --chown=1001:0 root.crt /root/.postgresql/
copY --chown=1001:0 key.jks /opt/ibm/wlp/usr/servers/defaultServer/resources/security/
RUN chmod -R 777 /opt/ibm/wlp/usr/servers/defaultServer/resources/security
copY --chown=1001:0 target/test.war /opt/ibm/wlp/usr/servers/defaultServer/
RUN installUtility install --acceptLicense defaultServer
RUN chmod -R 777 /opt/ibm/wlp/output/defaultServer/workarea
RUN chmod a+rwx /opt/ibm/wlp/output/defaultServer
解决方法
OpenLiberty 有一个自动化测试桶,它在 docker 容器中与 PostgreSQL 一起运行,其中 2 个 Liberty 服务器成功使用了 SSL。这是其中之一:
尝试在您的 <properties.postgresql>
下使用 <dataSource>
元素而不是通用的 <properties>
。前者具有 ssl 的附加属性,如测试用例的 server.xml 所示。