通过Java(JDBC)连接到启用SSL的Oracle DB

时间:2016-06-02 05:24:35

标签: java oracle ssl jdbc

我正在进行概念验证(PoC)Java报告项目,其中我使用JDBC Thin驱动程序从我的工作站连接到启用SSL的Oracle数据库。由于数据库启用了SSL,我将所有必需的证书添加到Oracle Wallet中,并在Java代码中提供了它的位置。证书还添加了JRE的 cacert 。 Java代码摘录 -

    try {
      Class.forName("oracle.jdbc.driver.OracleDriver");
    } catch (ClassNotFoundException e) {
      System.out.println("ERROR: Oracle JDBC Driver not found");
      e.printStackTrace();
      return;
    }

    System.out.println("Oracle JDBC Driver Registered!");
    Connection connection = null;
    String oracleURL = "jdbc:oracle:thin:@(DESCRIPTION(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCPS)(HOST=<hostname>)(PORT=2484)))(CONNECT_DATA=(SERVICE_NAME=<service>)))";

    // Provide user ID, password for schema     
    Properties props = new Properties();
    props.setProperty("user", "<user id>");
    props.setProperty("password", "<password>");

    // Setting properties for SSL
    props.setProperty("oracle.net.ssl_cipher_suites", "(ssl_rsa_export_with_rc4_40_md5, ssl_rsa_export_with_des40_cbc_sha, SSL_DH_anon_WITH_3DES_EDE_CBC_SHA, SSL_DH_anon_WITH_RC4_128_MD5,SSL_DH_anon_WITH_DES_CBC_SHA)");
    props.setProperty("oracle.net.ssl_client_authentication", "false");
    props.setProperty("oracle.net.ssl_version", "3.0");
    props.setProperty("oracle.net.encryption_client", "REJECTED");
    props.setProperty("oracle.net.crypto_checksum_client", "REJECTED");
    props.setProperty("javax.net.ssl.keyStore", "C:\\APP\\ORACLE\\product\\11.2.0\\client_1\\ewallet.p12");
    props.setProperty("javax.net.ssl.keyStoreType","PKCS12");
    props.setProperty("javax.net.ssl.keyStorePassword","Password1");

    try {
      connection = DriverManager.getConnection(oracleURL, props); 
    } catch (SQLException e) {
      System.out.println("Connection Failed! Check output console");
      System.out.println("Error code: " + e.getErrorCode());
      System.out.println("SQL State: " + e.getSQLState());
      e.printStackTrace();
      return;
    }

我能够编译程序并使用以下命令运行它 -

java -cp z:\jdk1.7.0_13\bin\ojdbc14.jar;z:\jdk1.7.0_13\bin OracleConnCheck

其中:

  • z:\ jdk1.7.0_13 \ bin \ ojdbc14.jar - ojdbc14.jar的位置
  • z:\ jdk1.7.0_13 \ bin - Java classpath
  • OracleConnCheck - Java类

但我总是遇到伴随NL异常或SO异常的IO错误。我在这里检查了Oracle以及与同一个异常相关的文章,但没有一个解决我的确切问题。有人可以帮忙吗?谢谢!

编辑:添加堆栈跟踪 -

    Oracle JDBC Driver Registered!
Connection Failed! Check output console
Error code: 17002
SQL State: null
java.sql.SQLException: Io exception: NL Exception was generated
        at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:113)
        at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:147)
        at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:257)
        at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:389)
        at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:454)
        at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
        at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
        at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:802)
        at java.sql.DriverManager.getConnection(DriverManager.java:579)
        at java.sql.DriverManager.getConnection(DriverManager.java:190)
        at OracleConnCheck.establishConnection(OracleConnCheck.java:51)
        at OracleConnCheck.main(OracleConnCheck.java:72)

------- The End -------

2 个答案:

答案 0 :(得分:0)

确保使用最新的12.1.0.2 JDBC驱动程序。您可以从OTN下载。此外,您还需要包含osdt_core.jar和osdt_cert.jar。

有关详细信息,请参阅SSL with Oracle JDBC白皮书。

答案 1 :(得分:0)

“生成NL异常”表示连接字符串中存在格式错误。在您的情况下,您在<a href="logout('<%=email%>')">Link</a> 之后遗漏了new String[]{"-i",file1.toString(),"-filter_complex", "[0:v]pad=iw:ih+200:0:(oh-ih)/2:color=white[v0];[v0]drawtext=fontsize=60:fontfile=/system/fonts/DroidSans.ttf:fontcolor=black:text=THIS IS:x=(w-tw)/2:y=(50-th)/2[v1];[v1]drawtext=fontsize=60:fontfile=/system/fonts/DroidSans.ttf:fontcolor=black:text=SPARTA!:x=(w-tw)/2:y=h-25-(th/2)", "-acodec", "copy", final_output2};