使用Java创建与Oracle数据库的SSL连接

时间:2018-12-12 17:20:50

标签: java oracle ssl jdbc ojdbc

我正在尝试使用Java和SSL连接到Oracle DB。现在,我在具有数据库的服务器上安装了Java程序。

当我尝试运行它时出现此错误(注释中出现完整错误):

java.sql.SQLRecoverableException: IO Error: Inbound closed before receiving peer's close_notify: possible truncation attack?, connect lapse 15 ms., Authentication lapse 0 ms.

这是我的Java代码:

public static void main(String[] args) {
        Security.addProvider(new oracle.security.pki.OraclePKIProvider());
        String url = "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=tcps)(HOST=localhost)(PORT=2484))(CONNECT_DATA=(SERVICE_NAME=ORCL)))"; 
        Properties props = new Properties(); 
        props.setProperty("javax.net.ssl.trustStore", 
                "C:\\app\\Administrator\\virtual\\product\\12.2.0\\dbhome_1\\bin\\wallets\\Administrator\\cwallet.sso"); 
        props.setProperty("javax.net.ssl.trustStoreType","SSO"); 
        props.setProperty("javax.net.ssl.keyStore", 
                "C:\\app\\Administrator\\virtual\\product\\12.2.0\\dbhome_1\\bin\\wallets\\Administrator\\cwallet.sso"); 
        props.setProperty("javax.net.ssl.keyStoreType","SSO"); 
        try {
            Connection conn = DriverManager.getConnection(url, props);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }

我将其添加到listener.ora和tnsnames.ora中(我也尝试了sso文件,只是链接到目录)

WALLET_LOCATION=(SOURCE=(METHOD=FILE) (METHOD_DATA=(DIRECTORY=C:\app\Administrator\virtual\product\12.2.0\dbhome_1\bin\wallets\Administrator\ewallet.p12 ))
SSL_CLIENT_AUTHENTICATION=TRUE

我还将其添加到java.security:

security.provider.11=oracle.security.pki.OraclePKIProvider

edit:我不知道评论的长度限制这么短。这是完整的错误:

Caused by: java.io.IOException: Inbound closed before receiving peer's close_notify: possible truncation attack?, connect lapse 11 ms., Authentication lapse 0 ms.
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:790)
    ... 6 more
Caused by: java.io.IOException: Inbound closed before receiving peer's close_notify: possible truncation attack?, connect lapse 11 ms.
    at oracle.net.ns.NSProtocolNIO.negotiateConnection(NSProtocolNIO.java:138)
    at oracle.net.ns.NSProtocol.connect(NSProtocol.java:317)
    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1438)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:518)
    ... 6 more
Caused by: javax.net.ssl.SSLException: Inbound closed before receiving peer's close_notify: possible truncation attack?
    at sun.security.ssl.Alerts.getSSLException(Unknown Source)
    at sun.security.ssl.SSLEngineImpl.fatal(Unknown Source)
    at sun.security.ssl.SSLEngineImpl.fatal(Unknown Source)
    at sun.security.ssl.SSLEngineImpl.closeInbound(Unknown Source)
    at oracle.net.nt.SSLSocketChannel.fill(SSLSocketChannel.java:534)
    at oracle.net.nt.SSLSocketChannel.unwrap(SSLSocketChannel.java:434)
    at oracle.net.nt.SSLSocketChannel.handshake(SSLSocketChannel.java:350)
    at oracle.net.nt.SSLSocketChannel.write(SSLSocketChannel.java:238)
    at oracle.net.ns.NIOPacket.writeToSocketChannel(NIOPacket.java:211)
    at oracle.net.ns.NIOConnectPacket.writeToSocketChannel(NIOConnectPacket.java:232)
    at oracle.net.ns.NSProtocolNIO.negotiateConnection(NSProtocolNIO.java:108)
    ... 9 more

1 个答案:

答案 0 :(得分:1)

您可以确保在类路径中包含oraclepki.jar,osdt_core.jar和osdt_cert.jar吗?您不需要使用密钥库和信任库属性。使用oracle.net.wallet_location和oracle.net.ssl_server_dn_match = true。请查看blog了解更多详细信息。