ceix
ceix

Reputation: 41

Connect to Oracle DB from Spring-jdbc with Oracle Wallet authentification

I was using Spring-jdbc with org.apache.commons.dbcp.BasicDataSource using the username and password for the connection. I want to use BasicDataSource because I only have one connection.

I had this code:

<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
    id="dataSource">
    <property name="driverClassName" value="${database.driverClassName}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.username}" />
    <property name="password" value="${database.password}" /> 
</bean>

Now I have to use authentication based in Oracle Wallet, I don't have problem in a simple application test without Spring but I can't integrate this new authentication with Spring. Does anyone know How I can do it??

Upvotes: 4

Views: 6604

Answers (1)

djKianoosh
djKianoosh

Reputation: 998

You mention "simple application test" so I'm assuming you need to configure your unit tests. In a unit test config class (for example class TestSpringWebConfig extends SpringWebConfig) this gets you an Oracle datasource using a wallet (bonus: the following uses a proxy database account):

System.setProperty("oracle.net.tns_admin", "path/to/your/tnsnames");

OracleDataSource ds = new OracleDataSource();

Properties props = new Properties();
props.put("oracle.net.wallet_location", "(source=(method=file)(method_data=(directory=path/to/your/wallet)))");

/*
  Use the following only if you have a proxy user database account instead of a normal DB account
  A test user's username could go here though
*/
props.put(OracleConnection.CONNECTION_PROPERTY_PROXY_CLIENT_NAME, "proxy-user-name"); 

ds.setConnectionProperties( props );
ds.setURL("jdbc:oracle:thin:/@dbAlias"); //dbAlias should match what's in your tnsnames

return ds;

This also assumes you have the following in your JDK:

In JAVA_HOME/jre/lib/security/java.security, add the following to the "List of providers":

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

And add the following jars from Oracle to JAVA_HOME/jre/lib/ext:

  • osdt_cert.jar
  • osdt_core.jar
  • oraclepki.jar

And of course, all of the above assumes the ojdbc7 jar is in your application's classpath already.

Upvotes: 4

Related Questions